plot - Plotting a subset of sequential (time series) data in R -
i have data frame similar 1 below, called df. want plot subsets of data, may 2012 june 2014. had been using plot function plot entire data frame, however, when split subsets of plot same plot no matter portion of data select.
jan feb mar apr may jun jul aug sep oct nov dec 2011 na na na na na na 13.724575 13.670017 13.782099 13.675788 13.442914 13.279969 2012 13.114463 13.021268 12.999545 12.990171 12.898018 12.611495 12.311641 12.126345 11.974871 12.019042 12.163618 12.304660 2013 12.374017 12.365795 12.323280 12.377747 12.462203 12.630298 12.780495 12.848942 12.806210 12.860463 12.838953 12.608965 2014 12.616257 12.720611 12.841626 12.897939 13.008535 13.136377 13.159393 13.290928 13.495218 13.636360 13.797778 13.827273 2015 13.662063 13.527596 13.568430 13.782818 13.889276 13.971303 14.181846 14.329937 14.385533 14.289386 14.222535 14.384618 2016 14.516674 14.759385 14.951384 14.763781 14.536779 13.978265 12.888989 11.612033 10.362592 9.205528 8.649027 8.662219 2017 8.614229 8.446361 8.239606 8.286693 8.498938 8.972903 na na na na na na my current code looks similar below (stripped out non-essentials, x , y labels).
dates <- seq(as.date("2011-01-01"), = "months", length = 84) plot(dates, df, type = "l") how can modify plot sections?
i ok switching ggplot2 too, seemed give me weird results stuck plot() (perhaps that's because it's time series data frame? i'm not sure).
you need subset of data. also, convert data frame form wide long format easier work with, specially plotting/visualization purposes;
#long format many rows instead of many columns library(reshape2) long <- melt(df, id.vars = c("year")) #add column actual dates instead year , month in different columns long$date <- as.date(with(long,paste(variable,"15",year,sep = "-")), "%b-%d-%y") #take subset of data may 2012 june 2014 date1 <- as.date("2012-05-01", "%y-%m-%d") date2 <- as.date("2014-06-30", "%y-%m-%d") subdf <- long[long$date < date2 & long$date > date1,] #use ggplot2 provides nice labels , theme timeseries default library(ggplot2) ggplot(data=subdf, aes(date,value)) + geom_line() 
data:
df <- structure(list(year = 2011:2017, jan = c(na, 13.114463, 12.374017, 12.616257, 13.662063, 14.516674, 8.614229), feb = c(na, 13.021268, 12.365795, 12.720611, 13.527596, 14.759385, 8.446361), mar = c(na, 12.999545, 12.32328, 12.841626, 13.56843, 14.951384, 8.239606 ), apr = c(na, 12.990171, 12.377747, 12.897939, 13.782818, 14.763781, 8.286693), may = c(na, 12.898018, 12.462203, 13.008535, 13.889276, 14.536779, 8.498938), jun = c(na, 12.611495, 12.630298, 13.136377, 13.971303, 13.978265, 8.972903), jul = c(13.724575, 12.311641, 12.780495, 13.159393, 14.181846, 12.888989, na), aug = c(13.670017, 12.126345, 12.848942, 13.290928, 14.329937, 11.612033, na), sep = c(13.782099, 11.974871, 12.80621, 13.495218, 14.385533, 10.362592, na), oct = c(13.675788, 12.019042, 12.860463, 13.63636, 14.289386, 9.205528, na), nov = c(13.442914, 12.163618, 12.838953, 13.797778, 14.222535, 8.649027, na), dec = c(13.279969, 12.30466, 12.608965, 13.827273, 14.384618, 8.662219, na)), .names = c("year", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"), class = "data.frame", row.names = c(na, -7l)) reshaped data long format:
head(long) # year variable value date # 1 2011 jan na 2011-01-15 # 2 2012 jan 13.11446 2012-01-15 # 3 2013 jan 12.37402 2013-01-15 # 4 2014 jan 12.61626 2014-01-15 # 5 2015 jan 13.66206 2015-01-15 # 6 2016 jan 14.51667 2016-01-15
Comments
Post a Comment