gpt4 book ai didi

r - 将均值添加到 ggplot 中的直方图

转载 作者:行者123 更新时间:2023-12-04 10:26:04 24 4
gpt4 key购买 nike

我在 nycflights13 包中为航类数据框的距离变量创建了一个直方图。

现在我正在尝试向标记均值的直方图添加一条垂直线,如下所示:

library(nycflights13)
attach(flights)
ggplot(flights, aes(x = distance)) +
geom_histogram(aes(y = ..density..),
color = "darkblue",
fill = "lightblue",
binwidth = 250) +
labs(x = "Departure delay (minutes)") +
geom_vline(xintercept = mean(distance, color = black, size = 1.5)) +
geom_density(color = "red", size = 1)

这给了我输出:
Warning messages:
1: In mean.default(distance, color = black, size = 1.5) :
argument is not numeric or logical: returning NA
2: Removed 1 rows containing missing values (geom_vline).

我该如何解决?我尝试删除 NA 值,但我想不是以正确的方式。

最佳答案

您需要输入 mean作为标量。因此我们可以使用 $这里。将颜色作为字符串输入 "black" .

library(nycflights13)
# attach(flights) # I strongly recommend not to use attach
library(ggplot2)
ggplot(flights, aes(x = distance)) +
geom_histogram(aes(y = ..density..),
color = "darkblue",
fill = "lightblue",
binwidth = 250) +
labs(x = "Departure delay (minutes)") +
geom_vline(xintercept = mean(flights$distance),
color="black", size=1.5) +
geom_density(color = "red", size = 1)

enter image description here

关于r - 将均值添加到 ggplot 中的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60632765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com