library(ggplot2) library(XML) weather<-read.table(text = "",col.names = c("date","time","pressure","precipitation","temperature","wind_velocity","wind_direction")) year<-2015;month<-1 for (i in 1:31){ html.name <-paste("http://www.data.jma.go.jp/obd/stats/etrn/view/hourly_s1.php?prec_no=69&block_no=47746&year=",year,"&month=",month,"&day=",i ,sep="",collapse="") t1<-readHTMLTable(html.name) t2<-t1$tablefix1[,c(1,2,4,5,9,10)] t2<-t2[-1,] date<-paste(year,"/",month,"/",i,sep="",collapse="") d1<- data.frame(matrix(rep(date,24),ncol=1)) t2<-cbind(d1,t2) names(t2)<-c("date","time","pressure","precipitation","temperature","wind_velocity","wind_direction") t2[,2]<-as.numeric(as.vector(t2[,2])) t2[,3]<-as.numeric(as.vector(t2[,3])) t2[,4]<-as.numeric(as.vector(t2[,4])) t2[,5]<-as.numeric(as.vector(t2[,5])) t2[,6]<-as.numeric(as.vector(t2[,6])) weather<-rbind(weather,t2) } wind<-data.frame(direction=as.vector(chartr("東西南北", "EWSN",weather$wind_direction)),velocity=as.numeric(as.vector(weather$wind_velocity))) wind<- subset(wind,direction=="N"|direction=="NNE"|direction=="NE"|direction=="ENE"|direction=="E"|direction=="ESE"|direction=="SE"|direction=="SSE"|direction=="S"|direction=="SSW"|direction=="SW"|direction=="WSW"|direction=="W"|direction=="WNW"|direction=="NW"|direction=="NNW") ord<-c("N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW") wind$direction<- factor(wind$direction,levels=ord) wind$cut<-cut(wind$velocity,breaks=c(0,3,6,10,20,100),labels=c("[0,3]","(3,6]","(6,10]","(10,20]" ,"(20, ") ,right =TRUE, include.lowest = TRUE) table(wind$cut,wind$direction) dummy<-data.frame(direction=levels(wind$direction),velocity=rep(NA,16)) newwind<-rbind(wind[,c("direction","velocity")],dummy) ord<-c("N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW") newwind$direction<- factor(newwind$direction,levels=ord) newwind$cut<-cut(newwind$velocity,breaks=c(0,3,6,10,20,100),labels=c("[0,3]","(3,6]","(6,10]","(10,20]" ,"(20, ") ,right =TRUE, include.lowest = TRUE) ggplot(newwind, aes(x =factor(direction),fill = cut)) + geom_bar(width =0.8) + coord_polar(theta = "x",start = -0.2) + scale_fill_brewer(palette = "Set2") + ylim(0,170) + theme(legend.position="bottom")+ labs(fill = "Velocity",x="Direction",y="",title=paste0("風配図(鳥取:",year,"年",month,"月)"))
|