简体   繁体   中英

how to plot scatterplot and histogram using R

I have a dataset about maximum wind speed(cm) as below:

Year Jan Feb Mar Apr May June July Aug Sept Oct Nov Dec     
2011 4.5 5.6 5.0 5.4 5.0 5.0  5.2  5.3 4.8  5.4 5.4 3.8     
2010 4.6 5.0 5.8 5.0 5.2 4.5  4.4  4.3 4.9  5.2 5.2 4.6     
2009 4.5 5.3 4.3 3.9 4.7 5.0  4.8  4.7 4.9  5.6 4.9 4.1     
2008 3.8 1.9 5.6 4.7 4.7 4.3  5.9  4.9 4.9  5.6 5.2 4.4     
2007 4.6 4.6 4.6 5.6 4.2 3.6  2.5  2.5 2.5  3.3 5.6 1.5     
2006 4.3 4.8 5.0 5.2 4.7 4.6  3.2  3.4 3.6  3.9 5.9 4.4     
2005 2.7 4.3 5.7 4.7 4.6 5.0  5.6  5.0 4.9  5.9 5.6 1.8     

How to create monthly max wind speed scatterplot (month in x-axis and wind speed in y-axis) and also the monthly max wind speed histogram by using R programming?

I recommend reading an introduction to R, but this should get you started.

df <- read.table(text="Year Jan Feb Mar Apr May June July Aug Sept Oct Nov Dec     
2011 4.5 5.6 5.0 5.4 5.0 5.0  5.2  5.3 4.8  5.4 5.4 3.8     
2010 4.6 5.0 5.8 5.0 5.2 4.5  4.4  4.3 4.9  5.2 5.2 4.6     
2009 4.5 5.3 4.3 3.9 4.7 5.0  4.8  4.7 4.9  5.6 4.9 4.1     
2008 3.8 1.9 5.6 4.7 4.7 4.3  5.9  4.9 4.9  5.6 5.2 4.4     
2007 4.6 4.6 4.6 5.6 4.2 3.6  2.5  2.5 2.5  3.3 5.6 1.5     
2006 4.3 4.8 5.0 5.2 4.7 4.6  3.2  3.4 3.6  3.9 5.9 4.4     
2005 2.7 4.3 5.7 4.7 4.6 5.0  5.6  5.0 4.9  5.9 5.6 1.8",header=TRUE)
#look at ?read.csv to learn how to read your data from a CSV file

#transform data.frame into long format
library(reshape2)
df <- melt(df,id="Year",variable.name = "Month",value.name = "Speed")

#plot boxplots per month
plot(Speed~Month,data=df)

箱形图

#histogram for January
hist(df$Speed[df$Month=="Jan"],main="Max wind speed in January",xlab="Speed")

直方图Jan

#histogram for 2011
hist(df$Speed[df$Year==2011],main="Max wind speed in 2011",xlab="Speed")

直方图2011

#create actual dates
df$Month2 <- factor(df$Month)
levels(df$Month2) <- 1:12
df <- transform(df,Time = as.Date(paste(Year,Month2,"15",sep="-"),'%Y-%m-%d'))
#plot Speed vs Time
plot(Speed~Time,data=df[order(df$Time),],type="l")

时间序列

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM