ggplot 通过第二个变量改变地图填充不透明度
ggplot alter map fill opacity by second variable
我正在创建 U.S 的历史地图,其中每个县都根据 2012 年奥巴马的选票比例进行着色。我想根据人口改变县叠加层的不透明度(类似于所做的 here)。我尝试将 alpha = populationVariable 添加到我的 geom_map aes,但没有成功。
这是我当前的代码。谁能指出我正确的方向?
gg <- ggplot()
gg <- gg + geom_map(data=mapC, map=mapC, aes(x=long, y=lat, map_id=id, group=group, fill=mapC$proportionObama))
gg = gg+ scale_fill_gradient2(limits= c(0,100), name="Proportion of Votes for Obama", low="#E5000A", high="#0012BF",mid="#720964", midpoint=50)
gg = gg + theme_map() +coord_equal()
gg <- gg + geom_path(data = mapS, aes(long,lat, group=group), colour="gray50", size=.25)
gg = gg + theme(legend.position="right")
gg
我认为 alpha
需要是一个映射到 0 和 1 之间的变量。ggplot
文档总是显示小数值。
- Hue Scale - http://docs.ggplot2.org/0.9.3.1/scale_hue.html
- Color Fill Alpha - http://docs.ggplot2.org/current/aes_colour_fill_alpha.html
您没有可重现的代码,所以这是一个似乎有效的快速测试。
library(data.table)
library(ggplot2)
tmp = data.table(
'x'=c(1,2,3),
'y'=c(1,2,3),
'z'=c(.1,.5,.8)
)
p = ggplot()
p = p + geom_point( data=tmp , aes(x=x,y=y,alpha=z))
print(p)
我正在创建 U.S 的历史地图,其中每个县都根据 2012 年奥巴马的选票比例进行着色。我想根据人口改变县叠加层的不透明度(类似于所做的 here)。我尝试将 alpha = populationVariable 添加到我的 geom_map aes,但没有成功。
这是我当前的代码。谁能指出我正确的方向?
gg <- ggplot()
gg <- gg + geom_map(data=mapC, map=mapC, aes(x=long, y=lat, map_id=id, group=group, fill=mapC$proportionObama))
gg = gg+ scale_fill_gradient2(limits= c(0,100), name="Proportion of Votes for Obama", low="#E5000A", high="#0012BF",mid="#720964", midpoint=50)
gg = gg + theme_map() +coord_equal()
gg <- gg + geom_path(data = mapS, aes(long,lat, group=group), colour="gray50", size=.25)
gg = gg + theme(legend.position="right")
gg
我认为 alpha
需要是一个映射到 0 和 1 之间的变量。ggplot
文档总是显示小数值。
- Hue Scale - http://docs.ggplot2.org/0.9.3.1/scale_hue.html
- Color Fill Alpha - http://docs.ggplot2.org/current/aes_colour_fill_alpha.html
您没有可重现的代码,所以这是一个似乎有效的快速测试。
library(data.table)
library(ggplot2)
tmp = data.table(
'x'=c(1,2,3),
'y'=c(1,2,3),
'z'=c(.1,.5,.8)
)
p = ggplot()
p = p + geom_point( data=tmp , aes(x=x,y=y,alpha=z))
print(p)