在 ggplot 中创建多列图例

我有下面的脚本,假设 用两列图例创造一个情节。

 #!/usr/bin/Rscript
library(ggplot2)
library(plyr)
library(reshape2)
library(scales)


file <- "http://dpaste.com/1354089/plain/";
dat <-read.table(file,header=TRUE);
datm <- melt(dat)


# Plot them
ggplot(datm,aes(x = variable,y = value,fill = Term)) +
geom_bar(position = "fill") +
scale_y_continuous(labels = percent_format())+
theme(legend.direction ="vertical",legend.position = "bottom")+


guides(color=guide_legend(ncol=2))+ # this doesn't seem to work


ggsave(file="~/Desktop/test.pdf",width=11,height=11)

但它却创造了这个数字 enter image description here

我怎样才能做得正确呢?

62708 次浏览

你必须指定正确的审美指南,并使用填充:

guides(fill=guide_legend(ncol=2))

你应该注意 geom_bar的警告