-1

Need a help plotting in r for time series data.The Y is temperature and X is date(now in format %d-%m-%y) and need a plot like:

temp data in month wise along with a box plot

I have tried:

univariare-temperature

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)

but the format is coming incorrect.

Any help will be really appreciable!!

Thanks Devi

DEvz
  • 17
  • 2
  • 9
  • create a proper reproducible example as [described here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and also `dput` your data used – parth Aug 07 '17 at 12:11
  • Please review [mcve]. Also use ctrl-g to insert graphics into the question (as opposed to linking to it). – G. Grothendieck Aug 07 '17 at 13:21

1 Answers1

1

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")
Jason
  • 58
  • 3
  • thank you so much for the quick reply!Just to add the dataset I am using have date in format '%d-%m-%y' and temp1 so here we are taking average for temp for each month and plotting it for 2011 year? so in the given code I should use my temp at place ? – DEvz Aug 07 '17 at 13:49
  • Hi @DeviVijayakumar, I think the average that you are referring to was my attempt at making some fake temperatures to model the image you posted. It was probably unnecessary to build the data this way. Essentially, I just created a temperature for each day of the year using averages from random numbers. So, I have 365 temperatures, each associated with a specific date. I think you are correct, although I do not know what your dataset "r," looks like. Wherever your numeric temperature data is, which should be one value per day of the year, substitute those values for `temp.avg` – Jason Aug 07 '17 at 14:07
  • @Jason..thank you so much...my data file is like a variable temp for each day for 365 days in 2011 and need to be a time series plot temp for the complete year instant dteday season yr mnth holiday weekday workingday weathersit temp 1 1 2011-01-01 spring 0 1 FALSE Saturday cloudy/misty 0.344167 atemp hum windspeed casual registered cnt 1 0.363625 0.805833 0.160446 331 654 985 – DEvz Aug 07 '17 at 14:16