Get ggplot2 legend to display percentage sign in r

Trevor Nederlof picture Trevor Nederlof · Mar 23, 2015 · Viewed 7.3k times · Source

Below is a reproducible example of the issue I am trying to solve. I have created a heatmap of sorts in ggplot2 and things have been going well. Since I have put percentage signs on the data to use with geom_text I would like to make the legend of geom_tile also to display percent signs (I can only multiply the actual values by 100 right now). Ideally I would like the legend bar on the right to show 8%, 4%, 0%, -4%, -8%.

#load in libraries
require(plyr)
require(dplyr)
require(reshape2)
require(ggplot2)
require(scales)

testDF <- structure(list(strategies = structure(c(8L, 7L, 6L, 5L, 4L, 3L, 
                                                  2L, 1L), .Label = c("Class 1", "Class 2", 
                                                                      "Class 3", "Class 4", "Class 5", "Class 6", 
                                                                      "Class 7", "Class 8"), class = "factor"), 
                         School1 = c(0.0355662887589396, 0.0316753241146625, 0.00606392341292672, 
                                     0.0250738342627283, -0.0405709181701368, 0.0237665074609996, 
                                     0.00587364885411765, -0.0343914002059331), School2 = c(NA, NA, 
                                                                                            NA, 0.0225535750673764, NA, -0.00448947685878404, NA, -0.0446386763157662
                                     ), School3 = c(NA, NA, NA, 0.0261099462365593, NA, 0.0199735626692146, 
                                                    NA, -0.0272279264519992), School4 = c(NA, NA, NA, 0.0164004151291513, 
                                                                                          NA, 0.00567638888888868, NA, -0.0384017249374949)), .Names = c("schools", 
                                                                                                                                                         "School1", "School2", "School3", "School4"), row.names = c(NA, -8L), class = "data.frame")


GraphMelt <- melt(testDF)
GraphMelt <- GraphMelt %>% mutate(text = sprintf("%1.2f%%", 100*value))
GraphMelt[,"text"] <- ifelse(GraphMelt[,"text"]=="NA%",NA,GraphMelt[,"text"])                                    
p <- ggplot(GraphMelt, aes(variable, schools))
p <- p + geom_tile(aes(fill = value*100), colour =   "white") + geom_text(aes(label=text),size=7)
p <- p + scale_fill_gradient(low = "red", high = "green",limits=c(-8,8))
p <- p + theme(
  axis.text.x= element_text(color="black", size=14, vjust=0.5),
  axis.text.y= element_text(color="black", size=14, vjust=0.5),
  axis.title.y = element_text(color="black",size=14, vjust=0.5),
  plot.title = element_text(color="black",size=14,face="bold", hjust=0.5,vjust=1),
  panel.background = element_blank(),
  legend.position="right",
  legend.title = element_blank(),
  legend.key = element_rect(fill="white"), legend.background = element_rect(fill=NA)
)
p <- p + xlab("") + ylab("") + ggtitle("Schools")

Answer

Gregor Thomas picture Gregor Thomas · Mar 23, 2015

Load the scales package (you already have it, but I want to be explicit about this dependency)

library(scales)

and add labels = percent to your fill scale (or use scales::percent if you don't want to use library(scales) for whatever reason).

scale_fill_gradient(low = "red", high = "green", limits = c(-8, 8), labels = percent)