Bon*_*ieM 3 dictionary r ggplot2 geom-bar r-sf
我想将条形图添加到 ggplot2 中的地图中。存在类似的问题(这个和这个),但他们的答案涉及 ggsubplot,它已被弃用。
geom_scatterpie()提供了一种使用饼图执行此操作的方法(示例 1,也请参阅示例 2),但饼图在视觉上不如条形图直观。同样,我们可以使用此处的geom_sf(size=)说明将气泡大小绘制到地图上。那么有没有办法用 bar 来做到这一点呢?
每个位置制作一根酒吧的可重复示例:
# devtools::install_github("hrbrmstr/albersusa")
library(albersusa)
library(sf)
library(ggplot2)
# make a map
statesus <- fortify(usa_sf("laea"))
p <- ggplot() +
     geom_sf(data=statesus, size=0.4)
# set up the data
lat <- c(-68.24, -109.88, -80.88, -113.85)
lon <- c(44.35, 38.24, 25.37, 48.75)
park <- c("Acadia", "Canyonlands", "Everglades", "Glacier")
proportion <- c(0.10, 0.80, 0.05, 0.45) # bar heights
parkdat <- data.frame(lat=lat, lon=lon, park=park, proportion=proportion)
parkdatsf <- st_as_sf(parkdat,
             coords=c(lon="lon", lat="lat"), 
             crs=4326, 
             agr="constant")
# add points to the map (ideally these would be bars)
p + geom_sf(data=parksdatsf)
添加条形作为点对我来说听起来有点尴尬。geom_rect如果您想在地图上添加条形,一种选择是像这样使用:
library(sf)
library(ggplot2)
library(albersusa)
p <- ggplot() +
  geom_sf(data=usa_sf(), size=0.4) +
  theme_minimal()
scale <- 10
width <- 4
p + 
  geom_rect(data=parkdat, aes(xmin = lat - width / 2, xmax = lat + width / 2, ymin = lon, ymax = lon + proportion * scale, fill = park))

| 归档时间: | 
 | 
| 查看次数: | 1044 次 | 
| 最近记录: |