R语言

R语言可视化练习之ggridges和awtools

2018-10-26  本文已影响57人  热衷组培的二货潜

参考实验:https://austinwehrwein.com/data-visualization/weather/

library(tidyverse)
library(ggridges)
# library(devtools)
# install_github("awhstin/awtools") 
# 报错,连不上,采用本地安装
#Installation failed: Timeout was reached: Connection timed out after 10000 milliseconds
# github链接 https://github.com/awhstin/awtools
devtools::install_local("D://awtools-master/",,force = TRUE)
library(awtools)

#import from github removing a field that doesn't exist in current year
ohare15<-read_csv('https://raw.githubusercontent.com/awhstin/temperaturesv2/master/OHARE-NCDC-2015.csv') %>% select(-c(5))
ohare16<-read_csv('https://raw.githubusercontent.com/awhstin/temperaturesv2/master/OHARE-NCDC-2016.csv') %>% select(-c(5))
ohare17<-read_csv('https://raw.githubusercontent.com/awhstin/temperaturesv2/master/OHARE-NCDC-2017.csv') %>% select(-c(5))
ohare18<-read_csv('https://raw.githubusercontent.com/awhstin/temperaturesv2/master/OHARE-NCDC-2018.csv')

ohare<-ohare15 %>%
  union(.,ohare16) %>%
  union(.,ohare17) %>%
  union(.,ohare18) %>%
  mutate(year=format(DATE,'%Y'),
         month=factor(months(DATE), levels=rev(month.name)),
         TAVG=ifelse(is.na(TAVG),(TMAX+TMIN)/2,TAVG)) 

这里有一个巨坑,应该是由于我的系统是中文的,所以months()这个函数输出的结果是中文的,然而R里面month.name是英文的,所以会导致month变量那一列全为NA,解决办法如下
R语言在中文系统下使用month()函数输出结果的巨坑

> weekdays(Sys.Date()+0:6)
[1] "星期五" "星期六" "星期日" "星期一" "星期二" "星期三" "星期四"
> Sys.Date()
[1] "2018-10-26"
> Sys.setlocale("LC_TIME", "English")
[1] "English_United States.1252"
> weekdays(Sys.Date()+0:6)
[1] "Friday"    "Saturday"  "Sunday"    "Monday"    "Tuesday"   "Wednesday" "Thursday" 

好了,坑跳过去了就继续下一步学习

ggplot(ohare, aes(x=TAVG, y=month, fill=year)) +
  geom_density_ridges(scale=.9, color=NA) +
  a_plex_theme(grid=FALSE) +
  scale_fill_manual(values=c('#d6d6d6','#adadad','#707070','#333333')) +
  labs(title='Annual Temperature Variablity: O\'Hare', 
       subtitle='Changes in temperature at O\'Hare Airport from January 1st 2015 to September 28 2018 ',
       caption='Data from the NCDC\nhttps://www.ncdc.noaa.gov/',
       x='Temperature (F)',
       y='Month')
image.png
lax<-read_csv('https://raw.githubusercontent.com/awhstin/temperaturesv2/master/LAX-NCDC-2015-18.csv') %>%
  mutate(year=format(DATE,'%Y'),
         month=factor(months(DATE), levels=rev(month.name)),
         TAVG=ifelse(is.na(TAVG),(TMAX+TMIN)/2,TAVG)) 

image.png

get到的新技能就是awtools这个包了

 a_plex_theme
上一篇下一篇

猜你喜欢

热点阅读