My ggplot has the following legend:
I want to group my individual legend variables, and add the group names and "brackets" like shown in legend below:
My data has 2 columns:
1 - States of USA
2 - Activity level which has a range from 10 (High) - 1 (Low)
I am also using data -
us<-map_data("state"), which is included in ggplot/map package.
My code:
ggplot()+ geom_map(data=us, map=us,aes(x=long, y=lat, map_id=region),
fill="#ffffff", color="#ffffff", size=0.15) +
geom_map(data=dfm4,map=us,aes(fill=ACTIVITY.LEVEL,map_id=STATENAME)
,color="#ffffff", size=0.15)+
scale_fill_manual("Activity",
values=c("10"="red4","9"="red2","8"="darkorange3",
"7"="orange3","6"="orange1",
"5"="gold2","4"="yellow","3"="olivedrab3","2"="olivedrab2",
"1"="olivedrab1"),
breaks=c("10","9","8","7","6","5","4","3","2","1"),
labels=c("High - 3","High - 2","High - 1","Moderate - 2","Moderate -
1","Minimal - 2","Minimal - 1","Low - 3","Low - 2","Low - 1"))+
labs(x="Longitude",y="Latitude")
Reproducible data:
state<-c("alabama",
"alaska", "arizona", "arkansas", "california", "colorado", "connecticut",
"delaware", "district of columbia", "florida", "georgia", "hawaii",
"idaho", "illinois", "indiana", "iowa", "kansas", "kentucky",
"louisiana", "maine", "maryland", "massachusetts", "michigan",
"minnesota", "mississippi", "missouri", "montana", "nebraska",
"nevada", "new hampshire", "new jersey", "new mexico", "new york",
"new york city", "north carolina", "north dakota", "ohio", "oklahoma",
"oregon", "pennsylvania", "puerto rico", "rhode island", "south carolina",
"south dakota", "tennessee", "texas", "utah", "vermont", "virgin islands",
"virginia", "washington", "west virginia", "wisconsin", "wyoming")
activity<-c("10", "10", "10", "10",
"8", "8", "6", "10", "10", "1", "10", "6", "4", "10", "10", "7",
"10", "10", "10", "2", "10", "10", "9", "9", "10", "10", "2",
"10", "8", "10", "10", "10", "10", "10", "3", "8", "10", "8",
"10", "10", "10", "10", "10", "10", "7", "10", "10", "1", "10",
"7", "10", "10", "9", "5")
reproducible_data<-data.frame(state,activity)
Because @erocoar provided the grob digging alternative, I had to pursue the create-a-plot-which-looks-like-a-legend way.
I worked out my solution on a smaller data set and on a simpler plot than OP, but the core issue is the same: ten legend elements to be grouped and annotated. I believe the main idea of this approach could easily be adapted to other
geom
andaes
.In
ggplot
the legend is a direct result of the mapping inaes
. Some minor modifications can be done intheme
or inguide_legend(override.aes
. For further customization you have to resort to more or less manual 'drawing', either by speleological expeditions in the realm of grobs (e.g. Custom legend with imported images), or by creating a plot which is added as legend to the original plot (e.g. Create a unique legend based on a contingency (2x2) table in geom_map or ggplot2?).Another example of a custom legend, again grob hacking vs. 'plotting' a legend: Overlay base R graphics on top of ggplot2.
It's an interesting question and a legend like that would look very nice. There's no data so I just tried it on a different plot - the code could probably be generalized much more but it is a first step :)
First, the plot
And then the changes using
gtable
andgrid
libraries.Potential problems are