ggplot地图带点:尺寸,颜色,传说,美学问题 [英] ggplot map with points: size, colour, legend, aesthetics problems

查看:129
本文介绍了ggplot地图带点:尺寸,颜色,传说,美学问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

  ggplot(legend = FALSE)+ 
geom_polygon(data = map .df,size = 0.1,aes(long,lat,group = group,color =grey80,fill = ind_vul))+
scale_fill_gradient2(low =blue,high =red,midpoint = mean (map.df $ ind_vul,na.rm = TRUE))+
geom_point(data = puntos.df,aes(as.numeric(long),as.numeric(lat),color = geografico),size = 2)+
facet_wrap(〜municipio,scales =free)+
tema.mapas

我发现了几个问题(显然与我对此主题的无知有关),问题如下(所有相关的,请参见 ggplot抛出奇怪的环境中的错误):


  • geom_polygon 中的size参数奇怪地工作,在 aes 之外没有任何效果,并且在它内部被忽略,如果没有 color 参数


  • 忽略 color 参数(同样在 geom_polygon )在上面的代码中说 color =grey80但多边形的轮廓线是另一种颜色,并且出现grey80作为传说中的要点!

  • 由于 color ='grey80' size = 0.1 不会要求您的 map.df 它们应该在 aes 调用之外。换句话说,当 color 位于 aes 内时,ggplot会认为你正在告诉它基于图的颜色在你的数据的某一列上,或者如果你在这里给出了一个文字字符串,在该字符串和通常的调色板中的某个条目之间创建一个映射。在某些情况下,将美学映射到文字字符串可能是一种有用的技术,但与将美学设置为价值不同。在 aes 之外,颜色会查找名为grey80的颜色,并使用该特定颜色。



    [改编自评论并稍微扩大]

    I have this piece of code:

    ggplot(legend = FALSE) + 
    geom_polygon(data = map.df, size = 0.1, aes(long, lat, group = group, colour = "grey80", fill = ind_vul)) + 
    scale_fill_gradient2(low = "blue", high = "red", midpoint = mean(map.df$ind_vul, na.rm = TRUE)) + 
    geom_point(data = puntos.df, aes(as.numeric(long), as.numeric(lat), colour = geografico), size = 2) +
    facet_wrap(~municipio, scales = "free") + 
    tema.mapas   
    

    And I found several problems (obviously related to my ignorance of this topic), the problems are the following (all related, see ggplot throwing an error in weird circumstances):

    • The size parameter in geom_polygon is working oddly, outside of the aes has no effect and inside of it is ignored, if there is no a colour parameter.

    • The colour parameter is ignored (again in geom_polygon) in the code above saids colour="grey80" but the contour line of the polygon is in another colour and the "grey80" appears as legend of the points!

    解决方案

    Since colour='grey80' and size=0.1 are not asking for a column of your map.df they should be outside of the aes call. In other words, when colour is inside aes, ggplot thinks you're telling it to base the color of the plot on some column in your data, or if given a literal string like you have here, to create a mapping between that string and some entry in the usual color palette. Mapping an aesthetic to a literal string can be a useful technique in some cases, but is different than setting the aesthetic to a value. Outside of aes, color will look for a color that is named "grey80" and will use that specific color.

    [adapted from comments and expanded slightly]

    这篇关于ggplot地图带点:尺寸,颜色,传说,美学问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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