gpt4 book ai didi

r - 为 geom_area() 定义不同于零的起始值

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

我想将 geom_area() 的起始 y 级别从零移开。这是在下面的例子中,第一个函数调用没问题,但我怎样才能纠正第二个调用的情节:

library(ggplot2)

set.seed(123)

myplot <- function (split.value) {
year <- (2000:2017)
index <- rnorm(18, split.value,5)
interp <- approx(year, index, n=1000)
df <- data.frame(year = interp$x,
index = interp$y,
valence = ifelse(interp$y <= split.value, "pos", "neg"),
stringsAsFactors = FALSE)
ggplot(df, aes(x = year, y = index)) +
geom_area(aes(fill = valence), alpha = 0.5) +
geom_line() +
geom_hline(yintercept = split.value) +
scale_fill_manual(values=c("green", "red"), guide=FALSE) +
scale_x_continuous(expand=c(0, 0))
} # cf. R Graphics Cookbook

myplot(0) # o.k. (with more interpolation points)
# but the following does not work:

myplot(2) # .. would like to shift green-area-MIN-y-value to 2
# and red-area-MAX-y to 2

最佳答案

geom_areageom_ribbon 的特例修复 ymin到零。所以下面的作品而不是你的 geom_area

geom_ribbon(aes(ymin=split.value, ymax=index, fill = valence), alpha = 0.5) +

关于r - 为 geom_area() 定义不同于零的起始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45544559/

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