||
library(gcookbook)
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+
geom_bar(position = "dodge",stat ="identity" )
原始数据 | 基础簇状图 |
备注:#其中cultivar,data是分类变量,weight是数值
library(gcookbook)
P1<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+
geom_bar(position = "dodge",stat ="identity",colour="black" )+
scale_fill_brewer(palette="Greens")
# scale_fill_brewer(palette=1)
# scale_fill_brewer(palette="Set2")
P1
P2<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+
geom_bar(position = "dodge",stat ="identity",colour="black" )+
# scale_fill_manual(values =c("#669933","#FFCC66"))
#scale_fill_manual(values =c("420","414"))
scale_fill_manual(values =c("lightpink1","lightsalmon"))
P2
scale_fill_brewer (palette="Greens") | scale_fill_manual(values =c("lightpink1","lightsalmon")) |
备注:#添加colour修改为黑色边框,使用scale_fill_brewer()或scale_fill_manual()函数
#注意颜色的映射设定在aes()内部,而颜色重新设置在aes()外部完成;此外,scale_fill_brewer()有三种颜色设置scale_fill_brewer(palette="Greens")、scale_fill_brewer(palette=1)、scale_fill_brewer(palette="Set2"),而scale_fill_manual(values =c("#669933","#FFCC66"))、scale_fill_manual(values =c("420","414"))、scale_fill_manual(values =c("lightpink1","lightsalmon")),选择任何一种函数时可以任选一种方式即可。
library(gcookbook)
P3<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+
geom_bar(position = "dodge",stat ="identity",width=1,colour="black" )+
scale_fill_brewer(palette="Greens")
P3
P4<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+
geom_bar(position = "dodge",stat ="identity",width=0.5,colour="black" )+
scale_fill_brewer(palette="Greens")
P4
position_dodge值 等于width值 P3 | position_dodge值 大于width值 P4 |
备注:簇状条形图默认间距为0,如果想增加条形的间距,需要满足position_dodge值大于width值,而position_dodge(0.9)相当于position="dodge",colour是设置边框颜色。
library(gcookbook)
P5<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+
geom_bar(position = "dodge",stat ="identity",width=0.5,colour="black" )+
geom_text(aes(label=Weight),vjust=1.5,colour="black",position=position_dodge(0.9),size=3)+
scale_fill_brewer(palette="Greens")
P5
#在条形图顶端上方
P6<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+
geom_bar(position = "dodge",stat ="identity",width=0.5,colour="black" )+
geom_text(aes(label=Weight),vjust=-0.5,colour="black",position=position_dodge(0.5),size=3)+
scale_fill_brewer(palette="Greens")
P6
#在簇状图顶端下方
P7<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+
geom_bar(position = "dodge",stat ="identity",width=0.5,colour="black" )+
geom_text(aes(label=Weight),vjust=1.5,colour="black",position=position_dodge(0.5),size=3)+
scale_fill_brewer(palette="Greens")
P7
P5 | P6 | P7 |
备注:P5,P6, P7不同时因为geom_text ()函数中vjust参数(正负值)与主要调整标签的上下位置有关,而position参数的大小与标签的中间左右对齐有关。
#如果标签超过绘图区域,因此考虑Y轴的范围调整
library(gcookbook)
P8<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+
geom_bar(position = "dodge",stat ="identity",width=0.5,colour="black" )+
geom_text(aes(y=Weight+0.1,label=Weight),vjust=-0.3,colour="black",position=position_dodge(0.5),size=3)+
scale_fill_brewer(palette="Greens")
P8
#Y轴上限变大
P9<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+
geom_bar(position = "dodge",stat ="identity",width=0.5,colour="black" )+
geom_text(aes(label=Weight),vjust=-0.3,colour="black",position=position_dodge(0.5),size=3)+
scale_fill_brewer(palette="Greens")+
ylim(0,max(cabbage_exp$Weight)*1.05)
P9
P8 | P9 |
备注:调整Y轴的范围可根据实际情况而定。
自我总结:
绘制簇状条形图及其美化,主要有以下几个函数:
ggplot(data,aes(x,y,fill)
geom_bar(position = "dodge",stat ="identity" , width=1, colour="black" )
geom_text(aes(label=Weight),vjust=1.5,colour="black",position=position_dodge(0.9),size=3)
scale_fill_brewer(palette="Greens")或scale_fill_manual(values =c("lightpink1","lightsalmon"))
geom_text(aes(label=Weight+0.1),vjust=1.5,colour="black",position=position_dodge(0.9),size=3)或geom_text(aes(label=Weight),vjust=0.3,colour="black",position=position_dodge(0.5),size=3)+
ylim(0,max(cabbage_exp$Weight)*1.05)
以上是我对簇状条形图的总结,望大家批评和指正,我们一起学习。
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-25 08:21
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社