透明度和Alpha级别ggplot2 stat_density2d与R中的地图和图层 [英] Transparency and Alpha levels for ggplot2 stat_density2d with maps and layers in R

查看:828
本文介绍了透明度和Alpha级别ggplot2 stat_density2d与R中的地图和图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以提供建议,以处理透明度和多层次的alpha级别的最佳方式(参考 ggplot2 )?在下面的示例代码中,很难看到单个状态边界线(例如南卡罗来纳州,北卡罗来纳州,弗吉尼亚州) - 我尝试过设置 alpha = 0.00001 ,但会出现更糟糕的是...

pre $ library $ g
$ b $ all_states = map_data(state )
set.seed(103)
df = data.frame(long = sample(-90:-70,100,T),lat = sample(30:45,100,T))
p = ggplot()+
geom_polygon(data = all_states,aes(x = long,y = lat,group = group),color =white,fill =grey80)+
stat_density2d(data = df,aes(x = long,y = lat,fill = .. level ..,alpha = .. level ..),#HELP HERE
size = 2,bins = 5,geom ='多边形')+
geom_point(data = df,aes(x = long,y = lat),
color =coral1,position = position_jitter(w = 0.4,h = 0.4) 0.8)+
theme_bw()
p


解决方案

ae内部映射一些变量到 alpha = s()那么默认情况下,alpha值的范围是从0.1到1(最低配对变量值为0.1,最高值为1)。您可以使用 scale_alpha_continuous()更改它,并设置不同的最大和最小范围值。

  ggplot()+ 
geom_polygon(data = all_states,aes(x = long,y = lat,group = group),
color =white,fill =grey80)+
stat_density2d(data = df,aes(x = long,y = lat,fill = .. level ..,alpha = .. level ..),
size = 2,bins = 5,geom ='polygon')+
geom_point(data = df,aes(x = long,y = lat),
color =coral1,position = position_jitter(w = 0.4,h = 0.4), alpha = 0.8)+
theme_bw()+
scale_alpha_continuous(range = c(0.1,0.5))


Can anyone offer suggestions as to the best way to deal with transparency and alpha levels with many layers (with reference to ggplot2)? In the sample code below, it becomes very difficult to see individual state borderlines (e.g. South Carolina, North Carolina, Virginia) -- I have tried setting alpha=0.00001 but this appears to make things worse...

library(ggplot2)

all_states = map_data("state")
set.seed(103)
df = data.frame(long=sample(-90:-70,100,T), lat=sample(30:45,100,T))
p = ggplot() + 
    geom_polygon(data=all_states, aes(x=long, y=lat, group=group), color="white", fill="grey80") + 
    stat_density2d(data=df, aes(x=long, y=lat, fill=..level.., alpha=..level..), # HELP HERE ???
                   size=2, bins=5, geom='polygon') +
    geom_point(data=df, aes(x=long, y=lat), 
               color="coral1", position=position_jitter(w=0.4,h=0.4), alpha=0.8) + 
    theme_bw()
p

解决方案

When you map some variable to alpha= inside the aes() then by default alpha values range from 0.1 to 1 (0.1 for lowest maped variable values and 1 for highest values). You can change it with scale_alpha_continuous() and setting different maximal and minimal range values.

ggplot() + 
  geom_polygon(data=all_states, aes(x=long, y=lat, group=group), 
               color="white", fill="grey80") + 
  stat_density2d(data=df, aes(x=long, y=lat, fill=..level.., alpha=..level..), 
                 size=2, bins=5, geom='polygon') +
  geom_point(data=df, aes(x=long, y=lat), 
             color="coral1", position=position_jitter(w=0.4,h=0.4), alpha=0.8) + 
  theme_bw()+
  scale_alpha_continuous(range=c(0.1,0.5))

这篇关于透明度和Alpha级别ggplot2 stat_density2d与R中的地图和图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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