ggplot2堆积柱形图笔记

#creat a dataset(生成数据)
specie<-c(rep("sorgho",3),rep("poacee",3),rep("banana",3),rep("triticum",3))
condition<-rep(c("normal","stress","Nitrogen"),4)
value<-abs(round(rnorm(12,0,15),2))
df<-data.frame(specie,condition,value)
library(ggplot2)
#分组柱形图
p1<-ggplot(df,aes(x=specie,y=value,fill=condition))+geom_bar(position="dodge",stat="identity")
p1+theme(legend.position = "none")#去掉右侧图例
p1+scale_fill_manual(values=c("blue","red","green"))#自定义填充色
Rplot09.png
Rplot10.png
Rplot11.png
#堆积柱形图
ggplot(df,aes(x=specie,y=value,fill=condition))+geom_bar(stat="identity")
ggplot(df,aes(x=specie,y=value,fill=condition))+
  geom_bar(stat="identity")+
  geom_text(aes(label=value),position=position_stack(vjust=0.5))#添加标签
可以比较一下一下三条命令出图的区别
ggplot(df,aes(x=specie,y=value,fill=condition))+
  geom_bar(stat="identity")+
  geom_text(aes(label=value))#1
ggplot(df,aes(x=specie,y=value,fill=condition))+
  geom_bar(stat="identity")+
  geom_text(aes(label=value),position=position_stack())#2 position_stack()参数用来调整添加的标签和每部分堆积柱子相匹配,默认应该是添加到每部分顶端
ggplot(df,aes(x=specie,y=value,fill=condition))+
  geom_bar(stat="identity")+
  geom_text(aes(label=value),position=position_stack(vjust=0.5))#3 vjust参数用来调整标签的为重,vjust=0.5将标签放到对应部位的中部
Rplot12.png
Rplot13.png
Rplot14.png
Rplot15.png

基本的堆积柱形图应该做完了,接下来模仿这张图片
QQ图片20180712220000.jpg

暂时只想到一种解决办法,更改数据格式,把之前用到的df数据集改成这样:添加两列用来指定文本和标签的位置
更改后的数据集
ggplot(df,aes(x=specie,y=value,fill=condition))+
  geom_bar(stat="identity")+
  geom_text(aes(label=specie,y=var4))+ylim(-20,65)+#设置纵坐标范围以掩盖多余的标签
  geom_label(aes(label=value,y=var5))+theme_bw()+
  theme(axis.text.x = element_blank(),#去除横坐标轴标签
        axis.ticks.x = element_blank(),#去除横坐标刻度
        axis.line.x = element_blank(),
        panel.border = element_blank(),#去掉边框
        axis.ticks.y = element_blank(),#去掉纵坐标刻度
        legend.position="none")#去掉图例
效果基本满意

PS:突然想到之前遇到的一个问题找到了解决办法,明天(20180702)来补充;找资料还找到了python绘制堆积柱形图的代码,明天也重复一下。有些困了,睡觉

更新

之前学习过kaggle的一篇文章https://www.kaggle.com/apapiu/exploring-kobe-s-shots探索科比的投篮选择

#完全重复其代码
setwd("Rpractice/kaggle_practice_data/Kobe_shot_selection/")
df<-read.csv("data.csv",stringsAsFactors=F)
train<-df[!is.na(df$shot_made_flag),]
names(train)
train$shot_made_flag<-factor(train$shot_made_flag,levels=c("1","0"))#这句代码很重要
ggplot(train,aes(x=season,fill=shot_made_flag))+geom_bar(stat="count",position="fill")+
scale_fill_manual(values=c("red","blue"))+
theme(axis.text.x = element_text(angle=90,vjust=0.5))#将横坐标标签调整为垂直,vjust 轻微调整位置

scale_fill_brewer(palette="Set1",direction=-1)#填充颜色
Rplot07.png

重复里面的堆积柱形图时遇到的问题:如果我想让上半部分红色与下半部分蓝色互换位置应该怎么办,答案:将代码一换成代码二

train$shot_made_flag<-factor(train$shot_made_flag,levels=c("1","0"))#代码一
train$shot_made_flag<-factor(train$shot_made_flag,levels=c("0","1"))#代码二
结果就变成了这样
参考文章

https://stackoverflow.com/questions/34903368/how-to-center-stacked-percent-barchart-labels
https://stackoverflow.com/questions/45469147/ggplot2-update-stacked-barplot-with-percentage-labels
https://www.r-graph-gallery.com/48-grouped-barplot-with-ggplot2/
https://blog.csdn.net/Bone_ACE/article/details/47427453

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 文|陈小肥 第一次看到“丧偶式育儿”这个词,是在朋友圈看到一个视频,视频开头是外国爸爸独自带着两个孩子,抱着年纪大...
    陈小肥卡阅读 7,091评论 0 1
  • 今天中午游泳很在状态,浑身有使不完的劲,越游状态越好。每每使劲蹬一下腿,向前划一下都很爽的感觉,而且感觉...
    记得祝福阅读 1,495评论 2 4
  • Leaked IAAF doping files: Wada 'very alarmed' by allegati...
    Goston007阅读 2,206评论 0 2
  • 画风不一样啊 你可能会问,高达模型不是很好做么?从板子上剪下来,贴上贴纸,插在一起就可以了啊。 让我们看看下面这张...
    曹建峰阅读 5,031评论 0 1