Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.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 聚合如何影响几何图形文本?_R_Ggplot2_Aggregate - Fatal编程技术网

R 聚合如何影响几何图形文本?

R 聚合如何影响几何图形文本?,r,ggplot2,aggregate,R,Ggplot2,Aggregate,我在网上的某个地方找到了下面的代码,并一直在尝试调整它以满足我的需要。在执行此操作时,我发现了几种情况,它们会发出错误消息,但我不明白为什么 我用4种不同版本的平均值计算方法尝试了以下代码: library(ggplot2) mtcars$cyl <- as.factor(mtcars$cyl) means1 <- aggregate(mtcars[1], mtcars[2], mean) means2 <- aggregate(list(mpg = mtcars$mpg)

我在网上的某个地方找到了下面的代码,并一直在尝试调整它以满足我的需要。在执行此操作时,我发现了几种情况,它们会发出错误消息,但我不明白为什么

我用4种不同版本的平均值计算方法尝试了以下代码:

library(ggplot2)

mtcars$cyl <- as.factor(mtcars$cyl)

means1 <- aggregate(mtcars[1], mtcars[2], mean)
means2 <- aggregate(list(mpg = mtcars$mpg), list(cyl = mtcars$cyl), mean)
means3 <- aggregate(mtcars[1], list(mtcars$cyl), mean)
means4 <- aggregate(mtcars$mpg, mtcars[2], mean)

means <- means1  #or means2, means3, means4
means[2] <- round(means[2], digits=2)

p <- ggplot(data = mtcars, aes(x = cyl, y = mpg))+
     geom_dotplot(binaxis = "y", stackdir = "center")
p + stat_summary(fun.data = "mean_sdl", fun.args = list(mult = 1), 
             geom = "errorbar", width = 0.2, color = "red") +
stat_summary(fun.y = mean, geom = "point", color = "red") +
geom_text(data = means, aes(label = means[2]),hjust = -0.5)
库(ggplot2)

mtcars$cyl自行调查的一种方法是在控制台上打印
means1
means2
等,并查看
means3
的不同之处。你觉得有什么不同吗?现在您可以将其连接到错误消息中的语言了吗?一个有用的命令是str()。比较str(means1)、str(means3)和str(means4)的输出。在这种情况下,只需查看输出数据帧就可以更轻松地完成。你会发现,实际上你已经没有了一个名为cyl的专栏了。@joran&kintany:我在发帖前查看了数据,并在问题中加入了我的思路,因为太长了,无法发表评论。