1

I have a quick question about plotting variables for multiple years as shown on this link: ggplot: Multiple years on same plot by month How do I assign my variable where "value" is located instead of fake data?

Access data at this link

library(readr)

rawdata <- read_csv("https://gist.githubusercontent.com/dewittpe/f9942bce11c34edabf888cbf8375ff17/raw/cb2b527fb2ee5c9c288b3246359c57d36df9fc6e/Data.csv")

0. SETUP DATA

library(zoo)
library(lubridate)
library(ggplot2)
value <- rawdata$Value #Name of variable from csv

dat1 = data.frame(date = seq(as.Date("1995-01-01"),
as.Date("1995-12-01"), "1 month"),
              value = cumsum(rnorm(12)))  ###How do I assign my variable where "value" is located instead of fake data?
dat1$date = as.yearmon(dat1$date)  

dat2 = data.frame(date = seq(as.Date("1996-01-01"),
as.Date("1996-12-01"), "1 month"),
              value = cumsum(rnorm(12)))
dat2$date = as.yearmon(dat2$date)


ggplot(rbind(dat1,dat2), aes(month(date, label=TRUE, abbr=TRUE), 
                         value, group=factor(year(date)),
                         colour=factor(year(date)))) +
  geom_line() +
  geom_point() +
  labs(x="Month", colour="Year") +
  theme_classic()'

enter image description here

1 Answer 1

1

One suggestion: use readr to read the data into R. This will help with setting the storage modes for each column. I made a copy of the data set in a github gist. To read the data into R use

library(readr)

dat1 <- read_csv("https://gist.githubusercontent.com/dewittpe/f9942bce11c34edabf888cbf8375ff17/raw/cb2b527fb2ee5c9c288b3246359c57d36df9fc6e/Data.csv")

Once the data has been read in, the graphic is generated as follows.

library(lubridate)
library(ggplot2)
library(dplyr)

# Use dplyr::filter to filter the data to the years of interest.

dat1 %>%
  dplyr::filter(lubridate::year(date) %in% 1995:1996) %>%

ggplot(.) +
  aes(x = lubridate::month(date, label = TRUE, abbr = TRUE),
      y = value,
      group = factor(lubridate::year(date)),
      color = factor(lubridate::year(date))) +
  geom_line() +
  geom_point() +
  labs(x = "Month", color = "Year") +
  theme_classic()

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

Hi Peter, would it be possible to select which years I'd want to plot and by month as i had it using your method? Run my code for illustration. Thank you.
Use dplyr::filter to filter the data set. I've edited the answer to show this.
Fantastic! Thank you for time!
Quick question, how would i be able to select dplyr::filter(lubridate::year(date) %in% 1995:1997) %>% and add 2002 excluding 1998-2001? Thanks again
Yes, use dat1 %>% dplyr::filter(lubridate::year(date) %in% c(1995, 1996, 2002))
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.