如何将州和县线添加到 R 中的 geom_raster ggplot?
How do I add state and county lines to a geom_raster ggplot in R?
我在南加州有一个数据集(纬度、经度、值),我可以使用 ggplot(见下文)绘制它。纬度范围从 31.5 到 35.5,经度范围从 -121 到 -113。 ggplot 地图看起来不错,但我想将州和县的边界线添加到我的地图中。
我尝试添加 geom_polygon() 但我一直在获取整个美国西南部的边界,而不是仅在我的地块区域(南加州)。
我想知道是否有人 suggestions/ideas 知道如何做到这一点。非常感谢。
"df is a 3D dataframe (Lat,Lon,value) over S. California"
"lat ranges from 31 to 35"
"lon ranges from -121 to -113"
"value ranges from 3000 to 5000"
library(maps)
library(fields)
library(ggplot2)
state_map <- map_data("state")
bmap=map_data("state", xlim=c(-121,-113), ylim=c(31.5,35.5))
p1 = ggplot() + geom_raster(data = df, aes(x=Lon, y = Lat, fill=Value)) +
coord_fixed(ratio = 1) +
geom_polygon(data=bmap,aes(x=long,y=lat,group=group), inherit.aes=F,
colour='black', fill=NA) +
scale_fill_gradientn(na.value="white",limits = c(3500,5000),
colours=c("yellow","orange","green","blue")) +
theme(panel.background = element_rect(fill = 'white',color="black"),
legend.key = element_blank())
print(p1)
我正在为整个西南地区设置边界,但我只想为我的数据区域设置边界:
您可以使用 coord_cartesian
缩放地图:
library(maps)
library(fields)
library(ggplot2)
state_map <- map_data("state")
bmap=map_data("state", xlim=c(-121,-113), ylim=c(31.5,35.5))
# Create the df dataset with random numbers for Values
n <- 80
df <- expand.grid(seq(31,35,length.out=n),
seq(-121,-113,length.out=n))
df$Vaule <- 3000+2000*runif(nrow(df))
names(df) <- c("Lat","Lon","Value")
p1 = ggplot() +
coord_fixed(ratio = 1) +
geom_raster(data = df, aes(x=Lon, y = Lat, fill=Value), alpha=0.2) +
geom_polygon(data=bmap,aes(x=long,y=lat,group=group), inherit.aes=F,
colour='black', fill=NA, lwd=1) +
scale_fill_gradientn(na.value="white",limits = c(3500,5000),
colours=c("yellow","orange","green","blue")) +
scale_y_continuous(expand = c(0,0)) +
scale_x_continuous(expand = c(0,0)) +
theme(panel.background = element_rect(fill = 'white',color="black"),
legend.key = element_blank()) +
coord_cartesian(xlim=c(-121, -113), ylim=c(31,35)) +
theme(panel.border = element_rect(colour = "gray50", fill=NA, size=1))
print(p1)
我在南加州有一个数据集(纬度、经度、值),我可以使用 ggplot(见下文)绘制它。纬度范围从 31.5 到 35.5,经度范围从 -121 到 -113。 ggplot 地图看起来不错,但我想将州和县的边界线添加到我的地图中。
我尝试添加 geom_polygon() 但我一直在获取整个美国西南部的边界,而不是仅在我的地块区域(南加州)。
我想知道是否有人 suggestions/ideas 知道如何做到这一点。非常感谢。
"df is a 3D dataframe (Lat,Lon,value) over S. California" "lat ranges from 31 to 35" "lon ranges from -121 to -113" "value ranges from 3000 to 5000"
library(maps)
library(fields)
library(ggplot2)
state_map <- map_data("state")
bmap=map_data("state", xlim=c(-121,-113), ylim=c(31.5,35.5))
p1 = ggplot() + geom_raster(data = df, aes(x=Lon, y = Lat, fill=Value)) +
coord_fixed(ratio = 1) +
geom_polygon(data=bmap,aes(x=long,y=lat,group=group), inherit.aes=F,
colour='black', fill=NA) +
scale_fill_gradientn(na.value="white",limits = c(3500,5000),
colours=c("yellow","orange","green","blue")) +
theme(panel.background = element_rect(fill = 'white',color="black"),
legend.key = element_blank())
print(p1)
我正在为整个西南地区设置边界,但我只想为我的数据区域设置边界:
您可以使用 coord_cartesian
缩放地图:
library(maps)
library(fields)
library(ggplot2)
state_map <- map_data("state")
bmap=map_data("state", xlim=c(-121,-113), ylim=c(31.5,35.5))
# Create the df dataset with random numbers for Values
n <- 80
df <- expand.grid(seq(31,35,length.out=n),
seq(-121,-113,length.out=n))
df$Vaule <- 3000+2000*runif(nrow(df))
names(df) <- c("Lat","Lon","Value")
p1 = ggplot() +
coord_fixed(ratio = 1) +
geom_raster(data = df, aes(x=Lon, y = Lat, fill=Value), alpha=0.2) +
geom_polygon(data=bmap,aes(x=long,y=lat,group=group), inherit.aes=F,
colour='black', fill=NA, lwd=1) +
scale_fill_gradientn(na.value="white",limits = c(3500,5000),
colours=c("yellow","orange","green","blue")) +
scale_y_continuous(expand = c(0,0)) +
scale_x_continuous(expand = c(0,0)) +
theme(panel.background = element_rect(fill = 'white',color="black"),
legend.key = element_blank()) +
coord_cartesian(xlim=c(-121, -113), ylim=c(31,35)) +
theme(panel.border = element_rect(colour = "gray50", fill=NA, size=1))
print(p1)