我有这样的数据:
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
我想使用不同的颜色为UnitCode
制作相对于SampleDepth
的叠加条形图。实施例 - (0至0.2米 - >土壤 - >蓝色),(0.2至0.4 - >粘土 - >绿色),(0.4至0.6 - >淤泥 - >粉红色)等。任何人都可以请帮助我这样做?我也提供了一个图像来表明我的意思。对于不同的深度间隔--->不同的颜色代表土壤类型。
图片示例:
谢谢
答案 0 :(得分:4)
这应该有效:
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))