Add multiple points to legend

77 views Asked by At

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
2

There are 2 answers

0
pogibas On BEST ANSWER

One solution would be to create "dummy" mappings. For Institutional Score we map fill and for Inst Mean we can map shape.

library(ggplot2)
ggplot(sample,aes(inst, peer_mean)) + 
    geom_errorbar(aes(ymin = peer_min, ymax = peer_max), 
                  colour = my_grey, width = 0.5, size = 2) + 
    geom_point(aes(fill = "Institutional Score", y = inst_value, ), 
               shape = 8, colour = my_red, size = 4) + 
    geom_point(aes(shape = "Inst Mean"), size = 3) + 
    facet_wrap(~ variable, scales = "free_x") +
    scale_shape_manual(values = 15) +
    labs(title = "this is my title",
         fill = NULL, 
         shape = NULL) +
    theme(panel.background=element_blank(), 
          plot.title=element_text(hjust=0.5, size=15, face = "bold"), 
          strip.text=element_text(size=12, face="bold"), 
          axis.title = element_blank())

enter image description here

PS.: I have similar answer here, however when working with multiple variables it becomes more complicated.

0
zx8754 On

Reshape the data then plot.

library(tidyr)

# reshape from wide-to-long, so we call geom_point once
plotDat <- sample %>% 
  gather(key = "Group", value = "Mean", -c(inst, variable, peer_max, peer_min))

ggplot(data = plotDat, aes(x = inst, y = Mean, shape = Group, colour = Group)) + 
  facet_wrap(~variable, scales = "free_x") + 
  geom_errorbar(aes(ymin = peer_min, ymax = peer_max), colour = my_grey, width = 0.5, size = 2) + 
  geom_point(size = 3) +
  scale_shape_manual(name  =  "Legend Title", values = c(8, 15)) +
  scale_colour_manual(name  =  "Legend Title", values = c(my_red, my_black, my_grey)) +
  ggtitle("this is my title") +
  # Keep all theme in one theme()
  theme(legend.position = "bottom",
        panel.background = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 15, face  =  "bold"),
        strip.text = element_text(size = 12, face = "bold"),
        axis.title  =  element_blank()) 

enter image description here