gpt4 book ai didi

r - ggplot2中缺少功能区

转载 作者:行者123 更新时间:2023-12-03 13:52:13 26 4
gpt4 key购买 nike

我似乎在ggplot2中设置要显示的功能区时遇到问题。

这是一个组合的数据集:

Estimate <-  c(100,125,150,175)
GlobalDFData <- data.frame(Estimate, Upper = Estimate + 25, Lower = Estimate - 25, Date = paste0('Q', 1:4,'_16'), Area = 'Global', stringsAsFactors = FALSE)

这是我尝试没有成功的代码。我得到了折线图,但没有上限和下限
ggplot(GlobalDFData, aes(x = Date)) + 
geom_line(aes(y = Estimate, group = Area, color = Area))+
geom_point(aes(y = Estimate, x = Date))+
geom_ribbon(aes(ymin = Lower, ymax = Upper))

最佳答案

geom_ribbon希望使用一个连续的x值,但是您可以在调用过程中提供一个x值来欺骗它。

plt <- ggplot(dat, aes(x = Date)) + 
geom_line(aes(y = Estimate, group = Area, color = Area)) +
geom_point(aes(y = Estimate, x = Date))
plt + geom_ribbon(aes(x = 1:length(Date), ymin = Lower, ymax = Upper), alpha = .2)

enter image description here
此外,您可以使用 geom_linerange,但这可能无法实现您想要的外观:
plt + geom_linerange(aes(ymin = Lower, ymax = Upper, color = Area))

enter image description here

奇怪的是,使用 geom_ribbon分配颜色可以获得相同的(有效的)结果(图未显示):
plt + geom_ribbon(aes(ymin = Lower, ymax = Upper, color = Area))

另外,您可以结合 zoo使用 scale_x_yearqtr包将季度时间转换为可以理解为连续的时间:
library(zoo)

dat$Date <- as.yearqtr(dat$Date, format = 'Q%q_%y')

ggplot(dat, aes(x = Date)) +
scale_x_yearqtr(format = 'Q%q_%y') +
geom_line(aes(y = Estimate, group = Area, color = Area))+
geom_point(aes(y = Estimate, x = Date))+
geom_ribbon(aes(ymin = Lower, ymax = Upper), alpha = 0.2)

关于r - ggplot2中缺少功能区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34553044/

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