2.1 绘制简单折线图
library(ggplot2) ggplot(BOD, aes(x = Time, y = demand)) + geom_line()
BOD
## Time demand ## 1 1 8.3 ## 2 2 10.3 ## 3 3 19.0 ## 4 4 16.0 ## 5 5 15.6 ## 6 7 19.8
BOD1 <- BOD # Make a copy of the data BOD1$Time <- factor(BOD1$Time) #转为因子型变量 ggplot(BOD1, aes(x = Time, y = demand, group = 1)) + geom_line()
数据集BOD中没有对应于Time=6的数据点,因此Time被转化为因子型变量时,它并没有6这个水平。
可以运行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)
2.2 向折线图添加数据表记
ggplot(BOD, aes(x = Time, y = demand)) + geom_line() + geom_point()
library(gcookbook) # wordlpop 对应的采集时间间隔不是常数。时间越近的采集频率越高。 ggplot(worldpop, aes(x = Year, y = Population)) + geom_line() + geom_point()
# 当y轴取对数时也一样 ggplot(worldpop, aes(x = Year, y = Population)) + geom_line() + geom_point() + scale_y_log10()
2.3 绘制多重折线图
# 载入plyr,便于使用ddply() 创建样本数据集 library(plyr) # 汇总ToothGrowth 数据集 tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length=mean(len)) # 将 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()
# ggplot(tg, aes(x=factor(dose), y=length, colour=supp, group=supp)) + geom_line()
# 不能缺失group=supp语句,否则ggplot()会不知如何将数据组合在一起,从而报错 ggplot(tg, aes(x=factor(dose), y=length, colour=supp)) + geom_line()
plot of chunk unnamed-chunk-3
# 分组不正确也有可能变成锯齿状 ggplot(tg, aes(x=dose, y=length)) + geom_line()
plot of chunk unnamed-chunk-3
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) #使用有填充色的点
# 数据标记相互重叠,需要相应的移动标记点以及连接线。 ggplot(tg, aes(x=dose, y=length, shape=supp)) + geom_line(position=position_dodge(0.2)) +#将连接线左右移动0.2 geom_point(position=position_dodge(0.2), size=4) # 将点的位置左右移动0.2
2.4 修改线条样式
通过设置线型(linetype)、线宽(size) 和颜色(colour)参数可以分别修改折现的线型、线宽和颜色。
ggplot(BOD, aes(x = Time, y = demand)) + geom_line(linetype = "dashed", size = 1, colour = "blue")
library(plyr) tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length = mean(len)) ggplot(tg, aes(x = dose, y = length, colour = supp)) + geom_line() + scale_colour_brewer(palette = "Set1"))
# 在aes()函数外部设定参数则会对所有折线图有效 ggplot(tg, aes(x = dose, y = length, group = supp)) + geom_line(colour = "darkgreen", size = 1.5)
# supp被映射给了颜色,所以自动作为分组变量 ggplot(tg, aes(x = dose, y = length, colour = supp)) + geom_line(linetype = "dashed") + geom_point(shape = 22, size = 3, fill = "white")
2.5 修改数据标记样式
# geom_point()设置点大小、颜色、填充 ggplot(BOD,aes(x = Time,y = demand)) + geom_line() + geom_point(size = 4,shape = 22,colour = "darkred",fill = "pink")
ggplot(BOD, aes(x = Time, y = demand)) + geom_line() + geom_point(size = 4,shape = 21, fill = "white")
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"))