geom_text values to appear neater on top of graphs

here is my dataframe

data.frame(
  stringsAsFactors = FALSE,
           Subtype = c("OPC", "Hypopharynx", "Larynx"),
             alive = c(88, 22, 100),
              dead = c(12, 55, 17),
         uncertain = c(10, 2, 2)
)

I then enter the following code

tata3 %>% gather(key = "status", value = value, -Subtype) %>% group_by(Subtype) %>% mutate(value = value / sum(value) * 100, status = factor(status, c("alive", "dead", "uncertain"))) %>% ggplot(aes(y = value, x = Subtype, fill = status)) + geom_col(width = .6, position = position_dodge(width = .6, preserve = "total")) + geom_text(aes(label = paste(round(value, 2), "%"), y = value + 3,group = status),position = position_dodge(width = .7, preserve = "total")) +labs(y = "percentage", x = "Status") + scale_fill_manual( values = c("alive" = "tan2", "dead" = "red", "uncertain" = "green"),aesthetics = c("color", "fill")) +theme(text = element_text(size = 15)) + scale_y_continuous(labels = scales::label_percent(scale = 1, accuracy = 1),limits = c(0, 100))

This gives me the graph that I want as attached. The only problem is that some of the values diaplayed appear to be on the graph

can I make this look neater...for e.g. wth regards to the 14.29% in the Larynx, I do not want the '1' to be touching the orange graph.

Many Thanks

I should add that 'tata3' is the name of the dataframe. I've tried various permutations of adjusting the geom_col width values and also the 0.7 in the "position dodge". Any offers?

Is this better?

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
tata3 <- data.frame(
  stringsAsFactors = FALSE,
  Subtype = c("OPC", "Hypopharynx", "Larynx"),
  alive = c(88, 22, 100),
  dead = c(12, 55, 17),
  uncertain = c(10, 2, 2)
)
tata3 %>% gather(key = "status", value = value, -Subtype) %>% 
  group_by(Subtype) %>% 
  mutate(value = value / sum(value) * 100, status = factor(status, c("alive", "dead", "uncertain"))) %>% 
  ggplot(aes(y = value, x = Subtype, fill = status)) + 
  geom_col(width = .6, position = position_dodge(width = .6, preserve = "total")) + 
  geom_text(aes(label = paste(round(value, 1), "%"), y = value + 7,group = status),
            position = position_dodge(width = .6, preserve = "total"),size=3,angle=90) +
  labs(y = "percentage", x = "Status") + 
  scale_fill_manual( values = c("alive" = "tan2", "dead" = "red", "uncertain" = "green"),aesthetics = c("color", "fill")) +
  theme(text = element_text(size = 15)) + 
  scale_y_continuous(labels = scales::label_percent(scale = 1, accuracy = 1),limits = c(0, 100))

Created on 2022-02-26 by the reprex package (v2.0.1)

Thanks,

Looks much neater. Didn't think of simply rotating the angle by 90degrees.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.