0

Hi everyone I have this data:

structure(list(Site = c("aNon burn", "aNon burn", "aNon burn", 
"Long rotation", "Long rotation", "Long rotation"), Depth = c("0-20cm", 
"20-40cm", "40-60cm", "0-20cm", "20-40cm", "40-60cm"), bacteria = c(10.03, 
8.46, 8.48, 9.34, 8.43, 8.45), bac_se = c(0.06, 0.06, 0.09, 0.08, 
0.1, 0.05), fungi = c(8.18, 6.31, 6.45, 7.65, 5.88, 5.86), fun_se = c(0.21, 
0.19, 0.17, 0.16, 0.17, 0.16)), row.names = c(NA, 6L), class = "data.frame")

and I would like to do a barplot with a single Y axis

I tried this code

ggplot(qpcrdata2, aes(x=Depth, y=bacteria, fill=Site)) +  geom_bar(stat="identity", position=position_dodge()) +geom_errorbar(aes(ymin=bacteria-bac_se, ymax=bacteria+bac_se), width=.2,position=position_dodge(.9))

Gives me this figure

enter image description here

but I would also like to add Fungi on the same figure.

Can anyone help with this?

Best wishes

martyn
  • 1
  • 2
  • Could you please share some reproducible data using `dput`? Instead of images pls – Quinten Aug 15 '23 at 08:43
  • 2
    reshape to long data (so you have a column defining bacteria or fungi, a column for value and a column for se). – Paul Stafford Allen Aug 15 '23 at 08:46
  • Hi, I tried to edit the post. I don't know what you mean? I don't use this very often. – martyn Aug 15 '23 at 08:52
  • Quinten mean that we need data, not an image, to try to reproduce your example. Either you put the data somewhere (accessible in the cloud), either you put it here as dput output. – Fabrizio Aug 15 '23 at 09:00
  • I don't know how to add it as a dput? there is no option to do that when i try and edit the post from what I can see. – martyn Aug 15 '23 at 09:05
  • Do: dput(qpcrdata2) and put the output in a code section of your question. – Fabrizio Aug 15 '23 at 09:14
  • I still have no idea what this means. I tried to do that and it didn't work – martyn Aug 15 '23 at 09:23
  • 1
    in R, if you type `dput(head(qpcrdata2))` it will output some text which defines the top few rows (the `head()`) of the `qpcrdata2` object. You can copy that text, then come back to your question here and hit "edit", paste the copied text into your question then highlight it and click the "{}" button to turn it into a code block, then save changes. – Paul Stafford Allen Aug 15 '23 at 09:27

1 Answers1

0

OK, here's an approach to try, note that I added some extra data to your example so we'd have three bars per group:

# reshape the data - I'm using the tidyverse approach here

qpcrdata3 <- qpcrdata2 %>% rename(bacteria_count = bacteria,
                                  fungi_count = fungi,
                                  bacteria_se = bac_se,
                                  fungi_se = fun_se) %>% 
                           pivot_longer(cols = -c(Site, Depth),
                                        names_to = c("SampleType", ".value"),
                                        names_sep = "_")

Next we want to plot. Since we have effectively two sets of x criteria (both depth and bacteria/fungi) we use interaction():

ggplot(qpcrdata3, 
       aes(x=interaction(Depth, SampleType), y=count, fill=Site)) +  
  geom_bar(stat="identity",
           position=position_dodge()) +
  geom_errorbar(aes(ymin=count - se, ymax=count + se),
                width=.2,
                position=position_dodge(.9))

This gives us the (somewhat ugly): enter image description here

You can use the tips in this answer here to prettify the X axis: https://stackoverflow.com/a/20073020/16730940

Paul Stafford Allen
  • 1,840
  • 1
  • 5
  • 16