如何使用ggplot2设置图例alpha [英] How to set legend alpha with ggplot2

查看:505
本文介绍了如何使用ggplot2设置图例alpha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相对于方向的风速图,它有很大的点数,所以除了颜色=月份之外,我还使用alpha = I(1/20)

以下是一段代码示例:

  library(RMySQL)
library(ggplot2)
con< -dbConnect(...)
wind < - dbGetQuery(con,SELECT speed_w / speed_e AS ratio,dir_58 as dir,MONTHNAME(timestamp)AS month,ROUND((speed_w + speed_e)/ 2 )AS速度FROM表名;);

png(ratio-by-speed.png,height = 400,width = 1200)
qplot(wind $ dir,wind $ ratio,ylim = c(0.5,1.5) ,xlim = c(0,360),color = wind $ month,alpha = I(1/30),main =West / East against direction)
dev.off()

这会产生一个像样的图,但是我的问题是图例的alpha也是1/30,这使得它不可读。有没有一种方法可以强制图例为1 alpha?

下面是一个例子:

解决方案

更新随着版本0.9.0,现在可以在指南 override.aes 覆盖图例中的美学值>功能。所以,如果你添加这样的东西到你的情节:

  + guides(color = guide_legend(override.aes = list(alpha = 1)))

应该这样做。



b
$ b

我通过使用数据的空子集并使用该调用中的图例对geom进行重复调用来解决此问题。不幸的是,如果数据帧实际上是空的(例如,从 subset(菱形,FALSE)),它不起作用,因为ggplot2似乎将这种情况视为同样它将 NULL 代替数据框。但是我们可以通过在一个绘图维度上只有一行的子集并将其设置为 NaN 来获得相同的效果,这将防止它被绘制出来。



以Chase为例:

 #Alpha参数淘汰了图例:
gp < - ggplot()+ geom_point(data = diamonds,aes(depth,price,color = clarity),alpha = 0.1)
print(gp)

#Full颜色图例:
dummyData< - diamonds [1,]
dummyData $ price< - NaN
#dummyData< - subset(diamonds,FALSE)#这会更好,但它不会不行!
gp < - ggplot()+
geom_point(data = diamonds,aes(depth,price,color = clarity),alpha = 0.1,legend = FALSE)+
geom_point(data = dummyData,aes(深度,价格,颜色=清晰度),alpha = 1.0,na.rm = TRUE)
print(gp)


I have a graph of wind speeds against direction which has a huge numeber of points, and so am using alpha=I(1/20) in addition to color=month

Here is a sample of code:

library(RMySQL)
library(ggplot2)
con <- dbConnect(...)
wind <- dbGetQuery(con, "SELECT speed_w/speed_e AS ratio, dir_58 as dir, MONTHNAME(timestamp) AS month, ROUND((speed_w+speed_e)/2) AS speed FROM tablename;");

png("ratio-by-speed.png",height=400,width=1200)
qplot(wind$dir,wind$ratio,ylim=c(0.5,1.5),xlim=c(0,360),color=wind$month,alpha=I(1/30),main="West/East against direction")
dev.off()

This produces a decent graph, however my issue is that the alpha of the legend is 1/30th also, which makes it unreadable. Is there a way I can force the legend to be 1 alpha instead?

Here is an example:

解决方案

Update With the release of version 0.9.0, one can now override aesthetic values in the legend using override.aes in the guides function. So if you add something like this to your plot:

+ guides(colour = guide_legend(override.aes = list(alpha = 1)))

that should do it.


I've gotten around this by doing a duplicate call to the geom using an empty subset of the data and using the legend from that call. Unfortunately, it doesn't work if the data frame is actually empty (e.g. as you'd get from subset(diamonds,FALSE)) since ggplot2 seems to treat this case the same as it treats NULL in place of a data frame. But we can get the same effect by taking a subset with only one row and setting it to NaN on one of the plot dimensions, which will prevent it from getting plotted.

Based off Chase's example:

# Alpha parameter washes out legend:
gp <- ggplot() + geom_point(data=diamonds, aes(depth, price, colour=clarity), alpha=0.1)
print(gp)

# Full color legend:
dummyData <- diamonds[1, ]
dummyData$price <- NaN
#dummyData <- subset(diamonds, FALSE)   # this would be nicer but it doesn't work!
gp <- ggplot() +
  geom_point(data=diamonds, aes(depth, price, colour=clarity), alpha=0.1, legend=FALSE) + 
  geom_point(data=dummyData, aes(depth, price, colour=clarity), alpha=1.0, na.rm=TRUE)
print(gp)

这篇关于如何使用ggplot2设置图例alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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