R语言数据可视化教程(ggplot2)_绘制条形图与折线图

# 绘制条形图
# 3.1 简单条形图
# 使用ggplot()函数和geom_bar(stat="identity")
library(gcookbook)
ggplot(pg_mean,aes(x=group,y=weight))+geom_bar(stat = "identity")
# 当x是连续型(数值型)变量时,ggplot不是只在实际取值处绘制图形,而将在x轴上介于最大值
# 和最小值之间所有可能的取值处绘制条形
# 可以使用factor()函数将连续型变量转化为离散型变量
BOD
str(BOD)
ggplot(BOD,aes(x=Time,y=demand))+geom_bar(stat = "identity")
ggplot(BOD,aes(x=factor(Time),y=demand))+geom_bar(stat = "identity")


# 默认设置下,条形图的填充色为黑灰色且条形图没有边框线,通过调整fill参数的值来改变条形图的填充色
# 通过colour参数为条形图添加边框线。
ggplot(pg_mean,aes(x=group,y=weight))+geom_bar(stat = "identity",fill="lightblue",colour="black")


# 3.2 绘制簇装条形图
# 将分类变量映射到fill参数,并运行命令geom_bar(position="dodge)
library(gcookbook)
cabbage_exp
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(position = "dodge",stat = "identity")
# fill参数用来指定条形的填充色,position="dodge"使得两组条形在水平方向上错开排列
# 映射给条形填充色参数的变量应该是分类变量而不是连续型变量
# 通过将geom_bar()中的参数指定为colour="black"为条形添加黑色边框线
# 通过scale_fill_brewer()或者scale_fill_manual()函数对图形颜色进行设置
# 使用RColorBrewer包中的Pastell调色板对图形进行调色
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(position = "dodge",stat = "identity",colour="black")+scale_fill_brewer(palette = "Pastell")
# library(RColorBrewer)


ce <- cabbage_exp[1:5,]
ce
ggplot(ce,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(position = "dodge",stat = "identity",colour="black")+scale_fill_brewer(palette = "Pastel1")


# 3.3 绘制频数条形图
# 使用geom_bar()函数,同时不要映射任何变量到y参数
ggplot(diamonds,aes(x=cut))+geom_bar()
# 等价于使用geom_bar(stat="bin")
# 当x轴对应于连续型变量时,得到一张直方图
ggplot(diamonds,aes(x=carat))+geom_bar()
# 3.4 条形图着色
# 将合适的变量映射到填充色(fill)上
library(gcookbook)
upc <- subset(uspopchange,rank(Change)>10)
ggplot(upc,aes(x=Abb,y=Change,fill=Region))+geom_bar(stat = "identity")
# 借助scale_fill_brewer()或者scale_fill_manual()重新设定图形颜色
# 颜色的映射设定是在aes()内部完成的,而颜色的重新设定是在aes()外部完成的
# ggplot(upc,aes(x=reorder(Abb,Change),y=Change,fill=Region))+geom_bar(stat = "identity",colour="black")+scale_fill_manual(values = c("#669933","#FFCC66"))+xlab("State")
# 3.5 对正负条形图分别着色
library(gcookbook)
csub <- subset(climate,Source=="Berkeley" & Year >=1900)
csub$pos <- csub$Anomaly10y >=0
csub
ggplot(csub,aes(x=Year,y=Anomaly10y,fill=pos))+geom_bar(stat = "identity",position = "identity")
# 通过scale_fill_manual()参数对图形颜色进行调整,设定参数guide=FALSE可以删除图例
ggplot(csub,aes(x=Year,y=Anomaly10y,fill=pos))+geom_bar(stat = "identity",position = "identity",colour="black",size=0.25)+scale_fill_manual(values = c("#CCEEFF","#FFDDDD"),guide=FALSE)
# 3.6 调整条形宽度和条形间距
# 通过设定geom_bar()函数的参数width可以使条形变得更宽或更窄,默认值为0.9
library(gcookbook)
ggplot(pg_mean,aes(x=group,y=weight))+geom_bar(stat = "identity")
ggplot(pg_mean,aes(x=group,y=weight))+geom_bar(stat = "identity",width = 0.5)
ggplot(pg_mean,aes(x=group,y=weight))+geom_bar(stat = "identity",width = 1)
# 簇状条形图默认组内的条形间距为0。增加组内条形的间距,可通过将width设定小一些,并令position_dodge的取值大于width
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat = "identity",width = 0.5,position = "dodge")
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat = "identity",width = 0.5,position = position_dodge(0.7))
# position="dodge是参数默认为0.9的position_dodge()的简写
# 下面四个命令等价:
# geom_bar(position = "dodge")
# geom_bar(width = 0.9,position = position_dodge())
# geom_bar(position = position_dodge(0.9))
# geom_bar(width = 0.9,position = position_dodge(width = 0.9))


