geom_boxplot geom_line结合

时间:2022-03-02 13:06:30

I would like to combine a boxplot and a line plot using ggplot2. However, I am struggling to have lines for each group (g) connecting points across the categories on the x-axis.

我想把boxplot和line plot结合使用ggplot2。但是,我很难在x轴上为每个组(g)提供跨类别的连接点。

To demonstrate the problem:

证明存在的问题:

df <- data.frame(x = rep(letters[1:3],each=5), 
                 y = c(1:5,sample(10,5),1:5), 
                 g = rep(LETTERS[1:5],3) )

library(ggplot2)
ggplot(df, aes(x=x,y=y)) + geom_boxplot() + geom_point(aes(colour = g))

geom_boxplot geom_line结合

I want to have a line connecting all A points, a line connecting all B points and so on.

我想要一条直线连接所有的点,一条连接所有B点的线等等。

I have looked at some answers here but I cannot incorporate them. They suggest using group=1 but this will not suit my individual lines for each group in g. Is it possible using stat_summary maybe?

我在这里看了一些答案,但是我不能把它们合并在一起。他们建议使用group=1,但这不适用于g中的每组。是否可能使用stat_summary ?

1 个解决方案

#1


3  

... +  geom_line(aes(group = g))

#1


3  

... +  geom_line(aes(group = g))