ggplot中的嵌套x标签:超过2,同时避免重新排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ggplot中的嵌套x标签:超过2,同时避免重新排序相关的知识,希望对你有一定的参考价值。

我正在尝试在绘图中创建4个级别的x轴标签。我发现大多数例子只使用2,所以不确定我想做什么是可能的。

数据:

data<-data.frame(treatment = c('Control', 'Trap', 'Control', 'Trap','Control', 'Trap','Control', 'Trap', 'Control', 'Trap'),
             n = c('(n=45)', '(n=59)', '(n=39)', '(n=111)','(n=65)', '(n=112)','(n=140)', '(n=35)', '(n=153)', '(n=71)'),
             area = c('here', 'here', 'here', 'here', 'there', 'there', 'here', 'here', 'there', 'there'),
             year = c('2015', '2015','2016', '2016','2016', '2016','2017', '2017','2017', '2017'),
             transposed = c('black', 'black', 'blue', 'red', 'green', 'orange', 'red', 'blue', 'orange', 'green'),
             estimate = c(0.9623486,0.964039,0.9471145,0.9487335,0.8527007,0.9639663,0.9530781,0.9492815,0.9318947,0.9654943), 
             se = c(0.008084,0.007722,0.0077928,0.0052604,0.0121859,0.0065871,0.0056207,0.0059268,0.0130123,0.0055851)) 

我尝试了不同的东西w / facet_wrap和annotate导致各种错误(比如点堆叠在彼此之上)。最差的当前情节(数据和标签混合):

library(ggplot2)
# wrong labels and misorders data points 
 ggplot(data, aes(n, estimate^34)) + 
   geom_point () +
   geom_line() +
   geom_errorbar(aes(ymin = (estimate - 2*se)^34, ymax = (estimate + 
                             2*se)^34 )) + 
   labs(x = "how to nest treatment and n within area, within year?", 
        y = "Nest Survival") +
   theme_classic()

enter image description here

我希望数据的绘制顺序与数据框中的顺序相同。

期望的情节(具有正确的数据排序):enter image description here

我很多类似的问题被标记为重复,但我认为有这么多标签“级别”会使它与众不同吗?非常感谢任何指导。

PS。数据框中的“转置”列是我希望每个点和CL将按顺序排列的颜色,但这可能是另一种蠕虫

答案

这不是你要求的,但也许你会考虑一种不同的方法。你可以使用视觉效果(颜色,vline)来呈现4个级别的数据,而不是简单地在你的情节中添加文字(“一张图片胜过千言万语”)。

转换数据:

# x-axis
# We need numerics for geom_vline    
data$Xname <- with(data, paste0(treatment, "
", n))
data$Xposition <- 1:nrow(data)
# y-axis
data$Ymin <- with(data, (estimate - 2 * se)^34)
data$Ymax <- with(data, (estimate + 2 * se)^34)
# year labels and positions
dataYear <- aggregate(data$Xposition, list(data$year), mean)
dataYear$y <- max(data$Ymax) + max(data$Ymax) * 0.1

绘图数据:

library(ggplot2)

ggplot(data, aes(Xposition, estimate^34)) + 
    geom_vline(xintercept = which(diff(as.numeric(data$year)) == 1) + 0.5,
               color = "grey", linetype = 2) +
    geom_text(data = dataYear, aes(x, y, label = Group.1),
              size = 5) +
    geom_errorbar(aes(ymin = Ymin, ymax = Ymax)) + 
    geom_point(aes(color = area), size = 4) +
    scale_x_continuous(breaks = data$Xposition, labels = data$Xname) +
    labs(x = NULL,
         y = "Nest Survival") +
    theme_classic() +
    theme(axis.text.x = element_text(size = 13),
          legend.position = "bottom")

结果:

enter image description here

另一答案

我认为Po的答案很棒,但使用facet_grid可以使它更容易一些,并节省了大量额外的数据准备。

# borrowing from Po's answer and using library(forcats) to set factor order like we want
data$Xname <- forcats::fct_inorder(with(data, paste0(treatment, "
", n)))

ggplot(data, aes(Xname, estimate^34, color = area)) + 
    geom_point() +
    geom_line() +
    geom_errorbar(aes(ymin = (estimate - 2*se)^34, ymax = (estimate + 2*se)^34 )) +
    labs(x = NULL,
         y = "Nest Survival") +
    facet_grid(~year, scales = "free_x", space = "free_x") +
    theme_classic()

enter image description here

以上是关于ggplot中的嵌套x标签:超过2,同时避免重新排序的主要内容,如果未能解决你的问题,请参考以下文章

ggplot2:如何动态包装/调整大小/重新缩放 x 轴标签,使它们不会重叠

安排多个ggplots,但避免由于轴标签而挤压

R语言ggplot2可视化使用guide_axis函数避免X轴标签互相重叠(Overlapping)Dodge Overlapping X-axis Text with guide_axis()

在ggplot2中的x轴标签下方添加图像

如何将普通的ggplot与cowplot中的多面对齐?

删除ggplot中的所有x轴标签[重复]