目錄
- 0.問(wèn)題導(dǎo)入
- 1.示例數(shù)據(jù)
- 2.導(dǎo)入數(shù)據(jù)
- 3.解決R起初報(bào)錯(cuò)(圖1)
- 4.設(shè)置日期及x軸breaks的位置與標(biāo)注
- 5.將右側(cè)兩列子圖x軸時(shí)間轉(zhuǎn)化為年份(圖2)
- 6.自定義各子圖軸名(圖3)
- 7.總結(jié)
- 8.本文所用軟件包(沒(méi)有需要通過(guò)install.packages('')進(jìn)行安裝)
- 9.致謝
0. 問(wèn)題導(dǎo)入
日常繪圖中淹辞,我們可能想著把相關(guān)性分析圖與兩種指標(biāo)的時(shí)間演進(jìn)圖通過(guò)facet_wrap繪制到一塊。然后俘侠,R就開始報(bào)錯(cuò)了(如下...R的內(nèi)心OS其實(shí)是:x軸你到底是想要字符類型還是日期類型跋笞骸?搞么子耙佟央星?要上天嗎?)不不不惫东,我只是不想后期進(jìn)AI莉给。那么應(yīng)該怎么辦呢?今天這篇給出解決方案~
Error in as.Date.numeric(value) : 'origin'一定得給值
1. 示例數(shù)據(jù)(點(diǎn)擊下載示例數(shù)據(jù))
相關(guān)性分析所需數(shù)據(jù)
時(shí)間序列數(shù)據(jù)
2. 導(dǎo)入數(shù)據(jù)
setwd('L:\\JianShu\\20191223')
pl_df_cor = read.csv('pl_df_cor.csv',header = T)
pl_df_line = read.csv('pl_df_line.csv',header = T)
pl_df_cor = pl_df_cor[,-1]
pl_df_line = pl_df_line[,-1]
pl_df_cor = as.data.frame(pl_df_cor)
pl_df_line = as.data.frame(pl_df_line)
pl_df_line$Date = as.Date(pl_df_line$Date)
3. 解決R起初報(bào)錯(cuò)(圖1)
R起初報(bào)錯(cuò)的原因呢就是在于左側(cè)兩列與右側(cè)兩列x軸的label類型不同廉沮。我們?cè)诘谝徊酵ㄟ^(guò)將Date 轉(zhuǎn)換為 as.numeric(Date)颓遏,即numeric類型后,x軸的label類型得以統(tǒng)一滞时,故Bug排除叁幢,R不再報(bào)錯(cuò)。
但是坪稽,圖1 是不是看著很別扭曼玩。鳞骤。。好好的時(shí)間怎么變成了11800演训,這是什么鬼弟孟?
還有就是,各個(gè)子圖的橫軸样悟,縱軸并都是分別表示的Index與日期拂募,而圖1 中只是給出了圖統(tǒng)一的橫/縱軸名稱,這樣顯然是不妥的窟她!
那么如何解決呢陈症?本篇第4-6節(jié)逐一解答
p1 = ggplot()+
geom_point(data = pl_df_cor,
aes(x = IndexA, y = value),shape = 1, color = 'blue')+
geom_line(data = pl_df_line,
aes(x = as.numeric(Date), y = value, color = variable),size = 1)+
#scale_x_continuous(breaks = c(seq(-1,1,0.25),as.numeric(date)[date_breaks]),
# labels = c(as.character(seq(-1,1,0.25)),date_labels[date_breaks]))+
scale_color_manual(values = c('blue','green'))+
theme_bw()+
theme(
axis.text = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5),
axis.title = element_text(face = 'bold',color = 'black',size = 14,hjust = 0.5),
legend.text = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5),
legend.title = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5),
legend.position = 'bottom',
legend.direction = 'horizontal',
strip.background = element_blank(),
strip.placement = 'outside',
strip.text = element_blank()
)+
ylab("Index")+
xlab("Date")+
facet_wrap(~type,scales = 'free',nrow = 2)
png('plot1.png',
height = 20,
width = 20,
units = 'cm',
res = 800)
print(p1)
dev.off()
4. 設(shè)置日期及x軸breaks的位置與標(biāo)注
date = seq(as.Date('2002-01-01'),as.Date('2003-12-01'),'1 month')
date_breaks = c(seq(1,24,10))
date_labels = as.character(date)
5. 將右側(cè)兩列子圖x軸時(shí)間轉(zhuǎn)化為年份(圖2)
p2 = ggplot()+
geom_point(data = pl_df_cor,
aes(x = IndexA, y = value),shape = 1, color = 'blue')+
geom_line(data = pl_df_line,
aes(x = as.numeric(Date), y = value, color = variable),size = 1)+
scale_x_continuous(breaks = c(seq(-1,1,0.25),as.numeric(date)[date_breaks]),
labels = c(as.character(seq(-1,1,0.25)),date_labels[date_breaks]))+
scale_color_manual(values = c('blue','green'))+
theme_bw()+
theme(
axis.text = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5),
axis.title = element_text(face = 'bold',color = 'black',size = 14,hjust = 0.5),
legend.text = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5),
legend.title = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5),
legend.position = 'bottom',
legend.direction = 'horizontal',
strip.background = element_blank(),
strip.placement = 'outside',
strip.text = element_blank()
)+
ylab("Index")+
xlab("Date")+
facet_wrap(~type,scales = 'free',nrow = 2)
png('plot2.png',
height = 20,
width = 20,
units = 'cm',
res = 800)
print(p2)
dev.off()
6. 自定義各子圖軸名(圖3)
p3 = ggplot()+
geom_point(data = pl_df_cor,
aes(x = IndexA, y = value),shape = 1, color = 'blue')+
geom_line(data = pl_df_line,
aes(x = as.numeric(Date), y = value, color = variable),size = 1)+
scale_x_continuous(breaks = c(seq(-1,1,0.25),as.numeric(date)[date_breaks]),
labels = c(as.character(seq(-1,1,0.25)),date_labels[date_breaks]))+
scale_color_manual(values = c('blue','green'))+
theme_bw()+
theme(
axis.text = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5),
axis.title = element_text(face = 'bold',color = 'black',size = 14,hjust = 0.5),
legend.text = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5),
legend.title = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5),
legend.position = 'bottom',
legend.direction = 'horizontal',
strip.background = element_blank(),
strip.placement = 'outside',
strip.text = element_text(face = 'bold',color = 'black',size = 14,hjust = 0.5)
)+
ylab('')+
xlab('IndexA Date')+
facet_wrap(~type,scales = 'free',nrow = 2,
strip.position = 'left',
labeller = as_labeller(
c("1"="IndexB", "2" = "Indices", "3" = "IndexB", "4"= "Indices")))
png('plot3.png',
height = 20,
width = 20,
units = 'cm',
res = 800)
print(p3)
dev.off()
7. 總結(jié)
本篇主要解決了以下3個(gè)問(wèn)題:
1. 如何將具有不同類型x/y軸名的子圖通過(guò)geom_facet 繪制在一起?如連續(xù)數(shù)字與時(shí)間震糖?
2. 如何將轉(zhuǎn)化為numeric類型后的日期以正確形式表示录肯?
3. 如何自定義各子圖x/y軸軸名?
8. 本文所用軟件包(沒(méi)有需要通過(guò)install.packages('')進(jìn)行安裝)
library('ggplot2')
9. 致謝
首先吊说,感謝大家的持續(xù)關(guān)注论咏,小編會(huì)繼續(xù)努力,持續(xù)更新下去的颁井!
大家如果覺(jué)得有用厅贪,還麻煩大家轉(zhuǎn)發(fā)點(diǎn)贊加關(guān)注哈,也可以擴(kuò)散到朋友圈雅宾,多謝大家啦~
大家如果在使用本文代碼的過(guò)程有遇到問(wèn)題的养涮,可以留言評(píng)論,也可以私信我哈~~