# 3.7 绘制堆积条形图
# 使用geom_bar()函数,并映射一个变量给填充色参数fill即可
library(gcookbook)
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat = "identity")
cabbage_exp
# 默认绘制的条形图,条形的堆积顺序与图例顺序相反,通过guides()函数对图例顺序进行调整,并制定图例所对应的需要调整的图形属性
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat = "identity")+guides(fill=guide_legend(reverse = TRUE))
# 调整条形的堆叠顺序,通过指定图形映射中的参数order=desc()来实现
library(plyr)
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar,order=desc(Cultivar)))+geom_bar(stat = "identity")
# 使用scale_fill_brewer()函数得到一个调色板,最后设定colour="black"为条形添加一个黑色边框线
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar,order=desc(Cultivar)))+geom_bar(stat = "identity",colour="black")+guides(fill=guide_legend(reverse = TRUE))+scale_fill_brewer(palette = "Pastel1")


# 3.8 绘制百分比堆积条形图
# 首先通过plyr包中的ddply()函数和transform()函数将每组条形对应的数据标准化为100%格式,之后,对计算结果绘制堆积条形图
library(gcookbook)
library(plyr)
# 以Date为切割变量()对每组数据进行transform()
ce <- ddply(cabbage_exp,"Date",transform,percent_weight=Weight/sum(Weight)*100)
ggplot(ce,aes(x=Date,y=percent_weight,fill=Cultivar))+geom_bar(stat = "identity")
cabbage_exp
ce
ggplot(ce,aes(x=Date,y=percent_weight,fill=Cultivar))+geom_bar(stat = "identity",colour="black")+guides(fill=guide_legend(reverse = TRUE))+scale_fill_brewer(palette = "Pastel1")


# 3.9 添加数据标签
# 在绘图命令中加上geom_text()即可为条形图添加数据标签
# 需要分别指定一个变量映射给x,y和标签本身,通过设定vjust(竖直调整数据标签位置)可以将标签位置移动至条形图顶端的上方或下方
library(gcookbook)
# 条形图顶端下方
ggplot(cabbage_exp,aes(x=interaction(Date,Cultivar),y=Weight))+geom_bar(stat = "identity")+geom_text(aes(label=Weight),vjust=1.5,colour="White")
# 条形图顶端上方
ggplot(cabbage_exp,aes(x=interaction(Date,Cultivar),y=Weight))+geom_bar(stat = "identity")+geom_text(aes(label=Weight),vjust=-0.2)
# 将y轴上限变大
ggplot(cabbage_exp,aes(x=interaction(Date,Cultivar),y=Weight))+geom_bar(stat = "identity")+geom_text(aes(label=Weight),vjust=-0.2)+ylim(0,max(cabbage_exp$Weight)*1.05)
# 设定标签的y轴位置使其略高于条形图顶端——y轴范围会自动调整
ggplot(cabbage_exp,aes(x=interaction(Date,Cultivar),y=Weight))+geom_bar(stat = "identity")+geom_text(aes(y=Weight+0.1,label=Weight))
# 对于簇状条形图,需设定position=position_dodge()并给其一个参数来设定分类间距
# 使用字号size来缩小数据标签的字体大小以匹配条形宽度。数据标签的默认字号是5
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat = "identity",position = "dodge")+geom_text(aes(label=Weight),vjust=1.5,colour="White",position = position_dodge(.9),size=3)
# 向堆积条形图添加数据标签之前,要先对每组条形对应的数据进行累积求和
library(plyr)
# 根据日期和性别对数据进行排序
ce <- arrange(cabbage_exp,Date,Cultivar)
# 计算累积和
ce <- ddply(ce,"Date",transform,label_y=cumsum(Weight))
ce
ggplot(ce,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity")+geom_text(aes(y=label_y,label=Weight),vjust=1.5,colour="White")
# 另一种修改堆叠顺序的方法是在标度中指定breaks参数
# 如果想把数据标签置于条形中部,需对累积求和的结果加以调整,并同时略去geom_bar()函数中对y偏移量(offset)的设置
ce <- arrange(cabbage_exp,Date,Cultivar)
# 计算y轴位置,将数据标签置于条形中部
ce <- ddply(ce,"Date",transform,label_y=cumsum(Weight)-0.5*Weight)
ggplot(ce,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat = "identity")+geom_text(aes(y=label_y,label=Weight),colour="White")


ggplot(ce,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat = "identity",colour="black")+geom_text(aes(y=label_y,label=paste(format(Weight,nsmall = 2),"Kg")),size=4)+guides(fill=guide_legend(reverse = TRUE))+scale_fill_brewer(palette = "Pastl1")


# 3.10 绘制Cleveland点图
# 直接运行geom_point()命令
library(gcookbook)
tophit <- tophitters2001[1:25,]
ggplot(tophit,aes(x=avg,y=name))+geom_point()
tophit[,c("name","lg","avg")]


ggplot(tophit,aes(x=avg,y=reorder(name,avg)))+geom_point(size=3)+theme_bw()+theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank(),panel.grid.major.y = element_line(colour = "grey60",linetype = "dashed"))


