R 使用ggplot按组映射平均值
使用示例数据帧:R 使用ggplot按组映射平均值,r,ggplot2,R,Ggplot2,使用示例数据帧: df <- structure(list(value = c(10L, 8L, 6L, 4L, 2L, 9L, 7L, 5L, 3L, 1L, 1L, 1L, 2L, 3L, 4L, 3L, 3L, 4L, 5L, 2L, 2L, 4L, 6L, 4L, 7L, 3L, 5L, 4L, 6L, 3L), length = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L
df <- structure(list(value = c(10L, 8L, 6L, 4L, 2L, 9L, 7L, 5L, 3L,
1L, 1L, 1L, 2L, 3L, 4L, 3L, 3L, 4L, 5L, 2L, 2L, 4L, 6L, 4L, 7L,
3L, 5L, 4L, 6L, 3L), length = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L,
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L), wave = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L)), .Names = c("value", "length", "wave"
), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-30L), spec = structure(list(cols = structure(list(value = structure(list(), class = c("collector_integer",
"collector")), length = structure(list(), class = c("collector_integer",
"collector")), wave = structure(list(), class = c("collector_integer",
"collector"))), .Names = c("value", "length", "wave")), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
我们可以使用
stat\u summary
完成此任务
library(ggplot2)
ggplot(df, aes(x = length, y = value, col = as.factor(wave))) +
stat_summary(geom = "line", fun.y = mean)
谢谢@Markus。当我运行我的全部数据时,会有一些观察结果。首先,我的长度从1到10。但当我看图表时,它是1,10,2,3,4。。。是否可以指定数字的运行方式?其次,图没有形成,我得到了几何路径:每组只包含一个观测值。您是否需要调整团队美学?我该怎么办?你的
length
列可能是一个因素吗?对于错误:请尝试group=factor(wave)
insideggplot()
。在上面,我使用了col
。如果仍然不起作用,请编辑您的问题并使用dput()
共享您的数据。
library(ggplot2)
ggplot(df, aes(x = length, y = value, col = as.factor(wave))) +
stat_summary(geom = "line", fun.y = mean)