ggplot2:如何指定渐变颜色图例并将阴影浮雕背景添加到地图中

时间:2016-05-24 04:44:18

标签: r ggplot2 maps legend

使用此data.frame (you can download it from here)和以下代码

q <- cut(df$ParameterA, breaks=c(0, 0.02, 0.20, 0.07, 0.12, 0.17, 0.22, 0.25)), include.lowest=T, labels=labs)
legend(legend = c("<0.02", "0.02-0.07", "0.07-0.12", "0.12-0.17", "0.17-0.22", ">0.22"), fill = levels(q))

我得到了这个情节

enter image description here

我想:

1)在我的情节中添加阴影浮雕背景。

2)创建一个渐变的颜色图例,我可以在其中指定值的中断(例如0.02-0.07,0.07-0.12,0.12,0.17 ......)

我可以通过在图例中指定中断来在基本R中执行此操作。例如

ggplot2

但是,我不知道如何使用yes | execute_command

执行此操作

您的建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

要创建渐变颜色图例,您需要在调用cut之前ggplot数据框中的df$mean_cut <- cut(df$mean, c(0, 0.02, 0.20, 0.07, 0.12, 0.17, 0.22, 0.25), include.lowest = TRUE) 数据,如下所示:

, Inf

我在剪切breaks中添加了ggplot(df, aes(x=long, y= lat)) + geom_polygon(aes(x=long, y= lat, group= Site1, fill = mean_cut), color = "black", size = 0.1) ,因为我注意到2011年方面的底部多边形没有分配值。

然后您可以使用新数据栏填写:

scale_fill_brewer

然后您需要使用像 scale_fill_brewer(name=bquote(atop("Mean Annual", "Concentration" ~ (mg~L^{-1}))), type = "seq", palette = "OrRd", direction = 1) 这样的离散比例:

ggmap

我认为您可以使用myMap <- get_map(location, source, maptype) ggmap(myMap) + geom_polygon(...) + scale_fill_brewer(...) + facet_wrap(...) + theme_nothing() 轻松添加底图,但我不知道您的数据的位置,因此很难举例说明。

有关向底图添加底图的教程,请参阅https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/ggmap/ggmapCheatsheet.pdf。这将是:

data.replace( /\bABC\b/g , function(matched) {
    console.log(matched);
})
相关问题