produce graph from multiple data frames

df
fd

Hi all,

from the attached 2 datasets I would like to plot 2 graphs on the same page based on the codes column
on x- axis will be the year and y-axis will be the value .

value column is different in both datasets and I would like to have two graphs
any help?

1 Like

Remember put a reproducible example.
Im get the data by OCR in R

df <-structure(list(year = 2000:2010, 
code = c(129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L), 
value = c(0L, 12L, 23L, 34L, 0L, 89L, 67L, 0L, 0L, 45L, 23L)), 
class = "data.frame", row.names = c(NA,-11L))


# year code value
# 1  2000  129     0
# 2  2001  129    12
# 3  2002  129    23
# 4  2003  129    34
# 5  2004  129     0
# 6  2005  129    89
# 7  2006  129    67
# 8  2007  129     0
# 9  2008  129     0
# 10 2009  129    45
# 11 2010  129    23

p1 <- ggplot(df, aes(x=year  , y=value )) + 
  geom_point() # for points

df2 <- structure(list(year = 2000:2010, code = c(129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 139L), value = c(23L, 0L, 13L, 0L, 0L, 99L, 67L, 0L, 0L, 70L, 0L)),
 class = "data.frame", row.names = c(NA, 
-11L))

p2 <- ggplot(df2, aes(x=year  , y=value )) + 
  geom_point() # for points

library(gridExtra)
library(grid)

grid.arrange(p1,p2, ncol=1, top=textGrob("Multiple Plots", gp=gpar(fontsize=12, font = 2)))

1 Like

Alternatively, you can merge the two data frames and plot the panels using facet_wrap(). Below an example using @M_AcostaCH data frames.

library(tidyverse)

df <-structure(list(year = 2000:2010, 
  code = c(129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L), 
  value = c(0L, 12L, 23L, 34L, 0L, 89L, 67L, 0L, 0L, 45L, 23L)), 
  class = "data.frame", row.names = c(NA,-11L))

df2 <- structure(list(year = 2000:2010, code = c(129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 139L), value = c(23L, 0L, 13L, 0L, 0L, 99L, 67L, 0L, 0L, 70L, 0L)),
  class = "data.frame", row.names = c(NA, 
    -11L))

df %>% 
  left_join(df2, by = c("year", "code")) %>%
  pivot_longer(cols=value.x:value.y) %>%
  ggplot(aes(x = factor(year), y = value)) +
  geom_point(show.legend = F) +
  facet_wrap(~ name, nrow = 2)

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.