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)
See Question&Answers more detail:
os