如何用ggplot(geom_boxplot)在同一列中放置多个箱形图 [英] How to place multiple boxplots in the same column with ggplot(geom_boxplot)

查看:928
本文介绍了如何用ggplot(geom_boxplot)在同一列中放置多个箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个boxplot,其中4个因子(N1:N4)叠加在同一列中。例如,使用以下数据:

  df <-data.frame(N = N,Value = Value)
Q< -C( C1, C1, C2, C3, C3, C1, C1, C2, C2, C3, C3, Q1\" , Q1, Q1, Q1, Q3, Q3, 第四季度, 第四季度, Q1, Q1, Q1, Q1, Q3 Q3,Q4,Q4)
N <-c(N2,N3,N3,N2,N3,N2,N3 N2, N3, N2, N3, N0, N1, N2, N3, N1, N3, N0, N1,N0 ,N1,N2,N3,N1,N3,N0,N1)
值<-c(4.7,8.61,8.34,5.89,8.36,1.76 ,2.4,5.01,2.12,1.88,3.01,2.4,7.28,4.34,5.39,11.61,10.14,3.02,9.45,8.8,7.4,6.93,8.44,7.37,7.81,6.74,8.5)


$ b

与以下(通常)代码相结合,输出为4个箱形图,显示4个变量

  ggplot(df,aes(x = N,y = Value,color = N))+ theme_bw(base_size = 20)+ geom_boxplot()

非常感谢

解决方案 div>

更新回答



根据您的评论,这里有一个添加边际箱型图的方法。我们将使用内置的 mtcars 数据框。



首先,一些设置:

 库(cowplot)

#常用主题元素
thm = list(theme_bw(),
guides(color = FALSE,fill = FALSE),
theme(plot.margin = unit(rep(0,4),lines)))



现在,创建三个图:

 #主要阴谋
p1 = ggplot(mtcars,aes(wt,mpg,color = factor(cyl),fill = factor(cyl))+
geom_smooth(method =lm)+ labs(color =Cyl,fill =Cyl)+
scale_y_continuous(limits = c(10,35))+
thm [-2] +
theme(legend.position = c 0.85,0.8))

#上边界图
p2 = ggplot(mtcars,aes(factor(cyl),wt,color = factor(cyl))+
geom_boxplot ()+ thm + coord_flip()+ labs(x =Cyl,y =)

#右边界图
p3 = ggplot(mtcars,aes(factor(cyl) ,mpg,color = factor(cyl)))+
geom_boxplot()+ thm + labs(x =Cyl,y =)+
scale_y_co ntinuous(limits = c(10,35))

布置图并添加图例:

  plot_grid(plotlist = list(p2,ggplot(),p1,p3),ncol = 2,
rel_widths = c(5,1),rel_heights = c(1,5),align =hv)



原始回答



您可以将所有四个箱型图叠加在一个列中,但情节将不可读。下面的第一个例子将 N 作为x坐标,但保留 N 作为颜色审美。这导致 N 这四个等级被绘制在单个刻度线上(我通过设置 breaks NULL )。然而,情节仍然躲闪。要将它们叠加在一起,请将闪避宽度设置为零,如我在第二个示例中所做的那样。然而,这些图在重叠时不可读。

  ggplot(df,aes(x =,y = Value ,color = N))+ 
theme_bw(base_size = 20)+
geom_boxplot()+
scale_x_discrete(breaks = NULL)+
labs(x =)

ggplot(df,aes(x =,y = Value,color = N))+
theme_bw(base_size = 20)+
geom_boxplot(position = position_dodge(0) )+
scale_x_discrete(breaks = NULL)+
labs(x =)


I would like to built a boxplot in which the 4 factors (N1:N4) are overlaid in the same column. For example with the following data:

df<-data.frame(N=N,Value=Value)
Q<-c("C1","C1","C2","C3","C3","C1","C1","C2","C2","C3","C3","Q1","Q1","Q1","Q1","Q3","Q3","Q4","Q4","Q1","Q1","Q1","Q1","Q3","Q3","Q4","Q4")
N<-c("N2","N3","N3","N2","N3","N2","N3","N2","N3","N2","N3","N0","N1","N2","N3","N1","N3","N0","N1","N0","N1","N2","N3","N1","N3","N0","N1")
Value<-c(4.7,8.61,8.34,5.89,8.36,1.76,2.4,5.01,2.12,1.88,3.01,2.4,7.28,4.34,5.39,11.61,10.14,3.02,9.45,8.8,7.4,6.93,8.44,7.37,7.81,6.74,8.5)

with the following (usual) code, the output is 4 box-plots displayed in 4 columns for the 4 variables

ggplot(df, aes(x=N, y=Value,color=N)) +  theme_bw(base_size = 20)+ geom_boxplot()

many thanks

解决方案

Updated Answer

Based on your comment, here's a way to add marginal boxplots. We'll use the built-in mtcars data frame.

First, some set-up:

library(cowplot)

# Common theme elements
thm = list(theme_bw(), 
           guides(colour=FALSE, fill=FALSE),
           theme(plot.margin=unit(rep(0,4),"lines")))

Now, create the three plots:

# Main plot
p1 = ggplot(mtcars, aes(wt, mpg, colour=factor(cyl), fill=factor(cyl))) +
  geom_smooth(method="lm") + labs(colour="Cyl", fill="Cyl") +
  scale_y_continuous(limits=c(10,35)) +
  thm[-2] +
  theme(legend.position = c(0.85,0.8)) 

# Top margin plot
p2 = ggplot(mtcars, aes(factor(cyl), wt, colour=factor(cyl))) +
  geom_boxplot() + thm + coord_flip() + labs(x="Cyl", y="")

# Right margin plot
p3 = ggplot(mtcars, aes(factor(cyl), mpg, colour=factor(cyl))) +
  geom_boxplot() + thm + labs(x="Cyl", y="") +
  scale_y_continuous(limits=c(10,35))

Lay out the plots and add the legend:

plot_grid(plotlist=list(p2, ggplot(), p1, p3), ncol=2, 
          rel_widths=c(5,1), rel_heights=c(1,5), align="hv")

Original Answer

You can overlay all four boxplots in a single column, but the plot will be unreadable. The first example below removes N as the x coordinate, but keeps N as the colour aesthetic. This results in the four levels of N being plotted at a single tick mark (which I've removed by setting breaks to NULL). However, the plots are still dodged. To plot them one on top of the other, set the dodge width to zero, as I've done in the second example. However, the plots are not readable when they are overlaid.

ggplot(df, aes(x="", y=Value,color=N)) + 
  theme_bw(base_size = 20) + 
  geom_boxplot() +
  scale_x_discrete(breaks=NULL) +
  labs(x="")

ggplot(df, aes(x="", y=Value,color=N)) + 
  theme_bw(base_size = 20) + 
  geom_boxplot(position=position_dodge(0)) +
  scale_x_discrete(breaks=NULL) +
  labs(x="")

这篇关于如何用ggplot(geom_boxplot)在同一列中放置多个箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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