如何使用 ggplot 制作堆积条形图来表示土壤 column/types

How to make a stacked bar plot using ggplot to represent soil column/types

我有这样的数据:

SampleID  From  To   SampleDepth UnitCode  Gravel_perc Sand_perc Fines_perc
2007-01   0.00 0.2       0.100     Soil          25        70          4
2007-02   0.20 0.4       0.300     Clay          45        45          5
2007-03   0.40 0.6       0.500     Silt          40        50          5
2007-04   1.12 1.2       1.160     Soil          45        10         40
2007-05   2.31 2.5       2.405     Clay          10        30         50

我想用不同的颜色为 UnitCodeSampleDepth 制作一个堆叠条形图。示例 -(0 到 0.2 m -> 土壤 -> 蓝色),(0.2 到 0.4 -> 粘土 -> 绿色),(0.4 到 0.6 -> 淤泥 -> 粉红色)等。有人能帮我做这件事吗?我还提供了一张图片来说明我的意思。对于不同的深度间隔--->不同的颜色来表示土壤类型。

图片示例:

谢谢

这应该有效:

ggplot(dat, aes(x = factor(1), y = -SampleDepth, fill = UnitCode)) +
    geom_bar(stat = "identity") +
    scale_fill_manual(breaks = c("Clay", "Silt", "Soil"),
                      c("darkolivegreen3", "pink", "steelblue2"))

使用此数据:

dat = structure(list(SampleID = structure(1:5, .Label = c("2007-01", 
"2007-02", "2007-03", "2007-04", "2007-05"), class = "factor"), 
    From = c(0, 0.2, 0.4, 1.12, 2.31), To = c(0.2, 0.4, 0.6, 
    1.2, 2.5), SampleDepth = c(0.1, 0.3, 0.5, 1.16, 2.405), UnitCode = structure(c(3L, 
    1L, 2L, 3L, 1L), .Label = c("Clay", "Silt", "Soil"), class = "factor"), 
    Gravel_perc = c(25L, 45L, 40L, 45L, 10L), Sand_perc = c(70L, 
    45L, 50L, 10L, 30L), Fines_perc = c(4L, 5L, 5L, 40L, 50L)), .Names = c("SampleID", 
"From", "To", "SampleDepth", "UnitCode", "Gravel_perc", "Sand_perc", 
"Fines_perc"), class = "data.frame", row.names = c(NA, -5L))