gpt4 book ai didi

r - 在ggplot中创建多列图例

转载 作者:行者123 更新时间:2023-12-03 06:21:07 25 4
gpt4 key购买 nike

我有以下脚本,假设创建具有两列图例的绘图。

 #!/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

我怎样才能正确地做到这一点?

最佳答案

您必须为指南指定正确的美学并且您使用了填充:

guides(fill=guide_legend(ncol=2))

您应该使用 geom_bar 处理警告

关于r - 在ggplot中创建多列图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18400432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com