在r中绘制时间序列数据,每月一个月

时间:2017-08-07 11:49:40

标签: r plot ggplot2 time-series

需要帮助在r中绘制时间序列数据.Y是温度,X是日期(现在格式为%d-%m-%y),需要如下图:

temp data in month wise along with a box plot

我试过了:

univariare温

temperature = ts(r$temp, frequency = 12, start = 2011) plot(temperature, xaxt = "n") tsp = attributes(temperature)$tsp dates
= seq(as.Date("2011-01-01"), by = "month", along = temperature) axis(1, at = seq(tsp[1], tsp[2], along = temperature), labels = format(dates, "%m-%y"))

ggplot

gg2=ggplot(r,aes(mnth,temp)) + geom_line() 
windows() 
print(gg2)

但格式不正确。

任何帮助都会非常明显!!

由于 德维

1 个答案:

答案 0 :(得分:0)

Time Series Temperature Plot by Month

library(ggplot2)

# Simulated temperature data
temp <- runif(75)

temp.avg <- vector()

x <- 365

for(i in 1:x){
  if(i <= round(.33 * x)) {
    temp.avg[i] <- mean(sample(temp, 15, replace = TRUE))
  } else if (i <= round(.66 * x)) {
    temp.avg[i] <- abs(log(mean(sample(temp, 15, replace = TRUE))))
  } else {
    temp.avg[i] <- mean(sample(temp, 15, replace = TRUE)) * (i / x) + .15
 }
 }

 # Generate sequence of days in Date format "%d-%m-%y"
 from <- as.Date("01-1-11 12:00:00 EDT", "%d-%m-%y")
 to <- as.Date("31-12-11 12:00:00 EDT", "%d-%m-%y")
 times <- seq.Date(from, to, 1)

 # Put dates and temperatures into data frame
 Temperature_data <- data.frame(date = times, temp = temp.avg)

 # Plot in ggplot
 ggplot(Temperature_data, aes(date, temp)) + 
   geom_line() + 
   ylim(c(0, 1)) + 
   xlab("") + 
   ylab("Temperature") +
   scale_x_date(date_breaks = "1 month", date_labels = "%b %y") +
   theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
   ggtitle("Temperature fluctuations in 2011")
相关问题