使用ggplot2繪製折線圖.
折線圖整體美化.
繪製平滑折線圖.
繪製多變量折線圖.
library(tidyverse)
library(reshape2)
df=data.frame( 年份= seq(2006,2015), 牛肉=scale(c(44.72, 59.27, 44.32, 80.6, 119.59, 109.09, 61.21, 95.5, 79.32, 64.16)), 豬肉=scale(c(322.24,422.69,325.39, 295.04, 326.1, 332.01, 262.72, 275.65, 283.75, 400.88)) ) %>%
melt(id.vars='年份',variable.name='種類',value.name='值')
print(head(df))
ggplot(aes(x=年份, y=值))構建畫板, 然後用geom_line()在畫板上繪折線圖.
df%>%filter(種類=='牛肉')%>% ggplot(aes(x=年份, y=值))+ geom_line()+ labs(title='折線圖', subtitle=R.version.string, caption='@數萃大數據')
整體美化包含了畫板背景、繪圖區背景、坐標、注釋、標題的調整,以及前一節中的添加點標籤.p=df%>%filter(種類=='牛肉')%>% ggplot(aes(x=年份, y=值))+ geom_line(linetype=2,colour='white')+ labs(title='折線圖', subtitle=R.version.string, caption='@數萃大數據')+ scale_x_continuous(breaks = seq(2006,2015), labels = seq(2006,2015))+ scale_y_continuous(breaks = round(seq(min(df$值), max(df$值)+0.5, 0.6),2), labels = round(seq(min(df$值), max(df$值)+0.5, 0.6),2), limits = c(min(df$值), max(df$值)+0.5))+
geom_point(colour='red')+
geom_text(aes(x=年份, y=值, label=round(值,2)), colour='yellow',hjust=0.5,vjust=-1)+ theme(
plot.background = element_rect(fill = 'gray20'),
panel.background = element_rect(fill = 'gray20'),
panel.grid = element_blank(),
title = element_text(face = 'bold', size = 8,colour ='orange'),
plot.title = element_text(face = 'bold', size = 16,colour ='orange'),
plot.subtitle = element_text(face = 'bold', size = 8,colour ='orange'),
plot.caption = element_text(face = 'bold', size = 7, color = 'orange'),
axis.line = element_line(colour = 'white'),
axis.text = element_text(face = 'bold',colour = "white"),
axis.ticks = element_line(colour = 'white') )p
p+geom_smooth(span=0.2,colour='white',size=0.5, method = 'loess')
繪製多變量折線圖的方法和單變量類似,主要區別是將geom_line()中的linetype參數賦值給分組變量,此外也可以付給其他美學對象如colour, size等.df%>% ggplot(aes(x=年份, y=值))+ geom_line(aes(linetype=種類),colour='white',size=0.5)+ labs(title='折線圖', subtitle=R.version.string, caption='@數萃大數據')+ scale_x_continuous(breaks = seq(2006,2015), labels = seq(2006,2015))+ scale_y_continuous(breaks = round(seq(min(df$值), max(df$值)+0.5, 0.6),2), minor_breaks = round(seq(min(df$值), max(df$值)+0.5, 0.1),2), labels = round(seq(min(df$值), max(df$值)+0.5, 0.6),2), limits = c(min(df$值), max(df$值)+0.5))+ geom_point(colour='red')+ geom_text(aes(x=年份, y=值, label=round(值,2)), colour='yellow',hjust=0.5,vjust=-1,size=3)+ theme( plot.background = element_rect(fill = 'gray20'),
panel.background = element_rect(fill = 'gray20'),
panel.grid = element_blank(),
title = element_text(face = 'bold', size = 8,colour ='orange'),
plot.title = element_text(face = 'bold', size = 16,colour ='orange'),
plot.subtitle = element_text(face = 'bold', size = 8,colour ='orange'),
plot.caption = element_text(face = 'bold', size = 7, color = 'orange'),
axis.line = element_line(colour = 'white'),
axis.text = element_text(face = 'bold',colour = "white"), axis.ticks = element_line(colour = 'white'), legend.position ='top', legend.justification = c("right", "top"), legend.box.background = element_rect(color = 'gray20'), legend.background = element_rect(fill = 'gray20'),
legend.key = element_rect(fill = 'gray20', colour = "gray20"), legend.text = element_text(size = 8, colour = "white"),
legend.title = element_text(size = 8,face='bold') )
推薦閱讀
微課|ggplot2: 基礎知識
微課|R基礎繪圖:條形圖
微課|R基礎繪圖:箱線圖
【Python微課】系列之一:Matplotlib基礎繪圖
【統計思想之十六】—— 古人學問無遺力,少壯工夫老始成。
易圖秒懂の人工智慧誕生
【案例分析】NBA球員投籃數據分析與可視化
更多微課請關注【數萃大數據】公眾號,點擊學習園地—可視化
歡迎大家關注微信公眾號:數萃大數據
python大數據分析培訓班【杭州站】
時間:2017年8月18日-22日
地點:杭州亞朵吳酒店
更多詳情,請掃描下面二維碼
網絡爬蟲與文本挖掘培訓班【寧波站】
時間:2017年9月23日-25日
地點:維也納國際酒店(機場店)
更多詳情,請掃描下面二維碼