ggplot2直方图图例太大 [英] ggplot2 histogram legend too large

查看:2002
本文介绍了ggplot2直方图图例太大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述









然而,我有一些在传说中有这么多类别的传说正在扼杀剧情,例如





我该如何解决这个问题?

这是我的plot.r,我在命令行中调用这样的

  RScript plot.r foo.dat foo。 png 1600 800 



foo.dat

 帐户,操作,call_count ,day 
cal3510,foo-method,1,2016-10-01
cra4617,foo-method,1,2016-10-03
cus4404,foo-method,1,2016- 10-03
hin4510,foo-method,1,2016-10-03
mas4484,foo-method,1,2016-10-04
...

完整的foo.dat:


I'm pretty happy with the results I'm getting with R. Most of my stacked histogram plots are looking fine, e.g.

and

However, I have a few that have so many categories in the legend that the legend is crowing out the plot, e.g.

How can I fix this?

Here is my plot.r, which I call on the command line like this

RScript plot.r foo.dat foo.png 1600 800

foo.dat

account,operation,call_count,day
cal3510,foo-method,1,2016-10-01
cra4617,foo-method,1,2016-10-03
cus4404,foo-method,1,2016-10-03
hin4510,foo-method,1,2016-10-03
mas4484,foo-method,1,2016-10-04
...

entirety of foo.dat: http://pastebin.com/xnJtJSrU

plot.r

library(ggplot2)
library(scales)

args<-commandArgs(TRUE)

filename<-args[1]
png_filename<-args[2]

wide<-as.numeric(args[3])
high<-as.numeric(args[4])

print(wide)
print(high)

print(filename)
print(png_filename)

dat = read.csv(filename)

dat$account = as.character(dat$account)
dat$operation = as.character(dat$operation)
dat$call_count = as.integer(dat$call_count)
dat$day = as.Date(dat$day)

png(png_filename,width=wide,height=high)

p <- ggplot(dat, aes(x=day, y=call_count, fill=account))
p <- p + geom_histogram(stat="identity")
p <- p + scale_x_date(labels=date_format("%b-%Y"), limits=as.Date(c('2016-10-01','2017-01-01')))

print(p)

dev.off()

解决方案

Answer from @PierreLafortune

using:

p <- p + theme(legend.position="bottom")
p <- p + guides(fill=guide_legend(nrow=5, byrow=TRUE))

这篇关于ggplot2直方图图例太大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