Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 向x轴添加嵌套/分组级别_R_Ggplot2 - Fatal编程技术网

R 向x轴添加嵌套/分组级别

R 向x轴添加嵌套/分组级别,r,ggplot2,R,Ggplot2,我希望在x轴上添加第二层分组,如下面的结果a面板所示。与标签3或12相对应的每种估算类型(ITT与TOT)应有两点 以下是我获取所见内容的方法,减去对面板结果的编辑: df %>% ggplot(., aes(x=factor(estimate), y=gd, group=interaction(estimate, time), shape=estimate)) + geom_point(position=position_dodge(width=0.5)) + geom_e

我希望在x轴上添加第二层分组,如下面的结果a面板所示。与标签3或12相对应的每种估算类型(ITT与TOT)应有两点

以下是我获取所见内容的方法,减去对面板结果的编辑:

df %>%
  ggplot(., aes(x=factor(estimate), y=gd, group=interaction(estimate, time), shape=estimate)) +
  geom_point(position=position_dodge(width=0.5)) +
  geom_errorbar(aes(ymin=gd.lwr, ymax=gd.upr), width=0.1,
                position=position_dodge(width=0.5)) +
  geom_hline(yintercept=0) +
  ylim(-1, 1) +
  facet_wrap(~outcome, scales='free', strip.position = "top") +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  theme(panel.spacing = unit(0, "lines"), 
        strip.background = element_blank(),
        strip.placement = "outside")
以下是玩具数据:

df <- structure(list(outcome = c("Outcome C", "Outcome C", "Outcome C", 
"Outcome C", "Outcome B", "Outcome B", "Outcome B", "Outcome B", 
"Outcome A", "Outcome A", "Outcome A", "Outcome A"), estimate = c("ITT", 
"ITT", "TOT", "TOT", "ITT", "ITT", "TOT", "TOT", "ITT", "ITT", 
"TOT", "TOT"), time = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L), .Label = c("3", "12"), class = "factor"), 
    gd = c(0.12, -0.05, 0.19, -0.08, -0.22, -0.05, -0.34, -0.07, 
    0.02, -0.02, 0.03, -0.03), gd.lwr = c(-0.07, -0.28, -0.11, 
    -0.45, -0.43, -0.27, -0.69, -0.42, -0.21, -0.22, -0.33, -0.36
    ), gd.upr = c(0.31, 0.18, 0.5, 0.29, 0, 0.17, 0.01, 0.27, 
    0.24, 0.19, 0.38, 0.3)), class = "data.frame", row.names = c(NA, 
-12L))
df

x
美学更改为
交互(时间、因子(估计))
并添加了合适的离散标签

df %>%
  ggplot(., aes(x = interaction(time, factor(estimate)),                   # relevant
                y = gd, group = interaction(estimate, time), 
                shape = estimate)) +
  geom_point(position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(ymin = gd.lwr, ymax = gd.upr), width = 0.1,
                position = position_dodge(width = 0.5)) +
  geom_hline(yintercept = 0) +
  ylim(-1, 1) +
  facet_wrap(~outcome, scales = 'free', strip.position = "top") +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  theme(panel.spacing = unit(0, "lines"), 
        strip.background = element_blank(),
        strip.placement = "outside") +
  scale_x_discrete(labels = c("3\nITT", "12\nITT", "3\nTOT", "12\nTOT"))   # relevant

使用
网格发布解决方案。排列
。我更新了答案,只包含一个图例

    library(dplyr)
    library(ggplot2)

   p1 <- ggplot(filter(df, outcome == "Outcome A"), 
             aes(x = time,                   # relevant
                    y = gd, group = interaction(estimate, time), 
                    shape = estimate)) +
  geom_point(position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(ymin = gd.lwr, ymax = gd.upr), width = 0.1,
                position = position_dodge(width = 0.5)) +
  geom_hline(yintercept = 0) +
  ylim(-1, 1) +
  scale_x_discrete("")+
  facet_wrap(~estimate, scales = 'free_x', strip.position = "bottom") +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  theme(panel.spacing = unit(0, "lines"), 
        strip.background = element_blank(),
        strip.placement = "bottom",
        panel.border = element_rect(fill = NA, color="white")) +
  ggtitle("Outcome A")



p2 <- ggplot(filter(df, outcome == "Outcome B"), 
             aes(x = time,                   # relevant
                 y = gd, group = interaction(estimate, time), 
                 shape = estimate)) +
  geom_point(position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(ymin = gd.lwr, ymax = gd.upr), width = 0.1,
                position = position_dodge(width = 0.5)) +
  geom_hline(yintercept = 0) +
  ylim(-1, 1) +
  scale_x_discrete("")+
  facet_wrap(~estimate, scales = 'free_x', strip.position = "bottom") +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  theme(panel.spacing = unit(0, "lines"), 
        strip.background = element_blank(),
        strip.placement = "bottom",
        panel.border = element_rect(fill = NA, color="white")) +
  ggtitle("Outcome B")

