ggplot2 ::在所有面板中使用相同的参考绘图来刻画绘图 [英] ggplot2:: Facetting plot with the same reference plot in all panels

查看:167
本文介绍了ggplot2 ::在所有面板中使用相同的参考绘图来刻画绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想面向一个情节,但在每个小组中都有一个参考情节。让我试着用图片展示我想达到的效果:我的例子data_frame:

  require(dplyr)

df < - data_frame(id = c(rep('ctr',40),rep('pat',80)),
class = c(rep('ctr',40),rep (c('a','b'),each = 40)),
rank = rep(1:20,6),
mean = c(rep(seq(3,-3, 2),
rep(seq(1,-4,length.out = 20),2),
rep(seq(-2,-8,length.out = 20),2)),
sd = rep(seq(1.2,0.8,length.out = 20),times = 6),
exam = rep(c('blue','red '),each = 20,times = 3))

我的情节:

 #首先创建'controls'的引用图
require(ggplot2)
p_ctr < - ggplot()+
geom_line(data = filter(df,id =='ctr'),
aes(x = rank,y = mean,color = exam),linetype = 1)+
geom_r ibbon(data = filter(df,id =='ctr'),
aes(x = rank,ymax = mean + sd,ymin = mean-sd,
fill = exam),alpha =。 1)+
scale_colour_manual(values = c(#00b6eb,#eb0041))+
scale_fill_manual(values = c(#00b6eb,#eb0041))

#然后覆盖病人的情节

p_ctr + geom_line(data = filter(df,id =='pat'),
aes(x = rank, y = mean,linetype = class))+
geom_ribbon(data = filter(df,id =='pat'),
aes(x = rank,ymax = mean + sd,ymin = sd,
group = class),
alpha = .1)+
facet_wrap(〜exam)

即将到达:

理想情况下,我想绘制不同的类在单独的p anels,但在每个面板中将控制图作为参考:

预期结果



我尝试了不同的切面组合,没有很好的效果。我想,必须有一个简单的解决方案?

可能是这样的。

  library(dplyr)
library(ggplot2)

df1< - filter( (df,id =='pat')
df2 < - dplyr :: rename(df2,class_2 = class)

p_ctr < - ggplot()+
geom_line(data = df1,aes(x = rank,y = mean,color = exam))+
geom_ribbon(data = df1,
aes(x = rank,ymax = mean + sd,ymin = mean-sd,fill = exam),
alpha = .1)+
scale_colour_manual(values = c(#00b6eb ,#eb0041))+
scale_fill_manual(values = c(#00b6eb,#eb0041))+
geom_line(data = df2,
aes(x = rank, y =均值))+
geom_ribbon(data = df2,
aes(x = rank,ymax = mean + sd,ymin = mean-sd),
alpha = .1)+
facet_grid(class_2〜exam)

p_ctr



使用 facet_wrap 会给我以下错误:

gList中的

错误(列表(x = 0.5,y = 0.5,width = 1,height = 1,只是=center,:
只有'grobs' gList







在寻找解决方案时, p>

  p_ctr + geom_line(data = filter(df,id =='pat'),
aes(x = rank, y =均值))+
geom_ribbon(data = filter(df,id =='pat'),
aes(x = rank,ymax = mean + sd,ymin = mean-sd),
alpha = .1)+
#facet_wrap(〜exam)+
facet_grid(class〜exam)



这基本上是你的参考情节,它的结束没有 linetype group 参数。此外,我还分了 class〜exam 。从这个图中你可以看到'问题'在于 class 包含三个独特的元素: a b ctr 。这就是为什么我将 df2 中变量 class 重命名为 class_2 >,它只有两个独特的元素: a b 。通过 class_2〜exam 的分面,然后提供所需的输出。



我希望这有助于。


I would like to facet a plot, but with a reference plot in each panel. Let me try to show with pictures what I want to achieve: My example data_frame:

require(dplyr)

df <- data_frame( id = c(rep('ctr',40), rep('pat',80)),
                  class = c(rep('ctr',40), rep(c('a','b'), each = 40)),
                  rank =  rep (1:20,6),
                  mean = c(rep(seq(3,-3, length.out = 20),2), 
                           rep(seq(1,-4, length.out = 20),2), 
                           rep(seq(-2,-8, length.out = 20),2)),
                  sd = rep(seq(1.2,0.8, length.out = 20), times = 6),
                  exam = rep(c('blue','red'), each = 20, times = 3))

My plot:

# first, create reference plot of the 'controls'
require(ggplot2)
p_ctr <- ggplot() + 
 geom_line(data = filter(df, id == 'ctr'),
            aes(x=rank, y=mean, color=exam), linetype=1) + 
  geom_ribbon(data = filter(df, id == 'ctr'),
              aes(x = rank, ymax = mean+sd, ymin = mean-sd, 
                  fill = exam), alpha = .1) +
  scale_colour_manual(values = c("#00b6eb","#eb0041")) + 
  scale_fill_manual(values = c("#00b6eb","#eb0041")) 

# then, overlay with plot of 'patients'

p_ctr + geom_line(data = filter(df, id == 'pat'), 
              aes(x=rank, y=mean, linetype = class)) +
  geom_ribbon(data = filter(df, id == 'pat'), 
              aes(x = rank, ymax = mean+sd, ymin = mean-sd,
                  group = class), 
              alpha = .1) +
  facet_wrap(~exam)

That is halfway there: Ideally, however, I would like to plot the different "classes" in separate panels, but with the control plot as a reference in each panel:

Expected result:

I have tried different combinations of facetting, without good result. I guess, there must be a simple solution?

解决方案

Maybe like so.

library(dplyr)
library(ggplot2)

df1 <- filter(df, id == 'ctr')
df2 <- filter(df, id == 'pat')
df2 <- dplyr::rename(df2, class_2 = class)

p_ctr <- ggplot() + 
 geom_line(data = df1, aes(x=rank, y=mean, color=exam)) +
 geom_ribbon(data = df1,
             aes(x = rank, ymax = mean+sd, ymin = mean-sd, fill = exam),
             alpha = .1) +
 scale_colour_manual(values = c("#00b6eb","#eb0041")) +
 scale_fill_manual(values = c("#00b6eb","#eb0041")) +
 geom_line(data = df2,
           aes(x=rank, y=mean)) +
 geom_ribbon(data = df2,
             aes(x = rank, ymax = mean+sd, ymin = mean-sd),
             alpha = .1) +
 facet_grid(class_2 ~ exam)

p_ctr

Using facet_wrap gives me the following error:

error in gList(list(x = 0.5, y = 0.5, width = 1, height = 1, just = "centre", : only 'grobs' allowed in "gList"


You probably came across this plot while looking for the solution.

p_ctr + geom_line(data = filter(df, id == 'pat'), 
                  aes(x=rank, y=mean)) +
        geom_ribbon(data = filter(df, id == 'pat'), 
                    aes(x = rank, ymax = mean+sd, ymin = mean-sd), 
                    alpha = .1) +
      # facet_wrap(~exam) +
        facet_grid(class ~ exam)

This is basically your reference plot and its overlay, without the linetype and group arguments. Additionally I faceted by class ~ exam. From this plot you see that 'the problem' is that class contains three unique elements: a, b and ctr. That's why I renamed the variable class in df2 to be class_2 which has only two unique elements: a and b. Faceting by class_2 ~ exam then gives the desired output.

I hope this helps.

这篇关于ggplot2 ::在所有面板中使用相同的参考绘图来刻画绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