如何使用 ggplot2 设置图例 alpha
我有一个风速与方向的图表,其中有大量的点,因此除了 color=month 之外,我还使用 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 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()
这会产生一个不错的图表,但是我的问题是图例的 Alpha 也是 1/30,这使得它不可读。有没有办法可以强制图例为 1 alpha?
这是一个例子:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新 随着版本 0.9.0 的发布,现在可以使用
guides
函数中的override.aes
覆盖图例中的美学值。所以如果你在你的图中添加这样的东西:那就应该可以了。
我通过使用数据的空子集并使用该调用中的图例对 geom 进行重复调用来解决此问题。不幸的是,如果数据框实际上是空的(例如,您从
subset(diamonds,FALSE)
获得的数据),它就不起作用,因为 ggplot2 似乎对待这种情况与对待相同。 >NULL
代替数据框。但是,我们可以通过获取只有一行的子集并将其在绘图维度之一上设置为 NaN 来获得相同的效果,这将阻止它被绘制。基于蔡斯的例子:
Update With the release of version 0.9.0, one can now override aesthetic values in the legend using
override.aes
in theguides
function. So if you add something like this to your plot: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 treatsNULL
in place of a data frame. But we can get the same effect by taking a subset with only one row and setting it toNaN
on one of the plot dimensions, which will prevent it from getting plotted.Based off Chase's example:
谷歌搜索了一下这篇文章,似乎并没有表明 ggplot 目前支持此选项。其他人通过使用 gridExtra 并使用 viewPorts 解决了相关问题此处讨论 。
我不是那么老练,但这里有一种方法应该可以给你带来想要的结果。该方法是绘制几何图形两次,一次没有 alpha 参数,并且位于真实绘图区域之外。第二个几何图形将包含 alpha 参数并抑制图例。然后我们将使用 xlim 和 ylim 指定绘图区域。鉴于您有很多点,这大约会使绘图时间增加一倍,但应该会给您带来您想要的效果。
使用钻石数据集:
A bit of googling turned up this post which doesn't seem to indicate that ggplot currently supports this option. Others have addressed related problems by using gridExtra and using viewPorts as discussed here.
I'm not that sophisticated, but here's one approach that should give you the desired results. The approach is to plot the geom twice, once without an alpha parameter and outside of the real plotting area. The second geom will include the alpha parameter and suppress the legend. We will then specify the plotting region with xlim and ylim. Given that you are a lot of points, this will roughly double the plotting time, but should give you the effect you are after.
Using the diamonds dataset: