简体   繁体   English

使用线和多因素连接ggplot箱图

[英]Connect ggplot boxplots using lines and multiple factor

I'm trying to connect ggplot2 boxplots with geom_lines for multiple factors. 我正在尝试将ggplot2箱线图与geom_lines连接以考虑多种因素。 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. 例如,对于我的变量FL,我只想连接这两个箱形图,而不将它们与其余变量连接。 Similarly for the variable RW, connecting those two sex boxplot without the remaining others. 类似地,对于变量RW,将这两个性别盒图连接起来,而没有剩下的。

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, 非常感谢和最良好的祝愿,

I don't know of way to connect the points in exactly the plot that you produced. 我不知道如何将点精确连接到您生成的图中。 But I can show you how to do something similar. 但我可以向您展示如何做类似的事情。

The difficulty is that all the points that belong to a pair of boxplots share the same x-coordinate (that is, the same value for variable ). 困难在于,属于一箱形图的所有点共享相同的x坐标(即, variable值相同)。 Therefore I have used interaction(sex, variable) as the x-coordinate, such that each boxplot has its own x-value. 因此,我已将interaction(sex, variable)用作x坐标,以便每个箱线图都有自己的x值。 This means, however, that the paired boxplots are less visible. 但是,这意味着成对的箱形图不太明显。 On the other hand, the connecting lines work in the other direction. 另一方面,连接线沿另一个方向工作。

In order to plot the lines, they are grouped by interaction(index, variable) , which means that data points are connected, when they share the same values for index and variable . 为了绘制线,将它们按interaction(index, variable)分组,这意味着当数据点共享相同的indexvariable值时,数据点已连接。

Enough talk, here's the 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~.) +
    scale_x_discrete(labels = rep(levels(melt_crabs$variable), each = 2))

在此处输入图片说明

@Stibu's answer is definitely the quickest and cleanest way to get your desired output. @Stibu的答案绝对是获得所需输出的最快,最干净的方法。 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: 只需对@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, 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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM