1

I am using the following code to plot:

q <- qplot(quarterYear, var_1, data=dataset_1)
q <- q + geom_line(aes(group = 1))
q + theme(axis.text.x = element_text(angle = 90, hjust = 1))

which uses the following quarterly datasets:

> dataset_1$var_1
 [1]        NA        NA        NA        NA  444618.3 1556125.2  744145.1  844862.2  773188.2
[10] 1204732.2 1832308.2 1732186.6 1475089.7 1238791.2  772359.0  927111.5  982978.9  581415.1
[19]  489457.8  446419.0  403841.0  654630.9  753729.4  513755.0  587031.5  465808.7  462710.4
[28]  537923.9  409037.8  785118.7

> dataset_1$quarterYear
 [1] "2012Q1" "2012Q2" "2012Q3" "2012Q4" "2013Q1" "2013Q2" "2013Q3" "2013Q4" "2014Q1" "2014Q2"
[11] "2014Q3" "2014Q4" "2015Q1" "2015Q2" "2015Q3" "2015Q4" "2016Q1" "2016Q2" "2016Q3" "2016Q4"
[21] "2017Q1" "2017Q2" "2017Q3" "2017Q4" "2018Q1" "2018Q2" "2018Q3" "2018Q4" "2019Q1" "2019Q2"

I would also like to plot the following variable in the same graph from another dataset (which uses annual data instead of quarterly):

> dataset_2$var_2
 [1] 3544407      NA      NA      NA 5254710      NA      NA      NA 4430303      NA      NA
[12]      NA 2499437      NA      NA      NA 2216218      NA      NA      NA 2024459      NA
[23]      NA      NA 2537232      NA

How can I add this second line in the same graph with the first variable?

EDIT:

Here is a dataframe with all the variables needed:

> dput(dataframe_1)
structure(list(var_1 = c(NA, NA, NA, NA, 3544407.49, NA, NA, 
NA, 5254709.85, NA, NA, NA, 4430302.745, NA, NA, NA, 2499437.24, 
NA, NA, NA, 2216217.61, NA, NA, NA, 2024459.22, NA, NA, NA, 2537232.26, 
NA), quarterYear = c("2012Q1", "2012Q2", "2012Q3", "2012Q4", 
"2013Q1", "2013Q2", "2013Q3", "2013Q4", "2014Q1", "2014Q2", "2014Q3", 
"2014Q4", "2015Q1", "2015Q2", "2015Q3", "2015Q4", "2016Q1", "2016Q2", 
"2016Q3", "2016Q4", "2017Q1", "2017Q2", "2017Q3", "2017Q4", "2018Q1", 
"2018Q2", "2018Q3", "2018Q4", "2019Q1", "2019Q2"), var_2 = c(NA, 
NA, NA, NA, 444618.290581211, 1556125.2312821, 744145.122633215, 
844862.245210837, 773188.163309878, 1204732.23940684, 1832308.19798703, 
1732186.62322613, 1475089.69403864, 1238791.22680584, 772358.984604352, 
927111.535808541, 982978.903715697, 581415.120412662, 489457.818616084, 
446419.025443493, 403840.992700758, 654630.928503824, 753729.397874631, 
513754.997587623, 587031.503365487, 465808.650890606, 462710.391792817, 
537923.914536013, 409037.838465172, 785118.681845306)), row.names = c(NA, 
-30L), class = "data.frame")
7
  • The easiest way is to combine the datasets and then plot the different variables. You have to set a matching x-axis for all your y-axis variables, before you will be able to plot them together. Commented Feb 5, 2020 at 11:53
  • Can you give me the code of what you are suggesting to do? Commented Feb 5, 2020 at 11:58
  • Only if you could provide 2 example datasets. In pseudocode it would look like: 1. Split quateryearcolumn to a quarter column and a year column 2. left join dataset2 on dataset 1 using the year column in dataset1 and dataset2 3. plot all desired lines. Commented Feb 5, 2020 at 12:12
  • I have provided the dataset above you have all you need right? Commented Feb 5, 2020 at 12:15
  • It is easier when you provide scripts which create a dataframe. People are less likely to help you when you have hardcoded dataframes. I do not want to spend my time typing over dataframes. ;) Commented Feb 5, 2020 at 12:22

1 Answer 1

1

Thanks for providing the data! As explained in the comments:

library(dplyr)
library(ggplot2)

Step1 - Create a year column

df2 = dataframe1 %>% mutate(Year = substr(quarterYear, start = 1, stop = 4))

Step2 - Create column with var_1 per year, for later left_join

df3 = df2 %>% distinct(var_1, Year) %>% filter(!(is.na(var_1)))

Step3 - Left join to get data for ggplot in desired format

df4 = df2 %>% select(Year, quarterYear, var_2) %>% left_join(df3)

Step4 - Plot Data

ggplot(data = df4) + geom_line(aes(x = quarterYear, y = var_1, group =
                                           1)) + geom_line(aes(x = quarterYear, y = var_2, group = 2)) + theme(axis.text.x = element_text(angle = 90, hjust = 1))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.