ggplot(tophit,aes(x=reorder(name,avg),y=avg))+geom_point(size=3)+theme_bw()+theme(axis.text.x = element_text(angle = 60,hjust = 1),panel.grid.major.y = element_blank(),panel.grid.minor.y = element_blank(),panel.grid.major.x = element_line(colour = "grey60",linetype = "dashed"))


# reorder()函数只能根据一个变量对因子水平进行排序
# 提取出name变量,依次根据变量lg和avg对其进行排序
nameorder <- tophit$name[order(tophit$lg,tophit$avg)]
# 将name转化为因子,因子水平与nameorder一致
tophit$name <- factor(tophit$name,levels = nameorder)
# geom_segment()函数用“以数据点为端点的线段"代替贯通全图的网格线
# geom_segment()函数需要设定x,y,xend,yend四个参数
ggplot(tophit,aes(x=avg,y=name))+geom_segment(aes(yend=name),xend=0,colour="grey50")+geom_point(size=3,aes(colour=lg))+scale_fill_brewer(palette = "Set1",limits=c("NL","AL"))+theme_bw()+theme(panel.grid.major.y =element_blank(),legend.position=c(1,0.55),legend.justification=c(1,0.5))
# 分面显示,通过调整lg变量的因子水平

ggplot(tophit,aes(x=avg,y=name))+geom_segment(aes(yend=name),xend=0,colour="grey50")+geom_point(size=3,aes(colour=lg))+scale_fill_brewer(palette = "Set1",limits=c("NL","AL"),guide=FALSE)+theme_bw()+theme(panel.grid.major.y =element_blank())+facet_grid(lg~.,scales = "free_y",space = "free_y")

# 折线图
# 4.1 绘制简单折线图
# 运行ggplot()函数和geom_line()函数,并分别指定一个变量映射给x和y
library(ggplot2)
ggplot(BOD,aes(x=Time,y=demand))+geom_line()
# 当x对应于因子型变量时,必须使用命令aes(group=1)以确保ggplot()知道这些数据属于同一个分组
BOD1 <- BOD
BOD1$Time <- factor(BOD1$Time)
ggplot(BOD1,aes(x=Time,y=demand,group=1))+geom_line()
# 默认情况ggplot2绘制的折线图y轴范围刚好能容纳数据集中的y值
# 运行ylim()设定y轴范围或者运行含一个参数的expand_limit()扩展y轴的范围。
ggplot(BOD,aes(x=Time,y=demand))+geom_line()+ylim(0,max(BOD$demand))
ggplot(BOD,aes(x=Time,y=demand))+geom_line()+expand_limits(y=0)


# 4.2 向折线图添加数据标记
# 在代码中加上geom_point()
ggplot(BOD,aes(x=Time,y=demand))+geom_line()+geom_point()
library(gcookbook)
ggplot(worldpop,aes(x=Year,y=Population))+geom_line()+geom_point()
ggplot(worldpop,aes(x=Year,y=Population))+geom_line()+geom_point()+scale_y_log10()


# 4.3 绘制多重折线图
# 在分别设定一个映射给x和y的基础上,再将另外一个(离散型)变量映射给颜色(colour)或者线型(linetype)即可
# 载入plyr包,便于使用ddply()函数创建样本数据集
library(plyr)
# 对ToothGrowth数据集进行汇总
tg <- ddply(ToothGrowth,c("supp","dose"),summarise,length=mean(len))
tg
# 将supp映射给颜色(colour)
ggplot(tg,aes(x=dose,y=length,colour=supp))+geom_line()
# 将supp映射给线型(linetype)
ggplot(tg,aes(x=dose,y=length,linetype=supp))+geom_line()
str(tg)
ggplot(tg,aes(x=factor(dose),y=length,colour=supp,group=supp))+geom_line()
# 注意,不可缺少group=supp语句,否则,ggplot()会不知如何将数据组合在一起绘制折线图
# 分组不正确时会遇见另一种问题:折线图呈锯齿状
ggplot(tg,aes(x=dose,y=length))+geom_line()
# 原因在于x在每个位置对应于多个点,ggplot()误以为这些点属于同一组数据而将其用一根折线相连。
# 如果折线图上有数据标记,则可以将分组变量映射给数据标记的属性,如shape和fill等
ggplot(tg,aes(x=dose,y=length,shape=supp))+geom_line()+geom_point(size=4) #更大的点
ggplot(tg,aes(x=dose,y=length,fill=supp))+geom_line()+geom_point(size=4,shape=21) # 使用有填充色的点
# 数据标记会相互重叠,需要令其彼此错开
# 将连接线左右移动0.2,将点的位置左右移动0.2
ggplot(tg,aes(x=dose,y=length,shape=supp))+geom_line(position = position_dodge(0.2))+geom_point(position = position_dodge(0.2),size=4)


# 4.4 修改线条样式
# 通过设置线型(linetype),线宽(size)和颜色(colour)参数可以分别修改折线的线型、线宽和颜色
# 通过将这些参数的值传递给geom_line()函数可以设置折线图的对应属性
ggplot(BOD,aes(x=Time,y=demand))+geom_line(linetype="dashed",size=1,colour="blue")


# 使用其他调色板为图形着色,可以调用scale_colour_brewer()和scale_colour_manual()函数
# 加载plyr包
library(plyr)
# 对ToothGrowth数据集进行汇总
tg <- ddply(ToothGrowth,c("supp","dose"),summarise,length=mean(len))
ggplot(tg,aes(x=dose,y=length,colour=supp))+geom_line()+scale_color_brewer(palette = "Set1")
# 如果两条折线的图形属性相同,需要指定一个分组变量
ggplot(tg,aes(x=dose,y=length,group=supp))+geom_line(colour="darkgreen",size=1.5)
# 因为变量supp被映射给了颜色(colour)属性,所以,它自动作为分组变量
ggplot(tg,aes(x=dose,y=length,colour=supp))+geom_line(linetype="dashed")+geom_point(shape=22,size=3,fill="white")


# 4.5修改数据标记样式
# 在函数aes()外部设定函数geom_point()的大小(size)、颜色(colour)和填充色(fill)
ggplot(BOD,aes(x=Time,y=demand))+geom_line()+geom_point(size=4,shape=22,colour="darkred",fill="pink")
# 数据标记默认的形状(shape)是实线圆圈,默认的大小(size)是2,默认的颜色(colour)是黑色(black),填充色(fill)属性只适用于某些(标号21-25)具有独立边框线和填充颜色的点型。
# fill一般取空值或者NA。将填充色设定为白色可以得到一个空心圆
ggplot(BOD,aes(x=Time,y=demand))+geom_line()+geom_point(size=4,shape=21,fill="white")


# 如果要将数据标记和折线设定为不同的颜色,必须在折线绘制完毕后再行设定数据标记的颜色
# 此时,数据标记被绘制在更上面的图层,从而,避免被折线遮盖
# 通过aes()函数内部将分组变量映射给数据标记的图形属性可以将多条折线设定为不同的颜色。
# 使用scale_colour_brewer()函数和scale_colour_manual()函数可以修改默认颜色
# 在aes()函数外部设定shape和size可以将数据标记设定为统一的形状和颜色
# 载入plyr包
library(plyr)
# 对ToothGrowth数据集进行汇总
tg <- ddply(ToothGrowth,c("supp","dose"),summarise,length=mean(len))
# 保存错开(dodge)设置,接下来会多次用的
pd <- position_dodge(0.2)
ggplot(tg,aes(x=dose,y=length,fill=supp))+geom_line(position = pd)+geom_point(shape=21,size=3,position = pd)+scale_fill_manual(values = c("black","white"))


