微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ggplot:geom_text不在geom_col上方居中对齐

如何解决ggplot:geom_text不在geom_col上方居中对齐

请在下面找到我的数据p。我必须包含100个样本才能重现错误

问题:为什么geom_text不能始终在geom_col上方居中对齐打印-​​例如All面的SSA中的 21 28 ?我尝试调整position.dodge2vjust,但这没用。

This thread解决了该问题,但没有解决我的问题。

enter image description here

我的脚本

  ggplot(p %>%
           mutate(nystudie=as.character(study),best.resp =as.factor(response)) %>%       
           group_by(nystudie,best.resp) %>%      
           summarise(N=n(),Val=unique(treatment)) %>%    
           bind_rows(p %>% filter(response %in% 1:4,treatment!="Control") %>% droplevels() %>%       
                       mutate(nystudie=as.character(study),best.resp =as.factor(response)) %>%        
                       group_by(best.resp,treatment) %>% summarise(N=n()) %>%       
                       mutate(nystudie="All") %>%       
                       rename(Val=treatment)),aes(nystudie,N,color = best.resp,fill= best.resp)) +   
    geom_col(position = position_dodge2(preserve = "single",padding = 0.1)) +   
    facet_wrap(~Val,ncol = 2,scales="free") +
    scale_fill_grey(name="") +
    scale_color_grey(name="") +
    scale_y_continuous(breaks = seq(0,120,20)) +
    geom_text(aes(label=N),position = position_dodge2(.5),vjust=0,fontface=2,cex=4.5,show.legend = F) +
    theme(strip.background = element_blank(),strip.text = element_text(color = "black",size = 15),axis.text.x = element_text(angle = 90,vjust = 0.5,hjust=1),plot.margin = unit(c(1,3,1,1),"lines"))

数据

p <- structure(list(study = structure(c(8L,12L,4L,1L,11L,13L,14L,9L,10L,8L,6L,15L,7L,16L,2L,2L),.Label = c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","22"),class = "factor"),response = c("1","1","3"),treatment = structure(c(2L,1L),.Label = c("sstR","SSA"),class = "factor")),row.names = c(NA,-100L),class = "data.frame")

解决方法

添加标签时,必须注意使用与geom_col相同的位置。要将标签与条对齐,请使用position_dodge2(preserve = "single",width = .9,padding = 0.1)

library(ggplot2)
library(dplyr)

d1 <- p %>%
  mutate(
    nystudie = as.character(study),best.resp = as.factor(response)
  ) %>%
  group_by(nystudie,best.resp) %>%
  summarise(N = n(),Val = unique(treatment))
#> `summarise()` regrouping output by 'nystudie' (override with `.groups` argument)

d2 <- p %>%
  filter(response %in% 1:4,treatment != "Control") %>%
  droplevels() %>%
  mutate(
    nystudie = as.character(study),best.resp = as.factor(response)
  ) %>%
  group_by(best.resp,treatment) %>%
  summarise(N = n()) %>%
  mutate(nystudie = "All") %>%
  rename(Val = treatment)
#> `summarise()` regrouping output by 'best.resp' (override with `.groups` argument)

d <- bind_rows(d1,d2)

ggplot(d,aes(nystudie,N,color = best.resp,fill = best.resp)) +
  geom_col(position = position_dodge2(preserve = "single",padding = 0.1)) +
  facet_wrap(~Val,ncol = 2,scales = "free") +
  scale_fill_grey(name = "") +
  scale_color_grey(name = "") +
  scale_y_continuous(breaks = seq(0,120,20)) +
  geom_text(aes(label = N),position = position_dodge2(preserve = "single",padding = 0.1),vjust = 0,fontface = 2,cex = 4.5,show.legend = F) +
  theme(
    strip.background = element_blank(),strip.text = element_text(color = "black",size = 15),axis.text.x = element_text(angle = 90,vjust = 0.5,hjust = 1),plot.margin = unit(c(1,3,1,1),"lines")
  )

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。