Home > Blockchain >  Is there an easy way to adjust text labels individually in ggplot2?
Is there an easy way to adjust text labels individually in ggplot2?

Time:12-08

I am trying to add sample size labels to my boxplot in ggplot. I have everything I think I need but I am not sure how to finely adjust the position of the labels. I tried position_nudge and position_jitter but I want to be able to adjust each label individually so I don't have to worry about the label running into the mean bar.

My code with my data gives me this boxplot

give.n <- function(x){
  return(c(y = mean(x), label = length(x)))
}

ggplot(ratings_county_2019_DI, aes(x=county, y=di)) 
  geom_boxplot(aes(fill = county), fatten = 1.25) 
  scale_fill_manual(values = col19, aesthetics = c("colour", "fill")) 
  labs(x = "County", y = " DSI (%)") 
  theme_minimal() 
  theme(legend.position = "top") 
  stat_summary(fun.data = give.n, geom = "text") 
  ggtitle("Disease Serverity Index 2019 Survey") 
  theme(plot.title = element_text(hjust = 0.5))

CodePudding user response:

If you give position = position_nudge(y = VECTOR_OF_NUDGES) that might provide the control you're looking for.

give.n <- function(x){
  return(c(y = mean(x), label = length(x)))
}

ggplot(mtcars, aes(x=as.character(gear), y=mpg)) 
  geom_boxplot(aes(fill = cyl), fatten = 1.25)  
  stat_summary(fun.data = give.n, geom = "text",
               position = position_nudge(y = c(0, -2.5, -1)))

enter image description here

  • Related