gpt4 book ai didi

r - ggplot2 正/负图无法清晰呈现

转载 作者:行者123 更新时间:2023-12-02 04:44:02 25 4
gpt4 key购买 nike

我正在尝试复制顶部底部图,如示例 here但它没有正确渲染(紫色系列有 +ve 和 -ve 值,绿色为负值)留下杂乱的人工制品。我也在努力创建一个玩具示例来复制这个问题,所以我希望尽管我缺乏数据,但有人可以提供帮助

我的代码是:

negatives <- result[result$value < 0,]
positives <- result[result$value >= 0,]

ggplot() +
geom_area(data = negatives, aes(x = datetime, y = value, fill=variable)) +
geom_area(data = positives, aes(x = datetime, y = value, fill=variable))

我的数据结构是:

dput(droplevels(head(result)))

structure(
list(
datetime = structure(c(1421751900, 1421751900, 1421752200, 1421752200, 1421752500, 1421752500), class = c("POSIXct", "POSIXt"), tzone = ""),
variable = structure(c(1L, 2L, 1L, 2L, 1L, 2L), .Label = c("V-S-MNSP1", "VIC1-NSW1"), class = "factor"),
value = c(9, 80.106180098, 9, 77.719632578, 9, 84.158868934 )
),
.Names = c("datetime", "variable", "value"),
row.names = c(NA, 6L),
class = "data.frame")

top/bottom plot

最佳答案

我想通了,我的数据点可以是正面的也可以是负面的。如果我没有每个日期时间的每个变量的值,那么堆叠似乎不起作用。所以不要这样做

negatives <- result[result$value < 0,]
positives <- result[result$value >= 0,]

我这样做

result$positive <- ifelse(result$value >= 0,result$value,0)
result$negative <- ifelse(result$value < 0,result$value,0)

ggplot(data = result) +
geom_area(aes(x = datetime, y = positive, fill=variable), position = 'stack') +
geom_area(aes(x = datetime, y = negative, fill=variable), position = 'stack')

然后我的图表正确呈现

戴夫

关于r - ggplot2 正/负图无法清晰呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34913981/

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