使用线条和多因素连接 ggplot 箱线图
Connect ggplot boxplots using lines and multiple factor
我正在尝试将 ggplot2 箱线图与 geom_lines 联系起来用于多种因素。到目前为止,我已经能够完成用线连接所有箱线图的工作,请参见附图。但我希望通过相应的因子连接唯一的箱线图。
例如,对于我的变量 FL,我只想连接这两个箱线图,而不将它们与其余变量连接。与变量 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~.)
有人知道如何实现吗?我希望我能以最清楚的方式解释我自己。
非常感谢和最良好的祝愿,
我不知道如何准确地连接你制作的情节中的点。但我可以告诉你如何做类似的事情。
困难在于,属于 对 箱线图的所有点共享相同的 x 坐标(即 variable
的相同值)。因此,我使用 interaction(sex, variable)
作为 x 坐标,这样每个箱线图都有自己的 x 值。然而,这意味着成对的箱线图不太明显。另一方面,连接线在另一个方向工作。
为了绘制线条,它们按 interaction(index, variable)
分组,这意味着当数据点共享 index
和 variable
的相同值时,它们是连接的。
废话少说,代码如下:
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 的回答绝对是获得所需输出的最快、最干净的方法。建立他的答案,因为对于给定的变量,地块不再彼此相邻,您可能希望将每个地块放在自己单独的地块中。只需对@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 = "")
如果你想把顶部的条带移到底部,我也做了一些挖掘,这并不难。改编代码自:How to display strip labels below the plot when faceting?
我们得到:
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)
我正在尝试将 ggplot2 箱线图与 geom_lines 联系起来用于多种因素。到目前为止,我已经能够完成用线连接所有箱线图的工作,请参见附图。但我希望通过相应的因子连接唯一的箱线图。
例如,对于我的变量 FL,我只想连接这两个箱线图,而不将它们与其余变量连接。与变量 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~.)
有人知道如何实现吗?我希望我能以最清楚的方式解释我自己。
非常感谢和最良好的祝愿,
我不知道如何准确地连接你制作的情节中的点。但我可以告诉你如何做类似的事情。
困难在于,属于 对 箱线图的所有点共享相同的 x 坐标(即 variable
的相同值)。因此,我使用 interaction(sex, variable)
作为 x 坐标,这样每个箱线图都有自己的 x 值。然而,这意味着成对的箱线图不太明显。另一方面,连接线在另一个方向工作。
为了绘制线条,它们按 interaction(index, variable)
分组,这意味着当数据点共享 index
和 variable
的相同值时,它们是连接的。
废话少说,代码如下:
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 的回答绝对是获得所需输出的最快、最干净的方法。建立他的答案,因为对于给定的变量,地块不再彼此相邻,您可能希望将每个地块放在自己单独的地块中。只需对@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 = "")
如果你想把顶部的条带移到底部,我也做了一些挖掘,这并不难。改编代码自:How to display strip labels below the plot when faceting?
我们得到:
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)