ggplot2:如何分配渐变颜色图例并向地图添加阴影浮雕背景
ggplot2: how to assign a graduated color legend and add shaded relief background to a map
使用此 data.frame (you can download it from here) 和下面的代码
library(ggplot2)
library(scales)
library(ggmap)
setwd("<path to df.csv file>")
df <- read.csv("df.csv")
ggplot(df, aes(x=long, y= lat)) +
geom_polygon(aes(x=long, y= lat, group= Site1, fill = mean),
color = "black",
size = 0.1)+
scale_fill_distiller(name=bquote(atop("Mean Annual",
"Concentration" ~ (mg~L^{-1}))),
palette =
"OrRd"
,
breaks = pretty_breaks(n = 5),
direction = 1)+
facet_wrap(~year, ncol=4)+
theme_nothing(legend = TRUE)
我得到了这个情节
我想:
1) 为我的绘图添加阴影浮雕背景。
2) 创建一个渐变颜色图例,我可以在其中分配值的中断(例如 0.02-0.07、0.07-0.12、0.12、0.17..)
我可以在基本 R 中通过在图例中分配中断来做到这一点。例如
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))
但是,我不知道如何使用 ggplot2
非常感谢您的建议。
要创建渐变色图例,您需要在调用 ggplot
之前 cut
数据框中的数据,如下所示:
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)
我在你的剪辑 breaks
中添加了一个 , Inf
因为我注意到你的 2011 构面中的底部多边形没有被赋值。
然后您可以使用新的数据列进行填充:
ggplot(df, aes(x=long, y= lat)) +
geom_polygon(aes(x=long, y= lat, group= Site1, fill = mean_cut),
color = "black",
size = 0.1)
那么你需要使用一个离散的尺度,比如 scale_fill_brewer
:
scale_fill_brewer(name=bquote(atop("Mean Annual",
"Concentration" ~ (mg~L^{-1}))),
type = "seq",
palette = "OrRd",
direction = 1)
我认为您可以使用 ggmap
相当轻松地添加底图,但我不知道您的数据的位置,因此很难举出示例。
有关将底图添加到地块的教程,请参阅 https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/ggmap/ggmapCheatsheet.pdf。应该是这样的:
myMap <- get_map(location, source, maptype)
ggmap(myMap) +
geom_polygon(...) +
scale_fill_brewer(...) +
facet_wrap(...) +
theme_nothing()
使用此 data.frame (you can download it from here) 和下面的代码
library(ggplot2)
library(scales)
library(ggmap)
setwd("<path to df.csv file>")
df <- read.csv("df.csv")
ggplot(df, aes(x=long, y= lat)) +
geom_polygon(aes(x=long, y= lat, group= Site1, fill = mean),
color = "black",
size = 0.1)+
scale_fill_distiller(name=bquote(atop("Mean Annual",
"Concentration" ~ (mg~L^{-1}))),
palette =
"OrRd"
,
breaks = pretty_breaks(n = 5),
direction = 1)+
facet_wrap(~year, ncol=4)+
theme_nothing(legend = TRUE)
我得到了这个情节
我想:
1) 为我的绘图添加阴影浮雕背景。
2) 创建一个渐变颜色图例,我可以在其中分配值的中断(例如 0.02-0.07、0.07-0.12、0.12、0.17..)
我可以在基本 R 中通过在图例中分配中断来做到这一点。例如
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))
但是,我不知道如何使用 ggplot2
非常感谢您的建议。
要创建渐变色图例,您需要在调用 ggplot
之前 cut
数据框中的数据,如下所示:
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)
我在你的剪辑 breaks
中添加了一个 , Inf
因为我注意到你的 2011 构面中的底部多边形没有被赋值。
然后您可以使用新的数据列进行填充:
ggplot(df, aes(x=long, y= lat)) +
geom_polygon(aes(x=long, y= lat, group= Site1, fill = mean_cut),
color = "black",
size = 0.1)
那么你需要使用一个离散的尺度,比如 scale_fill_brewer
:
scale_fill_brewer(name=bquote(atop("Mean Annual",
"Concentration" ~ (mg~L^{-1}))),
type = "seq",
palette = "OrRd",
direction = 1)
我认为您可以使用 ggmap
相当轻松地添加底图,但我不知道您的数据的位置,因此很难举出示例。
有关将底图添加到地块的教程,请参阅 https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/ggmap/ggmapCheatsheet.pdf。应该是这样的:
myMap <- get_map(location, source, maptype)
ggmap(myMap) +
geom_polygon(...) +
scale_fill_brewer(...) +
facet_wrap(...) +
theme_nothing()