Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用ggplot2可视化钻孔剖面(堆叠条形图)?_R_Ggplot2_Bar Chart_Stacked Chart - Fatal编程技术网

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

R 如何使用ggplot2可视化钻孔剖面(堆叠条形图)?,r,ggplot2,bar-chart,stacked-chart,R,Ggplot2,Bar Chart,Stacked Chart,我想画几个钻孔点,每个点都沿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 Dr

我想画几个钻孔点,每个点都沿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
到目前为止,我有两种方法(见下面的代码):

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.试试看:

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
它不是从0开始,我的颜色/类别旋转1步

2.试试看:

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示例中,可能是淤泥,然后是砾石,然后是淤泥)
我知道这是通过
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()
库(ggplot2)

df您可以
按“ID”分组
,并
反转默认的堆叠顺序。您也可以反转y轴(
缩放y\u反转
)以反映深度

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

@Valentin请注意编辑。我刚刚记得有一个
position\u堆栈(reverse=TRUE)
参数。不需要“反向”id变量。谢谢@Hendrik!您的解决方案工作正常,直到我在每列中找到10个单独的项目。之后,它将我的图层按如下顺序排列:1,10,11,2,3,4,5,6,7,8,9。我怎样才能解决这个问题?您可以在这里找到一个示例图像,其中标绘的
ix
作为标签:[1]()@Valentin。您的“ID”列似乎是一个
因子。确保它是数值型的。例如,这工作正常:
d