R - ggplot2 geom_area只有一个类别可见? [英] R - ggplot2 geom_area only one category visible?

查看:379
本文介绍了R - ggplot2 geom_area只有一个类别可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图在R中使用ggplot2做一个时间序列图。我想要显示两个折线图,这些折线图在特定日期的价值下面填充。我一直试图用geom_area(position =identity)函数来做到这一点。

但是,我的图表上只显示一种颜色(尽管两者都显示在图例中)。我开始通过融化()熔化我的数据,并且现在使用三列(X =时间,变量=地下水井,值=地下水位升高)工作。下面是我的代码的简化版本,以及我得到的截图。

  Bank01MWtest <-data.frame(X =(c(1,2,2,1)),variable =(c (MW-01A,MW-01A,MW-01B,MW-01B)),值=(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线的一种颜色。







任何帮助?

解决方案

我相信 geom_area 正在被替换由 geom_ribbon ggplot2 中,所以我将在我的解决方案中使用后者。您还需要将您的数据从长到宽重组为这个解决方案,为每个图例类别提供自己的专栏。我将使用 reshape2 包中的 dcast 函数执行此操作。



这里的想法是用不同的 ymax 变量添加图层,用 fill 选项分配图例标签,然后使用 scale_fill_manual 函数添加带有颜色的图例。

  library(ggplot2)
library(reshape2)

Bank01MWtest< -data.frame(X = sample(c(1,1,2,2)),
variable = sample (c(MW01A,MW01A,MW01B,MW01B)),
值=样本(c(576,571,584,580)))

###您的类别标签通过删除 - 符号
###,以便它们可以用作下面的变量名称。
$ b $ dat = dcast(Bank01MWtest,X〜variable)

ggplot(data = dat,aes(x = X))+
geom_ribbon(aes(ymin = 0,ymax = MW01A,fill =MW01A))+
geom_ribbon(aes(ymin = 0,ymax = MW01B,fill =MW01B))+
scale_fill_manual(,values = c (绿色,蓝色))+
coord_cartesian(ylim = c(570,590))


New to R, new to stackoverflow, so forgive me....

I'm trying to do a timeseries plot in R using ggplot2. I want to show two line graphs which are filled below their value for a given date. I've been trying to do this with the geom_area(position="identity") function.

However, only one color shows up on my graph (though both show in the legend). I started by melting my data using melt() and am now working with three columns (X=time, variable=groundwater well, value=groundwater elevation). Below is a simplified version of my code, and a screenshot at what I get.

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))

I want to show two colors. One color below MW.01A line and one below MW.01B line.

Any help?

解决方案

I believe geom_area is being replaced by geom_ribbon in ggplot2, so I'll use the latter in my solution. You'll also need to restructure your data from long to wide for this solution, giving each of the legend categories their own column. I'll do this with the dcast function within the reshape2 package.

The idea here is to add layers with different ymax variables, assign legend labels with the fill option, and then add a legend with colors using the scale_fill_manual function.

library(ggplot2)
library(reshape2)

Bank01MWtest<-data.frame(X=sample(c(1,1,2,2)),
                     variable=sample(c("MW01A","MW01A","MW01B","MW01B")),
                     value=sample(c(576,571,584,580)))

### Note above I modified your category labels by getting rid of the "-" sign 
### so that they can be used as variable names below.

dat = dcast(Bank01MWtest, X~variable)

ggplot(data=dat, aes(x=X)) + 
  geom_ribbon(aes(ymin=0, ymax=MW01A, fill="MW01A")) + 
  geom_ribbon(aes(ymin=0, ymax=MW01B, fill="MW01B")) +
  scale_fill_manual("", values=c("green", "blue")) +
  coord_cartesian(ylim=c(570,590))

这篇关于R - ggplot2 geom_area只有一个类别可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