Reputation: 131
I am labelling a map with differents variables using geom_point, geom_label and geom_label_repel in a ggplot map.
All its ok but lamentably I can not add the legend for geom_label_repel (blue label), I tried with: scale_fill_manual, but dont solve my problem, example of my code and problem:
library(ggplot2)
library(maps)
library(ggrepel)
county_df <- map_data('county')
ny <- subset(county_df, region=="new york")
ny <- ny[c(1:10, 30:40),]
ggplot(ny, aes(long, lat, group=group)) +
geom_polygon(colour='black', fill=NA)+
geom_point(data = ny, aes(long, lat, size=group, color=order))+
geom_label_repel(data = ny, aes(long, lat, label=subregion),
fill = "#0033FF", box.padding = unit(.8, "lines"))+
geom_label(data = ny, aes(long+.01, lat+.05, label=region,
fill="#FF3399"), colour = "White", fontface = "bold")+
scale_fill_manual("Variable:", values=c("#FF3399", "#0033FF"),
labels = c('Region', "Subregion"))
This command gave me this map, you can see the legend only for Pink label (Region) and not for the blue label (subregion):
I tried using the command: show.legend = T, but lamentably this gave me a erroneous legend for the geom_point (group) and not show the legend for blue label(Subregion). The comand for this was:
ggplot(ny, aes(long, lat, group=group)) +
geom_polygon(colour='black', fill=NA)+
geom_point(data = ny, aes(long, lat, size=group, color=order))+
geom_label_repel(data = ny, aes(long, lat, label=subregion),
fill = "#0033FF", box.padding = unit(.8, "lines"),
show.legend = T)+
geom_label(data = ny, aes(long+.01, lat+.05, label=region,
fill="#FF3399"), colour = "White", fontface = "bold")+
scale_fill_manual("Variable:", values=c("#FF3399", "#0033FF"),
labels = c('Region', "Subregion"))
The map for this:
Upvotes: 0
Views: 870
Reputation: 2384
Looks like your fill
aesthetic for subregions was outside its aes()
so it technically wasn't 'mapped' and therefore did not showed up in the guide. The following fixes the problem I think:
library(ggplot2)
library(maps)
library(ggrepel)
county_df <- map_data('county')
ny <- subset(county_df, region=="new york")
ny <- ny[c(1:10, 30:40),]
ggplot(ny, aes(long, lat, group = group)) +
geom_polygon(colour = 'black', fill = NA) +
geom_point(data = ny, aes(long, lat, size = group, color = order)) +
geom_label_repel(
data = ny,
aes(long, lat, label = subregion, fill = "#0033FF"),
box.padding = unit(.8, "lines")
) +
geom_label(
data = ny,
aes(long + .01, lat + .05, label = region, fill = "#FF3399"),
colour = "White",
fontface = "bold"
) +
scale_fill_manual(aesthetics = "fill",
"Variable:",
values = c("#FF3399", "#0033FF"),
labels = c('Region', "Subregion")
)
Upvotes: 0