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

8
  • where is the image? also try to put code in code section for better readability. welcome to SO! Commented Apr 5, 2016 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 Commented Apr 5, 2016 at 15:49
  • how SampleDepth are grouped? Commented Apr 5, 2016 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 Commented Apr 5, 2016 at 16:03
  • What goes on the x-axis of your graph? Or do you only want a single bar? Commented Apr 5, 2016 at 17:21

1 Answer 1

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)) 
Sign up to request clarification or add additional context in comments.

9 Comments

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
Updated with colors.
Thank You very much for your answer. This helps a lot. :-)
Is there a way of controlling the width of the rectangle (bar), besides adjusting the plotting area?
@user2955884 sure, see ?geom_bar and use the width argument.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.