0

I have 2 datasets, both with the column Depth. However df1 has ~400 rows and df2 7000 rows. The depth values, which I want to be my common x axis, for df1 go from 48-120 and df2 48-133. When I make my plots this difference in the range stops the plots from lining up.

df1 sample data

depth L F P Depo 67.48 1.003 1.063 1.066 Turb 67.63 1.004 1.020 1.024 Dri 67.73 1.011 1.017 1.028 Dri 67.83 1.006 1.007 1.014 Turb 67.92 1.003 1.029 1.032 Pro 68.06 1.004 1.007 1.011 Pro

df2 sample data

depth Ca Ti 67.41 378 241 67.91 422 253 67.94 402 262 67.95 412 264 67.98 377 266 68.01 386 263 68.02 326 266 68.08 338 219

I tried making individual plots and then using grid.draw but this doesn't work.

creating plots from DF1

Lin <- ggplot(DF1, aes(x=depth, y=L)) + geom_line() + geom_point(data = DF1, aes(x=depth, y=L, color = Depo))
Fab <- ggplot(DF1, aes(x=depth, y=P)) + geom_path() + geom_point(data = DF1, aes(x=depth, y=P, color = Depo))
Fol <- ggplot(DF1, aes(x=depth, y=F)) + geom_path() + geom_point(data = DF1, aes(x=depth, y=F, color = Depo))

Combining plots with grid.draw works for the df1 graphs

grid.draw(rbind(ggplotGrob(Fol), ggplotGrob(Lin), ggplotGrob(Fab), size = "last"))

Creating plot from DF2

Ca1 <- ggplot(DF2, aes(x=depth, y=Ca)) + geom_path()

When I try to combine the plots from the 2 dataframes it throws an error that x and y must have the same amount of columns.

grid.draw(rbind(ggplotGrob(Fol), ggplotGrob(Lin), ggplotGrob(Fab), ggplotGrob(Ca1), size = "last"))

Cowplot works but the depths don't line up for my df2 graph (Ca1)

plot_grid(Fol, Lin, Fab, Ca1, align="h", axis="b", nrow = 4, rel_widths = c(1,2))

I tried some other ways of lining the graphs up but it seems they all line the plots up, not the actual values of the x axis. I also tried to use facet wrap but couldn't work out how to combine the 2 dfs. In my searching to resolve this problem I keep seeing to combine the 2 dataframes but I can't see how this would work with my data? Does anyone know how I can line these graphs up? I have so many variables I need to compare from both datasets.

Brendon
  • 29
  • 3
  • Try other packages from this to see if it works for you https://stackoverflow.com/a/48164920/786542 – Tung Apr 04 '19 at 06:21
  • Thanks. Both egg and patchwork just gave me the same thing, the plot from df2 is not aligned properly. I'll keep trying though. – Brendon Apr 04 '19 at 07:07

1 Answers1

1

To integrate the two data sets, which only have "depth" in common, you can gather the remaining numeric columns into "long" format, where we label the type of data in one column ('col' here) and the value in another ('val' here).

Once the data is combined, we can use facet_wrap(~col, scales = "free_y") to make facets for each variable, but with a common x axis.

library(tidyverse)
df_combo <- 
  bind_rows(
    df1 %>% gather(col, val, L:P),
    df2 %>% gather(col, val, Ca:Ti)
  )

ggplot(df_combo, aes(depth, val, color = Depo)) +
  geom_path() +
  facet_wrap(~col, scales = "free_y", ncol = 1)

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Hard to answer without seeing a sample of your prepared data. I suggest taking it step by step and confirming that each of the tables you prepare looks right. Is there nested data or matrix data somewhere? Is the `gather` step using column ranges appropriate to your data? Are there any typos where you're referring to columns that don't exist in exactly that form (including capitalization) in your data? – Jon Spring Apr 04 '19 at 21:19
  • Sorry I tried to delete that comment as I found the problem. It was just that my real data used the name Depth, and the sample data depth. The code you provided above was perfect. The only change I made was adding geom_point separately to color the points and not the line. Now I just have to work out how to stop the NA points in my df2 from being colored as I lose the detail since there's over 7000 points – Brendon Apr 04 '19 at 21:46
  • ggplot(df_combo, aes(depth, val)) + geom_path() + geom_point(aes(color=Depo)) + facet_wrap(~col, scales = "free_y", ncol = 1) – Brendon Apr 04 '19 at 21:47