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.Thevalues
argumentmustbeexplicit.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)