R-ggplot2-繪圖時(shí)x軸一種是numeric,一種是date?分面時(shí)老報(bào)錯(cuò)?怎么搞??

目錄

  • 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()
圖1 解決報(bào)錯(cuò)后的初始圖

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()
圖2 將右側(cè)兩列子圖x軸時(shí)間轉(zhuǎn)化為年份

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

圖3 自定義各子圖軸名

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)論,也可以私信我哈~~


小編聯(lián)系方式
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容