Home > front end >  Make value labels all same height on GGPLOT2?
Make value labels all same height on GGPLOT2?

Time:06-18

I would like to make all of my value labels to be at the same height above the bars on ggplot2. How can I do this?

p <- ggplot(figure_df, aes(x=event_type, y=fraction))  
  geom_bar(aes(fill=factor(method)), position=position_dodge(width=0.9), stat="identity")  
  facet_grid(~event_time, switch="x")  
  theme(axis.text.x = element_text(angle = 45,hjust = 1))  
  geom_text(aes(label=ifelse(method == 'THRIVE',round(pval, 2)," ")))

enter image description here

CodePudding user response:

It's always best to make your questions reproducible. Check it out: enter image description here

You said you wanted the values above the bars. You can set it to a known value or you can use the data to set a singular static value, like this:

ggplot(df1, aes(x = x, y = y, fill = coloring))  
  geom_col(position = position_dodge(.9))   
  geom_text(aes(y = max(y)   1, label = round(y, 1)),
            position = position_dodge(.9))

enter image description here

If you didn't want them at the same height, but you wanted them at the same distance from the bars, you can do that in a variety of different ways. Here is one method.

ggplot(df1, aes(x = x, y = y, fill = coloring))  
  geom_col(position = position_dodge(.9))   
  geom_text(aes(y = y   1, label = round(y, 1)),
            position = position_dodge(.9))

enter image description here

  • Related