使用线和多种因素连接ggplot箱形图 [英] Connect ggplot boxplots using lines and multiple factor

查看:215
本文介绍了使用线和多种因素连接ggplot箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将ggplot2盒形图与geom_lines连接起来,以获得多种因素。到目前为止,我已经能够完成将所有盒线与线连接,请参阅附加图片。但我希望通过它们相应的因子连接唯一的箱型图。





例如,对于我的变量FL,我只想连接那两个boxplot,而不将它们与其余变量连接起来。

  library(MASS)
同样对于变量RW,连接这两个性别boxplot数据(螃蟹)
melt_crabs< - 熔化(螃蟹,id.var = c(sp,sex,index))
ggplot(melt_crabs,aes(x = variable,y = value))+ geom_line(aes(group = index),size = 0.05,alpha = 0.7)+ geom_boxplot(aes(fill = sp),alpha = 0.5)+ facet_grid(sex〜。)

有谁知道如何做到这一点?我希望我能以最清晰的方式解释自己。



非常感谢和祝福,

方案

@ Stibu的答案绝对是获得您想要的输出的最快速和最干净的方法。从他的回答开始,既然对于一个给定的变量,情节不再是彼此相邻的,你可能希望把每个情节放在他们自己的独立情节中。只需对@ Stibu的代码中的 facet_grid 调用进行一些小调整即可:

  ggplot(melt_crabs,aes(x = interaction(sex,variable),y = value))+ 
geom_boxplot(aes(fill = sex),alpha = 0.5)+
geom_line aes(group = interaction(index,variable)),
alpha = 0.5,color =darkgrey)+
facet_grid(sp_variable,scales =free_x)+
scale_x_discrete labels =)



如果您想将顶部条移动到底部,我也会进行一些挖掘,但并不困难。修改以下代码:


I'm trying to connect ggplot2 boxplots with geom_lines for multiple factors. I'd been able to accomplish so far to connect all the boxplot with lines, see attach picture. But I wish to connect the only boxplots by their corresponding factor.

For example for my variable FL, I want to connect only those two boxplot, without connecting them with the remaining variables. Similarly for the variable RW, connecting those two sex boxplot without the remaining others.

library("MASS")  
data(crabs)  
melt_crabs <- melt(crabs,id.var=c("sp","sex","index"))   
ggplot(melt_crabs, aes(x = variable, y = value)) +   geom_line(aes(group = index), size = 0.05, alpha = 0.7) +   geom_boxplot(aes(fill = sp), alpha = 0.5) + facet_grid(sex~.)

Does anyone know how to achieve this? I hope I'd explain myself the most clear way.

Many thanks and best wishes,

解决方案

@Stibu's answer is definitely the quickest and cleanest way to get your desired output. Building off his answer, since for a given variable, the plots are no longer next to each other, you might want to put each in their own separate plot. This can be easily by only a minor tweak to the facet_grid call in @Stibu's code:

ggplot(melt_crabs, aes(x = interaction(sex, variable), y = value)) +
  geom_boxplot(aes(fill = sex), alpha = 0.5) +
  geom_line(aes(group = interaction(index, variable)),
            alpha = 0.5, colour = "darkgrey") +
  facet_grid(sp~variable,scales="free_x") +
  scale_x_discrete(labels = "")

Also I did some digging if you want to move the top strip to the bottom, it isn't that difficult. Adapting the code from: How to display strip labels below the plot when faceting?

We get:

p<-ggplot(melt_crabs, aes(x = interaction(sex, variable), y = value)) +
  geom_boxplot(aes(fill = sex), alpha = 0.5) +
  geom_line(aes(group = interaction(index, variable)),
            alpha = 0.5, colour = "darkgrey") +
  facet_grid(sp~variable,scales="free_x") +
  scale_x_discrete(labels = "")

# Convert the plot to a grob
gt <- ggplotGrob(p)

# Get the positions of the panels in the layout: t = top, l = left, ...
panels <-c(subset(gt$layout, name == "panel", select = t:r))

# Get the strip grob & x-axis label grob
stripGrob = gtable_filter(gt, "strip-top")

#Replace x-axis ticks with strip
gt = gtable_add_grob(gt, stripGrob, t = max(panels$b)+1, l = min(panels$l), r = max(panels$r))

# remove the old strip
gt = gt[-(min(panels$t)-1), ]

grid.newpage()
grid.draw(gt)

这篇关于使用线和多种因素连接ggplot箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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