ggplot map with l

后端 未结 2 742
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 13:00

I want to plot a world map using ggplot2 (v.9) which combines two pieces if information. The following example illustrates:

library(rgdal)
library(ggplot2)
         


        
相关标签:
2条回答
  • 2020-12-04 13:16

    One option is to map growth to the size of some points plotted at the centroid of the polygons.

    centroids <- as.data.frame(coordinates(world.map))
    df <- data.frame(df,centroids)
    
    choropleth <-ggplot() +
         geom_map(aes(fill = category, map_id = id),data = df, map =world.ggmap) +
         expand_limits(x = world.ggmap$long, y = world.ggmap$lat) + 
        scale_fill_hue(na.value=NA)
    choropleth
    
    
    
    choropleth + geom_point(aes(x=V1,y=V2,size=growth),data=df) +
        scale_area(range=c(0,3))
    

    enter image description here

    Or if you really want to double code color, you could color the points instead. Note, that you can also add a raster map of satellite imagery with the new OpenStreetMap package (shameless plug).

    library(OpenStreetMap)
    library(raster)
    rastermap <- openmap(c(70,-179),
            c(-70,179),zoom=2,type='bing')
    rastermap <- openproj(rastermap)
    autoplot(rastermap,expand=FALSE) +
         geom_map(aes(x=70,y=70,fill = category, map_id = id),data = df,
            map =world.ggmap) +
         expand_limits(x = world.ggmap$long, y = world.ggmap$lat) + 
        scale_fill_hue(na.value=NA) + 
        geom_point(aes(x=V1,y=V2,colour=growth),data=df) +
        scale_colour_gradient(low = "red", high = "blue", 
            guide = "colorbar",na.value=NA)
    

    enter image description here

    0 讨论(0)
  • 2020-12-04 13:24

    I would use different hue ranges for fill and line color:

    ggplot(df, aes(map_id = id)) +
      geom_map(aes(fill = growth, color = category), map =world.ggmap) +
      expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
      scale_fill_gradient(high = "red", low = "white", guide = "colorbar") +
      scale_colour_hue(h = c(120, 240))
    

    enter image description here

    OR, use fill for category and transparency for growth level.

    ggplot(df, aes(map_id = id)) +
      geom_map(aes(alpha = growth, fill = category), map =world.ggmap) +
      expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
      scale_alpha(range = c(0.2, 1), na.value = 1)
    

    enter image description here

    It depends on what you want to show.

    Just in case, here is the way to change the linesize:

    ggplot(df, aes(map_id = id)) +
     geom_map(aes(fill = growth, color = category, size = factor(1)), map =world.ggmap) +
     expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
     scale_fill_gradient(high = "red", low = "white", guide = "colorbar") +
     scale_colour_hue(h = c(120, 240)) + 
     scale_size_manual(values = 2, guide = FALSE)
    

    enter image description here

    Here is HSV version:

    df$hue <- ifelse(is.na(df$category), 0, as.numeric(df$category)/max(as.numeric(df$category), na.rm=T))
    df$sat <- ifelse(is.na(df$growth), 0, df$growth/max(df$growth, na.rm=T))
    df$fill <- ifelse(is.na(df$category), "grey50", hsv(df$hue, df$sat))
    
    ggplot(df, aes(map_id = id)) +
     geom_map(aes(fill = fill), map =world.ggmap) +
     expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
     scale_fill_identity(guide = "none")
    

    enter image description here

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