简体   繁体   中英

How to generate multiple columns in legend using ggplot2 (R)

I want to generate a multiple-column legend (2 or more) for the following figure using this piece of code. But the guides() does not work properly. Can someone help me?

library(ggplot2)
 testdf <- data.frame(site=unlist(strsplit(rawToChar(as.raw(65:90)),split = '')),
                              x = c(seq(0:24),80), y = seq(1:26))
 testdf$colours <- 'red'
 p <- ggplot(data=testdf,aes(x,y))
 p + scale_shape_manual(values=testdf$x)+
   scale_colour_manual(values=testdf$colours)+
   geom_point(aes(shape=site,colour=site),size=5)+
   guides(col=guide_legend(ncol=3,byrow=TRUE))

在此处输入图片说明

Thanks, Katie

col3 <- guide_legend(ncol = 3)
ggplot(testdf, aes(x = x, y = y, shape = site, colour = site)) + 
  geom_point() + 
  scale_shape_manual(values = testdf$x, guide = col3) + 
  scale_colour_manual(values = testdf$colours, guide = col3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM