问题
I have my data like this:
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
I want to make a stacked barplot for the UnitCode
with respect to the SampleDepth
using different colours. Example - (0 to 0.2 m -> Soil -> Blue), (0.2 to 0.4 -> Clay -> green), (0.4 to 0.6 -> Silt -> pink) etc. Can anyone please help me in doing this? Also I have provided an image to show what I mean. For different depth intervals ---> different colours to represent the soil type.
Image Example:
Thank You
回答1:
This should work:
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"))
Using this data:
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))
来源:https://stackoverflow.com/questions/36431036/how-to-make-a-stacked-bar-plot-using-ggplot-to-represent-soil-column-types