R / ggplot2:如何在重叠区域图中匹配图例和绘制颜色? [英] R/ggplot2: how to match legend and plot colors in overlapping area plots?

查看:572
本文介绍了R / ggplot2:如何在重叠区域图中匹配图例和绘制颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个面积图(称为蓝色和绿色),其中绿色大部分在蓝色 plot,但是在很少的点上,它高于 blue 图。我想使用透明度 alpha = 0.2 两者,也可以指定每个的颜色。我现在的问题是,由于绿色图大多在蓝色图下,混合颜色蓝色 + 绿色 =某些其他颜色, 绿色。然而,图例当然显示映射到蓝色的蓝色图和绿色到绿色。问题是,当有人看着图表时,他们会感到困惑,因为绿色图表大多看起来不是绿色的(因为它大部分时间与蓝色重叠)。

这是我的代码(我真正的应用程序的超级简化版本)。

  df  df.m<  -  melt(df,id = date',variable_name ='type')
df.m $ type < - ordered(df.m $ type,c('green','blue'))

ggplot df.m,aes(date,value))+
geom_area(aes(fill = type),position ='identity',alpha = 0.2)+
scale_fill_manual(values = c 'blue'))



如你所见,绿色地块的真绿色区域非常小,图例颜色绿色不符合大多数绿色图。这当然是设计的正确行为,但我想知道是否有一些方法我可以得到传说颜色为绿色,和重叠蓝色+绿色颜色进行匹配。我做的一件事是将 blue 变量转换为 blue - green ,然后使区域图堆叠 position =stack)。这几乎可行,但不太令人满意,因为在新的 blue 变量的负数的日期的颜色看起来很奇怪。



是否有其他方法获取绿色的图例颜色和重叠的蓝色/绿色区域匹配?也许是一种直接指定图例颜色的方法?任何帮助赞赏!

解决方案

我不认为在图例中定义自定义颜色是可能的,只需调用ggplot2,你坚持指定颜色,你可以绘制一个没有图例的 opts(legend.position =none),并附加一个单独的图例。使用网格视口。软件包 gridExtra 也可能是后一任务的重要资源。


I have two area plots (call them 'blue' and 'green') where the green is mostly under the blue plot, but on a very few points, it is higher than the blue plot. I want to use transparency say alpha = 0.2 for both, and also be able to specify the colors for each. My problem now is that since the green plot is mostly under the blue plot, its area mostly has the blended color blue + green = some other color, and in only a few places shows its "true" green color. However the legend of course shows the blue plot mapped to blue and the green plot mapped to green. Problem is, when someone looks at the graph, they will be confused since the green plot mostly looks non-green (because it's overlapping with blue most of the time).

Here's my code (super simplified version of my real application).

df <- data.frame( date = 1:5, blue = 10, green = c(1,5,11,5,1))
df.m <- melt( df, id = 'date', variable_name = 'type' )
df.m$type <- ordered( df.m$type, c('green', 'blue'))

ggplot(df.m, aes(date,value)) +
  geom_area( aes(fill = type),  position = 'identity', alpha = 0.2) +
  scale_fill_manual ( values = c('green', 'blue') )

As you can see, the "true green" area of the green plot is very small, so the legend color for green doesn't really match the majority of the green plot. This is of course the right behavior by design, but I am wondering if there is some way I can get the legend colour for green, and the overlapping blue+green color to match up. One thing I did try is to transform the blue variable to blue - green, and then make the area plots stacked (with position = "stack"). This almost works but isn't quite satisfactory because the color on the dates where the new blue variable is negative looks strange.

Are there other ways to get the legend color for green and the color of the "overlapping blue/green" area to match up? Maybe a way to directly specify the legend color? Any help appreciated!

解决方案

I do not think defining custom colors in the legend is possible with a simple call in ggplot2, but if you do insist on specifying the colors, you might draw the plot without a legend by opts(legend.position = "none") and attach a separate legend by eg. using grid viewport. Package gridExtra could also be a great resource for the latter task.

这篇关于R / ggplot2:如何在重叠区域图中匹配图例和绘制颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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