0

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:

enter image description here

Thank You

MLavoie
  • 9,671
  • 41
  • 36
  • 56
Rupesh
  • 41
  • 8
  • where is the image? also try to put code in code section for better readability. welcome to SO! – Zahiro Mor Apr 05 '16 at 15:47
  • Hi, Sorry its my first time asking a question and things are a little weird. I tried attaching an image but didnt show up. I will try again – Rupesh Apr 05 '16 at 15:49
  • how SampleDepth are grouped? – Vincent Bonhomme Apr 05 '16 at 15:58
  • The sample depths are the average of From and To. So basically it is the extension's of samples in the ground – Rupesh Apr 05 '16 at 16:03
  • What goes on the x-axis of your graph? Or do you only want a single bar? – Gregor Thomas Apr 05 '16 at 17:21
  • Hi Gregor, I just want a single bar. The y axis is the SampleDepth. – Rupesh Apr 05 '16 at 17:38
  • Are you sure you want to use ggplot? The aqp package has advanced functionality for plotting soil profile data. E.g. see https://www.r-project.org/conferences/useR-2011/TalkSlides/Contributed/17Aug_1705_FocusV_1-Hydrology_3-Roudier.pdf – dww Apr 05 '16 at 18:06
  • Hi, Thank You very much for your feedback. I didn't know about the aqp package, however it seems really helpful but currently, yes i wanted to accomplish this by using the ggplot. – Rupesh Apr 05 '16 at 18:14

1 Answers1

4

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"))

enter image description here

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))
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Hi Gregor, thank you very much for your answer. Is there a way we can manually change the colour ? Currently, the colour seems to be generated automatically. It is because I want to give a suitable colour classification to the specific soil types by myself. Thanks – Rupesh Apr 05 '16 at 18:12
  • Updated with colors. – Gregor Thomas Apr 05 '16 at 18:30
  • Thank You very much for your answer. This helps a lot. :-) – Rupesh Apr 05 '16 at 18:31
  • Is there a way of controlling the width of the rectangle (bar), besides adjusting the plotting area? – user2955884 Sep 17 '18 at 15:24
  • @user2955884 sure, see `?geom_bar` and use the `width` argument. – Gregor Thomas Sep 17 '18 at 20:20
  • But that is relative. In my case, the categories (i.e. clay, silt, soil...) are not all present in every barplot to be drawn, and some categories have much longer names than others. As a consequence, the rectangle becomes narrower if the legend becomes wider. I wish to set the width of the rectangle and, in case the legend would become wider, just make wider the size of the plot, keeping rectangles of consisting size. By now I'm just omitting the legend and writing labels on the barplot. – user2955884 Sep 18 '18 at 08:10
  • The `cowplot` package can help with that. See, e.g., [here](https://stackoverflow.com/a/13657460/903061) and [here](https://stackoverflow.com/a/39719810/903061), or using `gtable` [here](https://stackoverflow.com/a/24333504/903061), and [here](https://stackoverflow.com/a/36198587/903061) for aligning plots both ways. If that doesn't answer your question, I'd suggest asking a new question. – Gregor Thomas Sep 18 '18 at 13:21
  • Thanks. One more: using (standard) textures is very common in soil science and geology, is there any way to add them? – user2955884 Sep 18 '18 at 19:27
  • Search in the [ggplot2] tag for something like "add texture to plot". – Gregor Thomas Sep 19 '18 at 01:14