2

I am trying to create faceted maps with the "tmap" package. My data contains a lot of missing countries. When I run the code below, the output shows a lot of missing countries. How can get the plot to show missing countries?

output

library(tmap)

    world<-data("World")

    MAP<- read_excel("SSPMAP.xlsx")
    MAP2<-MAP %>% group_by(iso_a3,scenario ) %>%
      summarise(cibase=median(gr_C_2030)*100)
    table(MAP2$cibase)
    cut<-c(-4,-2,0,2,4,6,8)

    World_n<-left_join(World, MAP2, by="iso_a3") 

    ###########################
    Base<-tm_shape(World_n, projection = "eck4") +
      tm_polygons("cibase", breaks = cut,
                  palette = "RdYlGn", 
                  border.col = "black", 
                  border.alpha = 1,midpoint = NA ) +
      tm_legend(legend.position = c("left", "bottom")) +
      tm_facets(by=c("scenario"), ncol  =1, showNA = FALSE)+
      tm_layout(panel.labels=c("Baseline : SSP1","Baseline : SSP2","Baseline : SSP4","Baseline : SSP5"),panel.label.color = "black"
                ,panel.label.size=1.3,panel.label.fontface="bold")
    Base
    tmap_save(Base,"Base.png")

Sourabh Choraria
  • 2,255
  • 25
  • 64
RawisWar
  • 29
  • 4

2 Answers2

2

tmap works similar to a GIS, you can add layers. So, if you want to show countries with missing data you can add the entire world as a layer and put your data on top of that. Something like:

Base <- tm_shape(world) + tm_polygons() + 
      tm_shape(World_n, projection = "eck4") +
      tm_polygons("cibase", breaks = cut,
              palette = "RdYlGn", 
              border.col = "black", 
              border.alpha = 1,midpoint = NA ) +
  tm_legend(legend.position = c("left", "bottom")) +
  tm_facets(by=c("scenario"), ncol  =1, showNA = FALSE)+
  tm_layout(panel.labels=c("Baseline : SSP1","Baseline : SSP2","Baseline : SSP4","Baseline : SSP5"),panel.label.color = "black"
            ,panel.label.size=1.3,panel.label.fontface="bold")
Base
Orlando Sabogal
  • 1,470
  • 7
  • 20
  • I'm going to use your description of tmap as "like a GIS" as it will help me explain better to people why I prefer tmap to ArcGIS. Thanks! – damo Oct 07 '19 at 09:33
0

I think it would be helpful if you could include some data to work with? At the moment : MAP is asking for a local file. So it's a bit difficult to see what is happening. I'm going to guess that when you create MAP2 you're looking at the median of a population growth prediction? And maybe some of the rows in world_n don't have a value in cibase?


damo
  • 463
  • 4
  • 14