I have some data
sample <- data.frame(inst=c('School1', 'School1', 'School1', 'School1', 'School2', 'School2', 'School2', 'School2', 'School3', 'School3', 'School3', 'School3'), variable=c('Math_25', 'Math_75', 'Reading_25', 'Reading_75', 'Math_25', 'Math_75', 'Reading_25', 'Reading_75', 'Math_25', 'Math_75', 'Reading_25', 'Reading_75'), peer_min=c(50, 84, 61, 83, 40, 60, 55, 85, 52, 75, 75, 87), peer_max=c(66, 95, 77, 90, 55, 85, 72, 91, 67, 83, 84, 95), peer_mean=c(58.0, 89.5, 69.0, 86.5, 47.5, 72.5, 63.5, 88.0, 59.5, 79.0, 79.5, 91.0), inst_value=c(55, 93, 65, 95, 60, 70, 65, 80, 60, 85, 77, 89))
and a ggplot here with some HTML colors:
my_red <- c("#AF272F")
my_black <- c("#101820")
my_grey<- c("#D0D0CE")
library(ggplot2)
plot <- ggplot(data=sample,aes(x=inst, y=peer_mean, shape=15)) + geom_errorbar(aes(ymin=peer_min, ymax=peer_max), colour=my_grey, width=0.5, size=2) + facet_wrap(~variable, scales="free_x")+ theme(panel.background=element_blank()) + ggtitle("this is my title") + theme(plot.title=element_text(hjust=0.5, size=15, face = "bold")) + theme(strip.text=element_text(size=12, face="bold")) + geom_point(size=3) + geom_point(aes(x=inst, y=inst_value, shape=8), colour=my_red, size=4) + scale_shape_identity() + theme(axis.title.x = element_blank()) + theme(axis.title.y = element_blank())
I'm trying to get a legend with just the black squares (labeled as "Inst Mean") and the red star (labeled as "Institutional Score")
I saw this helpful question and tried to apply it to my plot:
plot <- plot + theme(legend.position="bottom") + scale_shape_manual(name = "Legend Title", values=c(15,8)) + scale_colour_manual(name = "Legend Title", values=c("my_black", "my_red"))
and I got this error:
Error: Continuous value supplied to discrete scale
One solution would be to create "dummy" mappings. For
Institutional Score
we mapfill
and forInst Mean
we can mapshape
.PS.: I have similar answer here, however when working with multiple variables it becomes more complicated.