Reputation: 635
I am trying to plot a certain map(polygons) with personalized colors and its respective legend - it is a colum in my data. I don't know how to determine this colors and labels in my code. When I plot the map, the colors are others.
My real data has hounders of sectors to be ploted, but for explain you, I will show only the first six lines and the colors and legend's labels
unique(data$color)
[1] green yellow red orange blue
Levels: blue green orange red yellow
unique(data$legend)
[1] aa1 aa2 aa3 aa4 aa5
Levels: aa1 aa2 aa3 aa4 aa5
head(data)
id color legend
1 3.300456e+14 green aa2
2 3.300456e+14 yellow aa4
3 3.300456e+14 red aa5
4 3.300456e+14 orange aa3
5 3.300456e+14 green aa2
6 3.300456e+14 orange aa3
First situation: legend ok, but with other colors
ggplot() +
geom_map(data=data, aes(map_id=id, fill = color), map=map.fort)+
geom_path(data=map.fort, aes(x=long, y=lat, group=group), colour="black", size=0.25)
Second situation: Colors ok, but legend is not ploted
geom_map(data=data, aes(map_id=id),fill = data$color, map=map.fort)+
geom_path(data=map.fort, aes(x=long, y=lat, group=group), colour="black", size=0.25)
All that I want is plot my map like the first image, but with my color personalized and the respective legend label
Upvotes: 2
Views: 1929
Reputation: 58825
Try setting the fill
aesthetic to as.character(color)
and then adding
scale_fill_identity(guide = "legend")
after the geom's.
Untested, since example unreproducible.
Answering the modified question:
Putting both the color and the legend in the data frame is not necessary; ggplot handles it differently. Set the fill
aesthetic to legend
and add
scale_fill_manual(values=(aa2="green", aa3="orange", aa4="yellow", aa5="red"))
This is probably now a duplicate, though.
Upvotes: 3