【问题标题】:Add a line from different result to boxplot graph in ggplot2在ggplot2中将不同结果的线添加到箱线图
【发布时间】:2013-03-20 02:34:38
【问题描述】:

我有一个包含 3 列 (y1, y2, x) 的数据框 (df1)。我设法在 y1, x 和 y2, x 之间绘制了一个箱线图。我有另一个数据框(df2),其中包含两列 A,x。我想绘制一个折线图(A,x)并将其添加到箱线图中。请注意,两个数据框中的变量 x 是轴访问,但是,它具有不同的值。我试图根据因子(x)组合和重塑数据框和绘图......我在一张图中得到了 3 个箱线图。我需要在一个图中将 df2 绘制为线,将 df1 绘制为箱线图。

df1 <- structure(list(Y1 = c(905L, 941L, 744L, 590L, 533L, 345L, 202L, 
369L, 200L, 80L, 200L, 80L, 50L, 30L, 60L, 20L, 30L, 30L), Y2 = c(774L, 
823L, 687L, 545L, 423L, 375L, 249L, 134L, 45L, 58L, 160L, 60L, 
20L, 40L, 20L, 26L, 19L, 27L), x = c(10L, 10L, 10L, 20L, 20L, 
20L, 40L, 40L, 40L, 50L, 50L, 50L, 70L, 70L, 70L, 90L, 90L, 90L
 )), .Names = c("Y1", "Y2", "x"), row.names = c(NA, -18L), class = "data.frame")

df2 <- structure(list(Y3Line = c(384L, 717L, 914L, 359L, 241L, 265L, 
240L, 174L, 114L, 165L, 184L, 96L, 59L, 60L, 127L, 54L, 31L, 
44L), x = c(36L, 36L, 36L, 56L, 56L, 56L, 65L, 65L, 65L, 75L, 
75L, 75L, 85L, 85L, 85L, 99L, 99L, 99L)), .Names = c("A", 
"x"), row.names = c(NA, -18L), class = "data.frame")

df_l <- melt(df1, id.vars = "x")

ggplot(df_l, aes(x = factor(x), y =value, fill=variable  )) +
geom_boxplot()+
#  here I'trying to add the line graph from df2
geom_line(data = df2, aes(x = x, y=A))

有什么建议吗?

【问题讨论】:

标签: r ggplot2


【解决方案1】:

在第二个数据集中,每个 x 值有三个 y 值,你想为每个 x 值绘制单独的线还是每个 x 值的平均值?两者如下图所示。诀窍是首先将两个数据集中的 x 变量更改为包含两个变量所有水平的因子。

df1 <-structure(list(Y1 = c(905L, 941L, 744L, 590L, 533L, 345L, 202L, 
369L, 200L, 80L, 200L, 80L, 50L, 30L, 60L, 20L, 30L, 30L), Y2 = c(774L, 
823L, 687L, 545L, 423L, 375L, 249L, 134L, 45L, 58L, 160L, 60L, 
20L, 40L, 20L, 26L, 19L, 27L), x = c(10L, 10L, 10L, 20L, 20L, 
20L, 40L, 40L, 40L, 50L, 50L, 50L, 70L, 70L, 70L, 90L, 90L, 90L
)), .Names = c("Y1", "Y2", "x"), row.names = c(NA, -18L), class = "data.frame")

df2 <- structure(list(Y3Line = c(384L, 717L, 914L, 359L, 241L, 265L, 
240L, 174L, 114L, 165L, 184L, 96L, 59L, 60L, 127L, 54L, 31L, 
44L), x = c(36L, 36L, 36L, 56L, 56L, 56L, 65L, 65L, 65L, 75L, 
75L, 75L, 85L, 85L, 85L, 99L, 99L, 99L)), .Names = c("A", 
"x"), row.names = c(NA, -18L), class = "data.frame")

library(ggplot2)
library(reshape2)

df_l <- melt(df1, id.vars = "x")

allLevels <- levels(factor(c(df_l$x,df2$x)))
df_l$x <- factor(df_l$x,levels=(allLevels))
df2$x <- factor(df2$x,levels=(allLevels))

每 x 类别的行数:

ggplot(data=df_l,aes(x = x, y =value))+geom_line(data=df2,aes(x = factor(x), y =A))  + 
geom_boxplot(aes(fill=variable )) 

x 个类别的连通均值:

ggplot(data=df2,aes(x = factor(x), y =A)) + 
stat_summary(fun.y=mean, geom="line", aes(group=1))  + 
geom_boxplot(data=df_l,aes(x = x, y =value,fill=variable )) 

【讨论】:

  • 第二个数据集包含两列新结果。但是,第二列“x”是第一个数据集中的相同变量。我设法绘制了第一个数据集的箱线图。我需要绘制第二个数据的简单折线图(geom_line())并将其与箱线图相结合。
  • 它为每个数据提供了一条垂直线。我需要有一条继续线。我知道,因为每个唯一的 x 值都有 3 个“A”值。但我想我仍然可以绘制它(借助均值函数)。
  • 代码没有连接线。我需要将线与箱线图分开。查看所需图表的更新。
  • 你应该更好地检查我的答案,最后一个情节正是如此。该行从 36 开始(与您的最新绘图相反),但这是由于第二个数据集。
  • 很抱歉,我收到了这个错误:“geom_path:每个组只包含一个观察。您需要调整组审美吗?”这似乎是由于 X 变量。??
猜你喜欢
  • 1970-01-01
  • 2023-04-11
  • 2018-05-11
  • 1970-01-01
  • 2018-02-14
相关资源
最近更新 更多