Get ggplot2 legend to display percentage sign in r

青春壹個敷衍的年華 提交于 2021-02-18 09:09:13

问题


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")

回答1:


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)


来源:https://stackoverflow.com/questions/29213881/get-ggplot2-legend-to-display-percentage-sign-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!