Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplot按组映射平均值_R_Ggplot2 - Fatal编程技术网

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)
inside
ggplot()
。在上面,我使用了
col
。如果仍然不起作用,请编辑您的问题并使用
dput()
共享您的数据。
library(ggplot2)
ggplot(df, aes(x = length, y = value, col = as.factor(wave))) + 
  stat_summary(geom = "line", fun.y = mean)