ggplot US state map; colors are fine, polygons jagged - r

前端 未结 2 1532
天涯浪人
天涯浪人 2020-12-09 13:31

I\'m trying to plot a US map where each state is shaded by the count that it has. I\'ve gotten the shading to work just fine. The problem I\'m running into, however, is that

相关标签:
2条回答
  • 2020-12-09 14:11

    You don't need to do the merge. You can use geom_map and keep the data separate from the shapes. Here's an example using the built-in USArrests data (reformatted with dplyr):

    library(ggplot2)
    library(dplyr)
    
    us <- map_data("state")
    
    arr <- USArrests %>% 
      add_rownames("region") %>% 
      mutate(region=tolower(region))
    
    gg <- ggplot()
    gg <- gg + geom_map(data=us, map=us,
                        aes(x=long, y=lat, map_id=region),
                        fill="#ffffff", color="#ffffff", size=0.15)
    gg <- gg + geom_map(data=arr, map=us,
                        aes(fill=Murder, map_id=region),
                        color="#ffffff", size=0.15)
    gg <- gg + scale_fill_continuous(low='thistle2', high='darkred', 
                                     guide='colorbar')
    gg <- gg + labs(x=NULL, y=NULL)
    gg <- gg + coord_map("albers", lat0 = 39, lat1 = 45) 
    gg <- gg + theme(panel.border = element_blank())
    gg <- gg + theme(panel.background = element_blank())
    gg <- gg + theme(axis.ticks = element_blank())
    gg <- gg + theme(axis.text = element_blank())
    gg
    

    enter image description here

    0 讨论(0)
  • 2020-12-09 14:26

    I encountered a similar problem when using ggplot2. As an alternative to @hrbrmstr 's solution, I found reloading the ggplot2 package (as well as rgdal/`maptools') resolved the issue for me.

    0 讨论(0)
提交回复
热议问题