2

I got this plot

enter image description here

Using this script

ggplot(df, aes(x=site_id, y=value)) + 
  geom_bar(stat ="identity", width = 0.8, fill = "lightgreen")+
  facet_wrap(~var,  scales ="free_y")+
  theme_bw()+
  labs(x= " ")+
  theme(axis.text.x=element_text(angle=45, vjust=1, hjust=1,size = 8))

and the data df below.

I want the values to be placed inside the bars of the highest sites (site5 and site6) and on the top of the bars for other sites. I managed to do it for one variable as below

ggplot(df[df$var=="A", ], aes(x=site_id, y=value)) + 
  geom_bar(stat ="identity", width = 0.8, fill = "lightgreen")+
  facet_wrap(~var,  scales ="free_y")+
  geom_text(aes(label=round(value,  digits = 1),
                vjust=0.3, 
                hjust=ifelse(value>5, 1.3,-0.3),
                angle = 90), 
            size=3,
            color="black"
            )+
  theme_bw()+
  labs(x= " ")+
  theme(axis.text.x=element_text(angle=45, vjust=1, hjust=1,size = 8)) 

enter image description here

I can do the same for all variables (one at a time) and then combine all the plots. However, this is time taking especially if I have many variables. I wonder if there any straightforward way to do that in the facet_wrap() with different scales for y axis. Any suggestions will be highly appreciated.

DATA

df <- read.table(text =c("
site_id var value
site1   A   0.177764513
site1   D   7.830275133
site1   B   0.248247923
site1   E   12.56416097
site1   C   0.751543862
site1   F   671.0885718
site2   A   0.967731591
site2   D   57.89824801
site2   B   1.304393848
site2   E   66.67904785
site2   C   4.993486213
site2   F   7078.505853
site3   A   0.919473968
site3   D   77.01265019
site3   B   1.144594697
site3   E   90.94579823
site3   C   2.369818009
site3   F   993.2029832
site4   A   2.033933861
site4   D   69.3294669
site4   B   2.749690832
site4   E   109.264453
site4   C   10.76299681
site4   F   8490.393252
site5   A   8.389513302
site5   D   271.7607716
site5   B   10.79398426
site5   E   403.8976818
site5   C   41.75038226
site5   F   34569.96598
site6   A   17.77809543
site6   D   638.3953947
site6   B   22.11052259
site6   E   876.9638354
site6   C   80.14343933
site6   F   77342.83885"), header =T)
shiny
  • 3,380
  • 9
  • 42
  • 79

1 Answers1

2

You can achieve what you want by subsetting the data in two separate geom_text calls:

library(dplyr)

n <- 2
ggplot(df, aes(x=site_id, y=value)) + 
  geom_col(width = 0.8, fill = "lightgreen")+
  geom_text(data = df %>% group_by(var) %>% top_n(n, value),
            aes(label = round(value,  digits = 1)),
            vjust = 0.3,
            hjust =  1.3,
            angle = 90, 
            size = 3,
            color = "black") +
  geom_text(data = df %>% 
              group_by(var) %>% 
              top_n(nlevels(.$site_id) - n, -value),
            aes(label = round(value,  digits = 1)),
            vjust = 0.3, 
            hjust =  -0.3,
            angle = 90, 
            size = 3,
            color = "black") + 
  facet_wrap(~var, scales = "free_y") +
  theme_bw() +
  labs(x = "") +
  theme(axis.text.x = element_text(angle = 45, 
                                   vjust = 1, 
                                   hjust = 1,
                                   size = 8))

enter image description here

yeedle
  • 4,918
  • 1
  • 22
  • 22