R绘图基础指南 | 2.折线图(下)

简介: R绘图基础指南 | 2.折线图

2.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()


image.png


# 颜色、透明度设置
ggplot(sunspotyear, aes(x = Year, y = Sunspots)) + 
  geom_area(colour = "black",fill = "blue", alpha = 0.2)


image.png


# 去掉底部横线 不设定colour,使用geom_line()绘制轨迹
ggplot(sunspotyear, aes(x = Year, y = Sunspots)) + 
  geom_area(fill = "blue",alpha = 0.2) + 
  geom_line()


image.png


2.7 绘制堆积面积图


library(gcookbook) 
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) + geom_area()

image.png


head(uspopage)
> head(uspopage)
  Year AgeGroup Thousands
1 1900       <5      9181
2 1900     5-14     16966
3 1900    15-24     14951
4 1900    25-34     12161
5 1900    35-44      9273
6 1900    45-54      6437
# 通过设定breaks翻转堆积顺序
# 透明度、颜色、大小设置
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) + 
  geom_area(colour = "black", size = 0.2, alpha = 0.4) + 
  scale_fill_brewer(palette = "Blues", breaks = rev(levels(uspopage$AgeGroup)))


image.png

# 设定order = desc(AgeGroup) 可以对堆积顺序进行反转
library(plyr)  
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup, order = desc(AgeGroup))) +     
  geom_area(colour = "black", size = 0.2, alpha = 0.4) + 
  scale_fill_brewer(palette = "Blues")


image.png

# 去掉框线
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup, order = desc(AgeGroup))) +     
  geom_area(colour = NA, alpha = 0.4) + 
  scale_fill_brewer(palette = "Blues") +     
  geom_line(position = "stack", size = 0.2)


image.png


2.8 绘制百分比面积堆积图


# 先计算百分比
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 = 0.2, alpha = 0.4) + 
  scale_fill_brewer(palette = "Blues", breaks = rev(levels(uspopage$AgeGroup)))


网络异常,图片无法展示
|


head(uspopage)
> head(uspopage)
  Year AgeGroup Thousands
1 1900       <5      9181
2 1900     5-14     16966
3 1900    15-24     14951
4 1900    25-34     12161
5 1900    35-44      9273
6 1900    45-54      6437
uspopage_prop <- ddply(uspopage, "Year", transform, Percent = Thousands/sum(Thousands) * 100)

2.9 添加置信域


运行 geom_ribbon()分别映射一个变量给 ymin 和 ymax。


climate数据集中的Anomaly10y变量表示了各年温度相对于1950-1980平均水平变异的10年移动平均。Unc10y表示其95%置信区间。


library(gcookbook) 
# 抓取 climate 数据的一个子集
clim <- subset(climate, Source == "Berkeley", select = c("Year", "Anomaly10y",     "Unc10y"))
head(clim)
> head(clim)
  Year Anomaly10y Unc10y
1 1800     -0.435  0.505
2 1801     -0.453  0.493
3 1802     -0.460  0.486
4 1803     -0.493  0.489
5 1804     -0.536  0.483
6 1805     -0.541  0.475
# 将置信域绘制为阴影
# 注意一下图层的顺序
ggplot(clim, aes(x = Year, y = Anomaly10y)) + 
  geom_ribbon(aes(ymin = Anomaly10y - Unc10y, ymax = Anomaly10y + Unc10y), alpha = 0.2) + 
  geom_line()


image.png

# 使用虚线表示置信域的上下边界
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()

image.png


参考书籍

R Graphics Cookbook, 2nd edition.


相关文章
|
3月前
|
数据可视化 Python
Plotly:绘制蜡烛图
Plotly:绘制蜡烛图
59 0
|
3月前
|
图形学
利用Graphics画出一幅图表绘制折线图
("某工厂某产品年度销售额图表",this.Font, Brushes.Black, new Point(420,14)); pen.Dispose();
27 0
|
6月前
|
Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-2
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
|
6月前
|
数据可视化 开发者 Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-1
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
R语言笔记丨绘图基础知识:饼图、条形图
R语言笔记丨绘图基础知识:饼图、条形图
|
7月前
|
存储 数据可视化
使用 plotly 绘制旭日图
使用 plotly 绘制旭日图
331 0
echart渐变折线图,立体柱状图,发光饼图,月份图等
echart渐变折线图,立体柱状图,发光饼图,月份图等
|
数据可视化 数据处理
R绘图案例|基于分面的折线图绘制
R绘图案例|基于分面的折线图绘制
301 0
|
开发者
pyecharts基础之柱状图的绘制
pyecharts分为v0.5.X和v1两个大版本,0.5.x 版本将不再进行维护推荐使用v1版本
114 0
|
数据可视化 数据挖掘 定位技术
pyecharts绘制条形图、饼图、散点图、词云图、地图等常用图形(一)
pyecharts绘制条形图、饼图、散点图、词云图、地图等常用图形
359 0
pyecharts绘制条形图、饼图、散点图、词云图、地图等常用图形(一)