Is it possible to manually indicate the number of factors shown per column in a ggplot legend? This is a reproducible example demonstrating what I am trying to do:
#loading libraries
library(ggplot2)
#Creating hypothetical dataframe
services<-data.frame(study=c(0),category=c(0),subcategory=c(0))
services<-services[-1,]
services[1:15,1]<-c(1:15)
services[1:4,2]<-c("Provisioning", "Regulating", "Suporting", "Cultural")
services[5:8,2]<-c("Provisioning", "Regulating", "Suporting", "Cultural")
services[9:10,2]<-c("Provisioning", "Regulating")
services[11:15,2]<-c("Provisioning", "Regulating", "Suporting", "Cultural", "Provisioning")
services[1:4,3]<-c("Water supply", "Climate regulation", "Soil formation", "Recreation")
services[5:8,3]<-c("Fisheries", "Water purification", "Habitat", "Recreation")
services[9:10,3]<-c("Water supply", "Flood regulation")
services[11:15,3]<-c("Agriculture", "Water purification", "Soil formation", "Aesthetics", "Fisheries")
services
#Manually re-ordering subcategory factors by larger categories and by number of occurences in the df (I am also looking for a better way to do this, as it must exist!!)
table(services$subcategory)
services$subcategory <- factor(services$subcategory,
levels=c(#Cultural services
"Recreation", "Aesthetics",
#Provisioning services
"Fisheries", "Water supply", "Agriculture",
#Regulating services
"Water purification", "Climate regulation","Flood regulation",
#Supporting services
"Soil formation", "Habitat"))
#Creating figure
ggplot(services, aes(category, fill=subcategory)) + geom_bar() +
theme(legend.position="right") +
guides(fill=guide_legend(ncol=4, title="Ecosystem Service Sub Categories"))+
xlab("Ecosystem Service Type") +
ylab("Number of times each ecosystem service was evaluated")
The Figure produced by this code
What would like the figure legend to look like instead!
An Even more advanced way I'd love the figure to come out as
Although this suggestion, and this one get at this idea, they do not produce the result I am looking for. I've been pondering this problem for weeks and have spent hours looking around for a solution as I'm against having to manually make these adjustments in power point, illustrator etc. Unable to find a good answer, I'm now turning towards you folks! Thanks for any help!