# 4.6绘制面积图
# 运行geom_area()函数即可绘制面积图
# 将sunspot.year数据集转化为数据框
sunspotyear <- data.frame(
  Year = as.numeric(time(sunspot.year)),
  Sunspots = as.numeric(sunspot.year)
)
ggplot(sunspotyear,aes(x=Year,y=Sunspots))+geom_area()
# 默认情况下,面积图的填充色为黑灰色且没有边框线,通过设定填充色(fill)可以修改面积图的填充色
# 通过设定alpha=0.2将面积图的透明度设定为80%
# 通过设定颜色(colour)可以为面积图添加边框线
ggplot(sunspotyear,aes(x=Year,y=Sunspots))+geom_area(colour="black",fill="blue",alpha=.2)
# 绘制不带边框线的面积图(不设定colour),用geom_line()函数绘制轨迹线
ggplot(sunspotyear,aes(x=Year,y=Sunspots))+geom_area(fill="blue",alpha=.2)+geom_line()
# 4.7 绘制堆积面积图
# 运行geom_area()函数,并映射一个因子型变量给填充色(fill)即可
library(gcookbook)
ggplot(uspopage,aes(x=Year,y=Thousands,fill=AgeGroup))+geom_area()
# 堆积面积图对应的基础数据通常为宽格式(wide format),但ggplot2要求数据必须是长格式(long format)
# 默认情况下图例的堆积顺序与面积图的堆积顺序是相反的,通过设定标度中的切分(breaks)参数可以翻转堆积顺序
ggplot(uspopage,aes(x=Year,y=Thousands,fill=AgeGroup))+geom_area(colour="black",size=.2,alpha=.4)+scale_fill_brewer(palette = "Blues",breaks=rev(levels(uspopage$AgeGroup)))
# 在aes()函数内部设定order=desc(AgeGroup)可以对堆积面积图的堆积顺序进行反转
library(plyr) # 为了使用desc()函数
ggplot(uspopage,aes(x=Year,y=Thousands,fill=AgeGroup,order=desc(AgeGroup)))+geom_area(colour="black",size=.2,alpha=.4)+scale_fill_brewer(palette = "Blues")
# 因为堆积面积图中的各个部分是由多边形构成的,因此其具有左、右框线,这样的绘图效果差强人意且可能产生误导效果
# 为了对此进行修正,先绘制一个不带边框线的堆积面积图(将colour设定为默认的NA值),然后,在其顶部添加geom_line()
ggplot(uspopage,aes(x=Year,y=Thousands,fill=AgeGroup,order=desc(AgeGroup)))+geom_area(colour=NA,alpha=.4)+scale_fill_brewer(palette = "Blues")+geom_line(position = "stack",size=.2)


# 4.8 绘制百分比堆积面积图
# 首先计算各组对应的百分比
library(gcookbook)
library(plyr)
# 将Thousands转化为Percent
uspopage_prop <- ddply(uspopage,"Year",transform,Percent=Thousands/sum(Thousands)*100)
ggplot(uspopage_prop,aes(x=Year,y=Percent,fill=AgeGroup))+geom_area(colour="black",size=.2,alpha=.4)+scale_fill_brewer(palette = "Blues",breaks=rev(levels(uspopage$AgeGroup)))
uspopage


# 4.9 添加置信域
# 运行geom_ribbon(),然后分别映射一个变量给ymin和ymax
library(gcookbook)
# 抓取climate数据集的一个子集
clim <- subset(climate,Source=="Berkeley",select = c("Year","Anomaly10y","Unc10y"))
clim
# 将置信域绘制为阴影
ggplot(clim,aes(x=Year,y=Anomaly10y))+geom_ribbon(aes(ymin=Anomaly10y-Unc10y,ymax=Anomaly10y+Unc10y),alpha=0.2)+geom_line()
# 使用虚线来表示置信域的上下边界
ggplot(clim,aes(x=Year,y=Anomaly10y))+geom_line(aes(y=Anomaly10y-Unc10y),colour="grey50",linetype="dotted")+geom_line(aes(y=Anomaly10y+Unc10y),colour="grey50",linetype="dotted")+geom_line()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

镰刀韭菜

看在我不断努力的份上,支持我吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值