如何使用 ggplot2 可视化钻孔剖面(堆积条形图)?

How to visualize a borehole profile (stacked bar plot) with ggplot2?

我想绘制几个钻孔点,每个钻孔点都显示沿 z 轴的地质钻孔数据。作为最终结果,它应该如下所示:

我的数据是这样的:

X layer.thickness layer.depth ID Category BSCategory
0             0.2         0.2  1     silt  Drilling1
0             1.0         1.2  2   gravel  Drilling1
0             3.0         3.2  3     silt  Drilling1
0             0.4         0.4  4     silt  Drilling2
0             0.8         1.2  5   gravel  Drilling2
0             2.0         3.2  6     sand  Drilling2

到目前为止我有两种方法(见下面的代码):

1.Try:

它不是从 0 开始的,我的 colors/categories 旋转了 1 步。

2.Try:

我知道这是 geom_bar() 方法的目的,但我可以停用分组和排序吗?

library(ggplot2)

df <- data.frame(X = 0, layer.thickness = c(0.2,1,3,0.4,0.8,2),layer.depth = c(0.2,1.2,3.2,0.4,1.2,3.2), ID = c(1,2,3,4,5,6),Category=c("silt","gravel","silt","silt","gravel","sand"),BSCategory=c("Drilling1","Drilling1","Drilling1","Drilling2","Drilling2","Drilling2"))

# 1. Try:
q <- qplot(x=X, y = layer.thickness,data = df, color = Category, group=1, geom="line",lwd=1)
q + facet_grid(. ~ BSCategory)
# Problem: Does not start at 0. Categories / Colors are rotated by 1 step.

# 2. Try:
# Best try so far
ggplot(data = df, aes(x = BSCategory, y = layer.thickness, fill = Category, color=Category)) + 
  geom_bar(stat="identity")
# Problem: Mixes up layer ordering
# groups values together (it schould be silt gravel silt in the Drilling1 example)


# Maybe good alternatives?
geom_linerange()
geom_vline()
geom_segment()

1.Try 的输出:

2.Try 的输出:

您可以group按"ID"和reverse默认堆叠顺序。您也可以反转 y 轴 (scale_y_reverse) 以反映深度。

ggplot(data = df, aes(x = BSCategory, y = layer.thickness, group = ID, fill = Category)) +
    geom_col(position = position_stack(reverse = TRUE)) +
    scale_y_reverse()