0

my_queryData frame1

quarter  year   value

q1       2012   100
q3       2013   200
q4       2014   400
q4       2015   250 
q3       2014   400

Data frame 2

quarter  year   value

q1       2014   100
q3       2012   200
q4       2015   400
q4       2012   250 
q3       2015   400

I want to compare these two by plotting in r.Can you please suggest here?

Note: I have tried different ways,but no luck

plot(DF1$fy,DF1$value,
  main ="Distribution of Assets in quarters across years",
    xlab = 'years',ylab = 'vales',type='l')
MKR
  • 19,739
  • 4
  • 23
  • 33

2 Answers2

0

You should obtain the desired output with the following code:

plot(DF1$year, DF1$value, type='l', col=1)
lines(DF2$year, DF2$value, col=2)
Roberto
  • 287
  • 1
  • 11
0

You can combine both data.frames together using dplyr::bind_rows and then use tidyr::expand to fill all missing quarter and year.

Now, you can draw line and point graph together using ggplot as:

library(tidyverse)
library(ggplot2)

bind_rows(df1, df2, .id="Name") %>% 
  right_join(expand(., Name, year = min(year):max(year), 
                    quarter = c("q1","q2","q3","q4")), by=c("Name","quarter","year")) %>%
  unite(YearQtr, c("year","quarter"), sep=":") %>%
  ggplot(aes(x = YearQtr, y = value, group=Name, col = Name)) +
  geom_point() +
  geom_line()

Result:

enter image description here

Data:

df1 <- read.table(text="
quarter  year   value
q1       2012   100
q3       2013   200
q4       2014   400
q4       2015   250 
q3       2014   400",
header = TRUE, stringsAsFactors = FALSE)


df2 <- read.table(text="
quarter  year   value
q1       2014   100
q3       2012   200
q4       2015   400
q4       2012   250 
q3       2015   400",
header = TRUE, stringsAsFactors = FALSE)
MKR
  • 19,739
  • 4
  • 23
  • 33
  • Thanks,but i am getting xaxis values overlapped,how can i increase the x axis values.years and quarters overlapped,unable to recognise – ADITYA KUMAR PINJALA Jun 27 '18 at 03:53
  • @ADITYAKUMARPINJALA If yoy mean overlapped at `x-axis` in `RStudio` then you can zoom graph. Another option is to use `Year` in 2-digit format. Actually, I would have preferred to use `zoo::yearmon` to display yearly quarter on x-axis. – MKR Jun 27 '18 at 04:43