p3 <-  ggplot(filter(df, outcome == "Outcome C"), 
              aes(x = time,                   # relevant
                  y = gd, group = interaction(estimate, time), 
                  shape = estimate)) +
  geom_point(position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(ymin = gd.lwr, ymax = gd.upr), width = 0.1,
                position = position_dodge(width = 0.5)) +
  geom_hline(yintercept = 0) +
  ylim(-1, 1) +
  scale_x_discrete("")+
  facet_wrap(~estimate, scales = 'free_x', strip.position = "bottom") +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  theme(panel.spacing = unit(0, "lines"), 
        strip.background = element_blank(),
        strip.placement = "bottom",
        panel.border = element_rect(fill = NA, color="white")) +
  ggtitle("Outcome C")


#layout matrix for the 3 plots and one legend

lay <- rbind(c(1,2,3,4),c(1,2,3,4),
             c(1,2,3,4),c(1,2,3,4))


g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}
#return one legend for plot 
aleg <- g_legend(p1)
gp1 <- p1+ theme(legend.position = "none")
gp2 <- p2+ theme(legend.position = "none")
gp3 <- p3+ theme(legend.position = "none")



gridExtra::grid.arrange(gp1,gp2,gp3,aleg, layout_matrix = lay)
库(dplyr)
图书馆(GG2)

p1这不是一个完美的解决方案,但它可能更具可伸缩性。它基于
cowplot
中的渐晕图

我按结果分割数据,然后使用
purr::imap
列出三个相同的图,而不是单独创建它们或硬编码任何内容。然后我使用2个
cowplot
函数,一个用于将图例提取为
ggplot
/
gtable
对象,另一个用于构建绘图和其他类似绘图的对象的网格

每个图仅针对一个结果绘制,x轴上的
时间为3或12,并由
估计值分面绘制。与您所做的类似,面被伪装成更像字幕

您可能需要进一步调整一些设计问题。例如,我调整了缩放比例以在组之间填充,以获得发布的外观。我把面板边框换成轴线,以避免在每一个图的中间有一个边框,因为它将在两个面之间画出一个更好的方法。

库(tidyverse)
绘图列表%
分割(.$结果)%>%
imap(功能(子功能、结果名称){
ggplot(子图df,aes(x=作为系数(时间),y=gd,形状=估计))+
几何误差条(aes(ymin=gd.lwr,ymax=gd.upr),宽度=0.1,位置=位置减淡(宽度=0.5))+
几何点(位置=位置减淡(宽度=0.5))+
geom_hline(yintercept=0)+
比例x离散(扩展=扩展比例(添加=2))+
ylim(-1,1)+
面_包裹(~估计,strip.position=“底部”)+
主题_bw()+
主题(panel.grid=element\u blank(),
面板间距=单位(0,“线”),
panel.border=元素_blank(),
axis.line=元素线(color=“黑色”),
strip.background=元素\空白(),
strip.placement=“外部”,
plot.title=元素\文本(hjust=0.5))+
实验室(标题=结果\名称)
})
列表中的每个绘图都是:

plot_list[[1]]

提取图例,然后映射到绘图列表以删除其图例


图例是否可能重复?可能是重复的谢谢,@iod。我找到了这个答案,但我在帖子中说,考虑到自2010年以来ggplot的所有发展,可能有更好的方法。谢谢,@Mike。在您指出的示例中,只有一个刻面术语,但我认为我至少需要使用两个,因为我已经刻面了
结果。结果看起来不太对劲。说得好,罗曼。我简化了数据框。谢谢@古罗马的有关修改
x
的有用信息。使用离散标签的有趣方法。如果我希望每对3/12点只有一个“ITT”和一个“TOT”标签(居中),我需要使用@Mike描述的方法吗?我可能会在实践中使用您的解决方案,因为它让我非常接近简单的代码。标记了一个不同的答案“正确”,因为它解决了超级标签的挑战。这绝对是好的,值得的,我很高兴它有帮助!在这一点上,也许
patchwork
会更直接一些?是的,我考虑过
patchwork
,因为我一直在将一些东西从
cowplot
切换到
patchwork
,但想利用
cowplot::get_legend
。这个函数对于这样的事情非常方便,而AFAIK
patchwork
没有类似的功能