文档介绍:RGraphicsCookbook——ggplot2 陈清2条形图:bargraphs折线图:LineGraphs散点图:ScatterPlots描述数据分布:SummarizedDataDistributions注解:Annotations坐标轴:Axes控制图形整体外观:ControllingtheOverallAppearanceofGraphs图例:Legends分面:factes配色:Usingcolorsinplots其他图形:MiscellaneousGraphsoutlines3条形图:bargraphs1、简单条形图1、、一个绘制在x轴的分类变量和一个绘制在y轴的连续型变量ggplot(pg_mean,aes(x=group,y=weight))+geom_bar(stat="identity")4条形图:bargraphs1、简单条形图2、注意x是离散型变量与连续型变量的区别,将连续变量转为因子型变量:ggplot(BOD,aes(x=factor(Time),y=demand))+geom_bar(stat="identity"):若不转化为因子,则会在每个连续变量的取值处都会画出来一个bar5条形图:bargraphs1、简单条形图3、默认条形图是黑灰色且没有边框线,fill可改变填充色,colour为条形图添加边框geom_bar(stat="identity",fill="lightblue",colour="black")6条形图:bargraphs2、簇状条形图1、将分类变量映射到fill,并运行geom_bar(position="dodge").ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(position="dodge",stat="identity")7条形图:bargraphs2、簇状条形图2、可用scale_fill_manual()或者scale_fill_brewer()对图形颜色进行设置ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(position="dodge",colour="black")+scale_fill_brewer(palette="Pastel1")8条形图:bargraphs1、使用geom_bar(),同时不映射任何变量到y参数ggplot(diamonds,aes(x=cut))+geom_bar()#Equivalenttousinggeom_bar(stat="bin") 对这里就不用映射y值,同时也不用stat="identity",geom_bar()在默认情况下将参数设定为stat="bin",就是计数(频数)3、频数条形图9条形图:bargraphs2、x轴对应于连续变量的条形图,也即常说的直方图,所以用geom_histogram()与geom_bar()结果一样ggplot(diamonds,aes(x=carat))+geom_bar()3、频数条形图10条形图:bargraphs4、条形图着色1、将合适变量映射到fill即可ggplot(upc,aes(x=Abb,y=Change,fill=Region))+geom_bar(stat="identity")