How to insert caption in ggplot maps?

2

How do I enter the caption and change the map scale to degrees?

library("rgeos")
library("maptools")
library("ggplot2")
library("mapproj")

cores1 = c('brown2', 'aquamarine2', 'darkgray','darkolivegreen1', 'floralwhite')

ggplot() + geom_polygon(data=mapa, mapping=aes(x=long, y=lat, group=group), 
                        color="black", fill=cores1[mapa$tb]) + 
           coord_map() + 
           labs(y="latitude", x="longitude") +
           theme(plot.title=element_text(hjust = 5)) 

    
asked by anonymous 27.06.2018 / 01:28

1 answer

3

The ggplot is not including the caption because you are assigning colors as an attribute and not as mapping . | Define attributes does not include new information in the chart (map), it only changes visual aspects. For this reason they do not need captioning.

Already mapping a variable in some aesthetic aspect (within aes(...) is to add information to the graph (map), so this information can be decoded, it needs captioning.

By simply including fill = tb within aes(...) , ggplot itself already creates the caption.

Re-creating the data

I used this object mapa to build as maps below:

mapa <- map_data("state")
mapa$tb <- factor(sample(1:5, size = nrow(mapa), replace = TRUE))

Plotting the maps

Simply remove fill=cores1[mapa$tb] and include fill = tb within aes()

ggplot() + 
  geom_polygon(data=mapa, mapping=aes(x=long, y=lat, group=group, fill = tb),
               color="black") + 
  coord_map() + 
  labs(y="latitude", x="longitude") +
  theme(plot.title=element_text(hjust = 5))

Butinthissolutionwelostthecolorsyouwanted.Toreinclinethemwiththeproposedmethodology,wehavetoaddamanualcolorscalewiththescale_fill_manual()function.Theargumentthatwillbepassedtothisfunctionisjustthevectorwiththecolorsyouhavechosen.Thevaluesargumentmustbeexplicit.Sowehave:

ggplot()+geom_polygon(data=mapa,mapping=aes(x=long,y=lat,group=group,fill=tb),color="black") + 
  coord_map() + 
  labs(y="latitude", x="longitude") +
  theme(plot.title=element_text(hjust = 5)) +
  scale_fill_manual(values = cores1)

    
28.06.2018 / 15:31