繁体   English   中英

我正在尝试将 plot geom_bar 放到 ggplot 上,但是 geom_bar 具有不同的示例源,并且 R 会弹出错误

[英]I am trying to plot geom_bar onto ggplot, but geom_bar has a differing sample source and R pops out an error

我想将 plot geom_bar 放到代表数据平均值的 ggplot 上。 我为 ggplot 获取了较小的数据,以免压倒 plot,但想为条形 plot 使用更大的数据,不幸的是它不允许我这样做,因为它说: Aesthetics must be either length 1 or the same as the data (2)这很奇怪,因为它似乎在喊一个错误“aes(x = min,y = rating)”但是当我删除geom bar时一切正常。 有没有人有任何想法?

ggplot(random_scores, aes(x = win, y = ratings)) +
  geom_point() +
  geom_bar(data = avg_samples, stat = "identity", alpha = 0.4 )

这是另一个尝试:

ggplot(random_scores, aes(x = win, y = ratings)) +
  geom_point() +
  geom_bar(data = avg_samples, stat = "identity", alpha = 0.4, aes(x = win, y =ratings ))

我相信这就是您想要实现的目标。 使用了著名的iris数据集。 如果这是您正在寻找的,您知道,接下来会发生什么。 只需更改 df,字段名称。

iris %>% ggplot(aes(x = as.factor(Species), y = Petal.Length)) +
  geom_point() +
  geom_bar(stat = "summary", aes(fun = "mean"), alpha = 0.5)

编辑即使是建议的方法也适用于我并给出相同的 output。

avg_length = iris %>% group_by(Species) %>% summarise(mean_len = mean(Petal.Length))

iris %>% ggplot(aes(x = as.factor(Species), y = Petal.Length)) +
  geom_point() +
  geom_bar(data = avg_length, aes(x = Species, y = mean_len), stat = "identity", alpha = 0.4)

output

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM