r - ggplot : geom_text does not center-align above geom_col()

标签 r ggplot2 plot facet-wrap

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

问题:为什么 geom_text 打印时没有在 geom_col 上方始终居中对齐 - 例如右侧 SSA 面的 All 中的 2128 ?我尝试调整 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, 12L, 12L, 4L, 4L, 
1L, 11L, 11L, 13L, 1L, 13L, 14L, 9L, 9L, 10L, 12L, 11L, 4L, 11L, 
11L, 12L, 8L, 11L, 13L, 11L, 6L, 15L, 6L, 4L, 7L, 13L, 11L, 4L, 
1L, 6L, 1L, 11L, 16L, 1L, 10L, 15L, 1L, 11L, 1L, 6L, 1L, 11L, 
12L, 11L, 13L, 16L, 1L, 8L, 11L, 10L, 4L, 4L, 12L, 10L, 6L, 15L, 
12L, 14L, 12L, 1L, 1L, 16L, 12L, 12L, 8L, 7L, 1L, 1L, 13L, 13L, 
14L, 9L, 14L, 2L, 11L, 4L, 1L, 16L, 15L, 11L, 9L, 4L, 13L, 12L, 
6L, 16L, 4L, 1L, 15L, 6L, 4L, 1L, 9L, 2L), .Label = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", 
"15", "22"), class = "factor"), response = c("1", "3", "4", "4", 
"3", "3", "3", "4", "4", "4", "4", "4", "3", "4", "4", "4", "3", 
"4", "4", "4", "4", "3", "1", "4", "4", "4", "3", "4", "3", "3", 
"4", "4", "4", "3", "4", "4", "4", "4", "4", "3", "4", "4", "3", 
"4", "4", "3", "3", "4", "3", "4", "4", "4", "4", "3", "3", "4", 
"4", "3", "3", "4", "3", "4", "4", "4", "3", "3", "4", "4", "4", 
"4", "2", "4", "4", "4", "4", "4", "3", "4", "3", "3", "4", "4", 
"4", "4", "4", "4", "3", "3", "4", "4", "3", "4", "4", "4", "4", 
"3", "3", "4", "2", "3"), treatment = structure(c(2L, 2L, 2L, 
2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 
1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 
1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 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", width = .9, 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")
  )

关于r - ggplot : geom_text does not center-align above geom_col(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64091110/

相关文章:

r - 在单个 ROC 图上绘制线性判别分析、分类树和朴素贝叶斯曲线

r - 如何将标签与圆环图中的中间/中心对齐

r - 如何在同一窗口中打印三个维恩图

animation - 如何在 Julia 中绘制多个 3D 轨迹(大概使用 for 循环?)——还有,如何随着时间的推移制作动画?

r - 用 R 中的连续索引向量化向量和矩阵的叉积

r - 使用ggplot获取带有百分比标签的条形图的最有效方法

r - 将合并应用于 data.frames 列表

r - 在circos中使用circos函数

r - 如何在ggplot的geom_label中设置标准标签大小?

r - 无法用神经网络图生成PDF