在ggplot中指定地图的色阶 [英] Specifying the colour scale for maps in ggplot

查看:427
本文介绍了在ggplot中指定地图的色阶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法为ggplot中的地图设置颜色比例。我需要灰度。非常感谢一个想法,我错了。我还想知道是否有一种更有效的方法将颜色变量放入ggplot中(即,将其附加到'强化'数据中)?

Having difficulty setting the colour scales for maps in ggplot. I need greyscale. Very grateful for an idea where I'm going wrong. I also wonder if there is a more efficient way of getting the colour variable into ggplot (i.e. than by attaching it to 'fortified' data)?

library(ggplot2)
states <- map_data("state")
var <- data.frame(table(states$region)) # using rows as a dummy variable
states$variable <- var$Freq[match(states$region,var$Var1)]

map <- ggplot(states, aes(x=long, y=lat)) + 
  geom_polygon(aes(group=group, fill=variable), col=NA,lwd=0)

map + scale_colour_gradient(low='white', high='grey20')
map + scale_colour_grey()

推荐答案

您需要使用 scale_fill _ * 而不是 scale_color _ * 。对于多边形几何体,多边形的填充颜色与 fill 唯美性,而不是颜色美学。通常,用于改变特定比例的细节的函数是 scale_ aesthetic_name _ type_of_scale,例如, scale_fill_gradient

You need to use scale_fill_* and not scale_color_*. For the polygon geometry the fill color of the polygons is coupled to the fill aesthetic, not to the color aesthetic. In general, the function used to change the details of a particular scale is scale_aesthetic_name_type_of_scale, e.g. scale_fill_gradient.

这篇关于在ggplot中指定地图的色阶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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