gpt4 book ai didi

R - ggplot2 geom_area 只有一个类别可见?

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

R 新手,stackoverflow 新手,所以请原谅我....

我正在尝试使用 ggplot2 在 R 中绘制时间序列图。我想显示两个折线图,它们在给定日期的值下方填充。我一直在尝试使用 geom_area(position="identity") 函数来做到这一点。

但是,我的图表上只显示了一种颜色(尽管两者都显示在图例中)。我首先使用melt() 融化我的数据,现在正在处理三列(X=时间,变量=地下水井,值=地下水高程)。下面是我的代码的简化版本,以及我得到的截图。

Bank01MWtest<-data.frame(X=(c(1,2,2,1)),variable=(c("MW-01A","MW-01A","MW-01B","MW-01B")),value=(c(576,571,584,580)))

ggplot(data=Bank01MWtest, aes(x=X, y=value,group=variable))+geom_area(position="identity", aes(fill=variable))+geom_line(aes(color=variable))+coord_cartesian(ylim=c(570,590))

我想显示两种颜色。一种颜色低于 MW.01A 线,一种颜色低于 MW.01B 线。

Elevation Data

Updated to be reproducible

有什么帮助吗?

最佳答案

用 geom_area 试试这个,用一些合成生成的 Bank01MWtest 数据集:

head(Bank01MWtest)
Time variable value
1 2016-07-01 MW-01A 582.5482
2 2016-07-02 MW-01A 580.5652
3 2016-07-03 MW-01A 582.3305
4 2016-07-04 MW-01A 583.3122
5 2016-07-05 MW-01A 576.3432
6 2016-07-06 MW-01A 584.4086

tail(Bank01MWtest)
Time variable value
195 2016-10-03 MW-01B 573.8355
196 2016-10-04 MW-01B 575.3218
197 2016-10-05 MW-01B 570.8007
198 2016-10-06 MW-01B 572.3415
199 2016-10-07 MW-01B 575.3291
200 2016-10-08 MW-01B 578.0055

ggplot(data=Bank01MWtest, aes(x=Time, y=value,group=variable))+
geom_area(position='identity', aes(fill=variable), alpha=0.2)+
scale_x_date(date_breaks= "1 month", date_minor_breaks = "15 days", date_labels = "%b",
limits = c(min(Bank01MWtest$Time),max(Bank01MWtest$Time))) +
geom_line(aes(color=variable))+coord_cartesian(ylim=c(570,590))

enter image description here

关于R - ggplot2 geom_area 只有一个类别可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39986072/

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