在ggplot中,如何获得stat_bin2d的两个图例(“渐变"类型)? [英] In ggplot, how can I get two legends ("gradient" type) for stat_bin2d?
本文介绍了在ggplot中,如何获得stat_bin2d的两个图例(“渐变"类型)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我这里有两个集群",只有一个传说.
Here I have two 'clusters', and just one legend.
如何获得具有两个不同颜色渐变的两个密度"图例?
How can I get two "density" legends with two different color gradients?
我已经尝试过group
,但是它不起作用.
I have tried group
but it does not work.
以下代码生成了上图:
library(ggplot2)
df <- data.frame(x=c(rnorm(1000,1,.1),rnorm(1000,3,.1)),
y=c(rnorm(1000,1,1),rnorm(1000,3,1)),
type=c(rep('a',1000),rep('b',1000)))
plot( ggplot(df) +
stat_bin2d(aes(x,y,fill=..density..,group='type')))
推荐答案
我不知道一种指定多个填充渐变的方法.但这是一种变通方法,它使用不同的透明度级别来模拟渐变,从而使填充可用于以下类型的映射:
I'm not aware of a way to specify more than one fill gradient. But here's a work around that uses different transparency levels to simulate the gradient, leaving fill available to be mapped with type:
ggplot(df, aes(x, y, fill = type)) +
stat_bin2d(aes(alpha = ..density..)) +
scale_alpha(range = c(1, 0.1)) +
theme_bw()
这篇关于在ggplot中,如何获得stat_bin2d的两个图例(“渐变"类型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文