在ggplot2 / ggmap上为不同的图形系列设置固定比例

问题描述 投票:0回答:1

我已经制作了一系列月度发病率的地图。想法是制作一个gif。问题是每个地块都是用自己的规模独立创建的。在这种情况下,我正在使用viridis。是否有某种方法可以保持所有绘图的相同比例和范围,即使特定月份(一个绘图)在这些尺度上没有数据?

这就是我所做的:

library(ggmap)
library(sp)

murder <- subset(crime, offense == "murder")

murder <- SpatialPointsDataFrame(murder[,c("lon", "lat")], data=murder,
                                 proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

p <- ggmap(get_stamenmap(bbox=c(left=min(murder$lon), bottom=min(murder$lat),
                                right=max(murder$lon), top=max(murder$lat)), zoom=12))

d <- unique(murder$month)

murder <- murder[!(murder$month=="january" & murder$hour>0 & murder$hour<17),]
murder <- murder[!(murder$month=="february" & murder$hour>110 & murder$hour<24),]
murder <- murder[!(murder$month=="march" & murder$hour>12 & murder$hour<17),]


for (i in d){
  murder2 <- murder[murder$month==i,]
  mapa <- p +
    geom_point(data=murder2@data, 
               aes(x=murder2@coords[,1], y=murder2@coords[,2], color=as.numeric(hour)), 
               alpha=0.5,size=7) +
    scale_color_viridis_c(option="C",breaks=c(0,2,4,8,12,16,20,23),
                          labels=c("00:00","02:00", "04:00","08:00","12:00","16:00","20:00","23:00"),
                          name="Hour",
                          guide=guide_legend( keyheight = unit(3, units = "mm"), 
                                              keywidth=unit(6, units = "mm"),
                                              label.position = "bottom", title.position = 'top', nrow=1,
                                              label.theme=element_text(size = 6,face = "bold",color = "grey2",family = "Gotham"),
                                              title.theme=element_text(size = 6,face = "bold",color = "grey2",family = "Gotham", hjust=0.5)
                          ))
  ggsave(filename=paste0(i,".png"),plot=mapa, bg="transparent", 
         width =15, height = 15, units="cm", dpi=200)
}

这些是我的结果。第一个图(从3月开始)在其比例上有8个值,而第二个图(2月形式)只有2个:

Mar

Feb

我希望每张地图都有相同的8个值。即使这些价值观内没有任何观察。

r ggplot2 ggmap
1个回答
2
投票

尝试使用以下内容替换for循环中的代码。评论中的解释:

# there's lat / lon info in the data file, no need to reference coords separately
murder2 <- murder[murder$month==i, ]@data 

# define hour as a factor here, with the same ranges & labels, for each loop
murder2$hour <- cut(murder2$hour, 
                    breaks = c(0, 2, 4, 8, 12, 16, 20, 23, 25), 
                    labels = c("00:00", "02:00", "04:00", "08:00", 
                               "12:00", "16:00", "20:00", "23:00"),
                    right = FALSE)

mapa <- p +
  geom_point(data = murder2,
             aes(x = lon, y = lat, color = hour)) +
  scale_color_viridis_d(option = "C", 
                        name = "Hour", 
                        drop = FALSE, # keeps unused levels in the legend
                        guide = guide_legend(keyheight = unit(3, units = "mm"), 
                                             keywidth = unit(6, units = "mm"),
                                             label.position = "bottom", 
                                             title.position = 'top', 
                                             nrow = 1,
                                             label.theme = element_text(size = 6, face = "bold",
                                                                        color = "grey2",
                                                                        family = "Gotham"),
                                             title.theme = element_text(size = 6, face = "bold",
                                                                        color = "grey2",
                                                                        family = "Gotham", 
                                                                        hjust=0.5)))
ggsave(filename = paste0(i,".png"), plot = mapa, bg = "transparent",
       width = 15, height = 15, units="cm", dpi = 200)

另外,如果最终目标是制作动画gif,您可能希望查看gganimate包。它可以自动执行在不同状态之间转换所涉及的大量工作。

© www.soinside.com 2019 - 2024. All rights reserved.