<Data Visualization>4 柱状图

基本使用:

  • 单序列柱形图:ggplot(mpg,aes(class,displ))+geom_bar(stat="identity",fill="steelblue")
  • 多系列簇状柱形图:ggplot(data=mpg,aes(x=class,fill=factor(year)))+geom_bar(position='stack')
  • 并列柱状图:ggplot(data=mpg,aes(x=class,fill=factor(year)))+geom_bar(position='dodge')
  • 横状条形图 ( + coord_flip()):
    ggplot(data=mpg,aes(x=class,fill=factor(year)))+geom_bar(position='dodge')+coord_flip()

加入 主题 (注意需要载入 ggthemes 包)

  • 使用华尔街主题:
ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+ggtitle("The Financial Performance of Five Giant")+theme_wsj()+scale_fill_wsj()

这里写图片描述

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+ggtitle("The Financial Performance of Five Giant")+theme_wsj()+scale_fill_wsj("rgby", "")

这里写图片描述

  • 使用 经济学人 主题
ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+ggtitle("The Financial Performance of Five Giant")+theme_economist(base_size=14)+scale_fill_economist()+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))

这里写图片描述

  • 若需在柱状上加入数据标签
    • 其中 label 和 最后的geo_text 是负责这个标签的
ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+geom_bar(stat="identity",position="dodge")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank())+geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)

Reference:
https://mp.weixin.qq.com/s?__biz=MzA3Njc0NzA0MA==&mid=2653189930&idx=1&sn=bf4bed50468198dd585e40e7b5b36cc4&scene=21#wechat_redirect

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值