跨多个地块的连线R地质剖面

时间:2019-07-19 11:34:30

标签: r ggplot2 gridextra

我正在尝试重新创建一个类似于下面的地质剖面图,其中显示了针对深度(y轴)绘制的各种岩石参数(x轴)

geological cross section

我可以很好地在ggplot2和网格中重新创建各个图,以创建非常相似的图。最后,我真的很想在各图之间加入线条,以显示与图中相似的地质区域。

下面是一些用水平线绘制图表的代码,我真正要做的是连接线(如果可能的话,在R中),并且如果可能的话,根据线对齐图表

library(ggplot2)
library(gridExtra)

df1 = data.frame(replicate(2,sample(0:200,100,rep=TRUE)))
df1$depth = seq.int(nrow(df1))

df2 = data.frame(replicate(2,sample(0:200,100,rep=TRUE)))
df2$depth = seq.int(nrow(df1))


top1 = 32
top2 = 50

plot1 = ggplot(df1, aes(y = depth, x = X1))+
  scale_y_continuous(trans = "reverse")+
  geom_path()+
  geom_hline(yintercept=top1, colour = "red")+
  annotate(geom="text", x=25, y=top1, label=top1, color="red")+
  theme_bw()+
  theme(panel.grid.major = element_line(colour = "grey"), panel.background = element_rect(colour = "black", size=0.5))+
  ylab("Depth ft")+
  ggtitle("plot1")


plot2 = ggplot(df2, aes(y = depth, x = X1))+
  scale_y_continuous(trans = "reverse")+
  geom_path()+
  geom_hline(yintercept=top2, colour = "red")+
  annotate(geom="text", x=25, y=top2, label=top2, color="red")+
  theme_bw()+
  theme(panel.grid.major = element_line(colour = "grey"), panel.background = element_rect(colour = "black", size=0.5))+
  ylab("Depth ft")+
  ggtitle("plot2")

grid.arrange (plot1, plot2, ncol=2)

这将是我希望在连接的线并可能对齐的情况下期望的结果。

enter image description here

感谢您提供的任何帮助或建议

欢呼

1 个答案:

答案 0 :(得分:0)

我无法帮助线连接部分,但是音阶偏移的想法听起来很有趣。此解决方案采用任意数量的数据帧和相应的等值线列表,然后移动y比例,以使每个等值线都为0。

然后绘制每个数据帧,并适当地重新编号y轴。

library(purrr)
library(dplyr)
library(ggplot2)

# library(cowplot)
#   I never load `cowplot` because it changes some settings onload.
#   I just call the namespace with `cowplot::plot_grid(...)`
#   You will need it installed though.

depth_plots <- function(..., isolines) {
  dats <- list(...)
  stopifnot(length(dats) == length(isolines))

  scaled_dats <- map2(dats, isolines, ~.x %>% mutate(sc_depth = depth - .y))

  new_range <- 
    map(scaled_dats, ~range(.x$sc_depth)) %>% 
    unlist() %>% 
    range() %>% 
    scales::expand_range(mul = 0.05)

  plots <- map2(
    scaled_dats, isolines,
    ~ggplot(.x, aes(y = sc_depth, x = X1)) +
      scale_y_continuous(
        trans = "reverse", 
        breaks = scales::extended_breaks()(.x$depth) - .y,
        labels = scales::extended_breaks()(.x$depth)
        ) +
      geom_path() +
      geom_hline(yintercept=0, colour = "red") +
      annotate(geom="text", x=25, y=0, label=.y, color="red") +
      coord_cartesian(
        ylim = new_range
      ) +
      theme_bw()
  ) 

  cowplot::plot_grid(plotlist = plots, nrow = 1)
}

要测试各种深度结构,我对您的样本数据做了一些更改:

df1 = data.frame(replicate(2,sample(0:200,100,rep=TRUE)))
df1$depth = seq.int(nrow(df1))

df2 = data.frame(replicate(2,sample(0:200,100,rep=TRUE)))
df2$depth = seq.int(nrow(df1))*0.75

df3 = data.frame(replicate(2,sample(0:200,100,rep=TRUE)))
df3$depth = seq.int(nrow(df1))*2

depth_plots(df1, df2, df3, isolines = c(32,50, 4))

enter image description here

希望让您入门!