在ggplot中创建多列图例

pdu*_*ois 45 plot r ggplot2

我有以下脚本,它假设创建一个包含两个列图例的图.

 #!/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)
Run Code Online (Sandbox Code Playgroud)

但它创造了这个数字 在此输入图像描述

我该怎么做才能正确?

Ido*_*mir 99

您必须将指南分配给正确的美学并使用填充:

guides(fill=guide_legend(ncol=2))
Run Code Online (Sandbox Code Playgroud)

你应该用geom_bar来处理警告

  • +1 简洁的答案。任何建议,如何解决警告? (3认同)