R--随处收藏

R语言中画柱状图的一些技巧

2016-03-31  本文已影响0人  zouxiaoyu

如何将图画成在black and white的背景下更好读而不会丢失过多信息的策略之一:使用scale_fill_grey():

library(ggplot2)data(tips)p0=qplot(day,tip/total_bill,data=tips,geom='boxplot',fill=day)+scale_fill_grey()print(p0)

This produces the output shown below

特点:X,Y轴翻转;将默认的bar的stat为count换成了自己定义的y值;使用stack做y值的堆积而不是用fill来归一化;可以将X轴按照自己定义要求的改变默认的显示顺序;bar图中使用了数据集中的rank变量来做柱状图的颜色填充;

ggplot(t,aes(x=metric,y=percent))+geom_bar(aes(fill=rank),position="stack",stat="identity")+coord_flip()

 ggplot(t,aes(x=reorder(metric,percent,sum),y=percent))+geom_bar(aes(fill=rank),position="stack",stat="identity")+coord_flip()

上一篇 下一篇

猜你喜欢

热点阅读