gpt4 book ai didi

r - 加权ggplot2警告: Ignoring unknown aesthetics: weight

转载 作者:行者123 更新时间:2023-12-03 09:05:45 32 4
gpt4 key购买 nike

我尝试使用 ggplot2 绘制加权密度。结果似乎不错,但我收到以下警告:警告:忽略未知的美学:重量。类似的问题似乎出现在other ggplot2 applications中因此我想知道是否可以忽略该警告。

可重现的示例:

library(ggplot2)

set.seed(123)

# Some random data & weights
x <- rnorm(1000, 5)
w <- x^5

# Plot unweighted
ggplot() + stat_density(aes(x = x))

# Plot weighted - Warning: Ignoring unknown aesthetics: weight
ggplot() + stat_density(aes(x = x, weight = w / sum(w))) # Weighting seems to work fine

# Comparison of weighted density in base graphics - Same results as with ggplot2
plot(density(x, weights = w / sum(w)))

可以忽略此警告消息吗?

最佳答案

您可以使用geom_密度来避免警告:

ggplot() + 
geom_density(aes(x = x, weight = w / sum(w)), color = "green") +
geom_density(aes(x = x), color = "blue")

resulting plot

我本来期望 stat_ 函数能够处理与 geom 相同的美感,而且它似乎确实这样做了。该警告将是一个错误,应向维护人员报告。

关于r - 加权ggplot2警告: Ignoring unknown aesthetics: weight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47592205/

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