Barplot with SERror from table

I am trying to plot grouped hatches from the table I generated below.

Group.1                S.obs     se.obs    S.chao1   se.chao1
Cliona celata complex  499.7143  59.32867  850.6860  65.16366
Cliona viridis         285.5000  51.68736  462.5465  45.57289
Dysidea fragilis       358.6667  61.03096  701.7499  73.82693
Phorbas fictitius      525.9167  24.66763  853.3261  57.73494

      

So far I have tried the following with no good results:

library(dplyr)
library(tidyr)
library(ggplot2)

data.frame(t(agg_media)) %>%
add_rownames() %>%
gather(group, value, - c(rowname, se.chao1)) -> media_2
gather(group, value, - c(rowname, se.obs)) -> media_3

#take out error bars from S.obs
# mutate(media2, se.chao1 = replace(se.chao1, which(group == "S.obs"),NA)) -> media3

dodge <- position_dodge(width=0.9)

g <- ggplot(data = agg_media, aes(x = rowname, y = value, fill = group)) +
   geom_bar(position = "dodge", stat = "identity") + 
   geom_errorbar(data = media_2, aes(ymax = value + se.chao1, ymin = value - se.chao1),
            position = dodge, width = 0.25) +
   geom_errorbar(data = media_3, aes(ymax = value + se.obs, ymin = value - se.obs),
            position = dodge, width = 0.25) +
 labs(x = "Sponge Species", y = "Averaged OTU Richness") +
 scale_y_continuous(expand = c(0,0))

ggsave(g, file = "Obs_Est_OTUs.svg")

      

The point is to take se.obs as the standard error for S.obs and se.chao1 as the standard error for S.chao1 and plot them as a grouped barcode ...

What am I doing wrong here?

+3


source to share


1 answer


Is this what you want?

Load the data snippet:

txt <- '"Group.1"                "S.obs"   "se.obs"  "S.chao1" "se.chao1"
"Cliona celata complex"  499.7143  59.32867  850.6860  65.16366
"Cliona viridis"         285.5000  51.68736  462.5465  45.57289
"Dysidea fragilis"       358.6667  61.03096  701.7499  73.82693
"Phorbas fictitius"      525.9167  24.66763  853.3261  57.73494'

dat <- read.table(text = txt, header = TRUE)

      

and download some packages. Specifically, I am going to use tidyr for data manipulation that doesn't really fit in with the concepts of molten material casting or reshaping

library("ggplot2")
library("tidyr")

      

These three steps get the data in a suitable format. First, we collect variables that are similar to melt()

, but we need to tell which variable not to collect, i.e. Which variable is a variableid

mdat <- gather(dat, S, value, -Group.1)

      

S

- this is the column I want to create containing the variable names value

is the name of the column I want to create which contains data from the selected columns, - Group.1

meaning working on all columns except group.1

. This gives:

                 Group.1        S     value
1  Cliona celata complex    S.obs 499.71430
2         Cliona viridis    S.obs 285.50000
3       Dysidea fragilis    S.obs 358.66670
4      Phorbas fictitius    S.obs 525.91670
5  Cliona celata complex   se.obs  59.32867
6         Cliona viridis   se.obs  51.68736
7       Dysidea fragilis   se.obs  61.03096
8      Phorbas fictitius   se.obs  24.66763
9  Cliona celata complex  S.chao1 850.68600
10        Cliona viridis  S.chao1 462.54650
11      Dysidea fragilis  S.chao1 701.74990
12     Phorbas fictitius  S.chao1 853.32610
13 Cliona celata complex se.chao1  65.16366
14        Cliona viridis se.chao1  45.57289
15      Dysidea fragilis se.chao1  73.82693
16     Phorbas fictitius se.chao1  57.73494

      

Then I want the data variables to S

be split by period ( .

) into two variables that I will call type

and var

. type

contains the values S

or se

and var

contains obs

orchao1

mdat <- separate(mdat, S, c("type","var"))

      



which gives:

                 Group.1 type   var     value
1  Cliona celata complex    S   obs 499.71430
2         Cliona viridis    S   obs 285.50000
3       Dysidea fragilis    S   obs 358.66670
4      Phorbas fictitius    S   obs 525.91670
5  Cliona celata complex   se   obs  59.32867
6         Cliona viridis   se   obs  51.68736
7       Dysidea fragilis   se   obs  61.03096
8      Phorbas fictitius   se   obs  24.66763
9  Cliona celata complex    S chao1 850.68600
10        Cliona viridis    S chao1 462.54650
11      Dysidea fragilis    S chao1 701.74990
12     Phorbas fictitius    S chao1 853.32610
13 Cliona celata complex   se chao1  65.16366
14        Cliona viridis   se chao1  45.57289
15      Dysidea fragilis   se chao1  73.82693
16     Phorbas fictitius   se chao1  57.73494

      

The last step in data processing is to decompose the current compact data so that we have columns S

and se

which we do with spread()

(this is a bit like casting in change)

mdat <- spread(mdat, type, value)

      

what gives us

mdat

> mdat
                Group.1   var        S       se
1 Cliona celata complex chao1 850.6860 65.16366
2 Cliona celata complex   obs 499.7143 59.32867
3        Cliona viridis chao1 462.5465 45.57289
4        Cliona viridis   obs 285.5000 51.68736
5      Dysidea fragilis chao1 701.7499 73.82693
6      Dysidea fragilis   obs 358.6667 61.03096
7     Phorbas fictitius chao1 853.3261 57.73494
8     Phorbas fictitius   obs 525.9167 24.66763

      

Now let's do it, we can build

ggplot(mdat, aes(x = Group.1, y = S, fill = var)) +
    geom_bar(position = "dodge", stat = "identity") +
    geom_errorbar(mapping = aes(ymax = S + se, ymin = S - se),
                  position = position_dodge(width=0.9), width = 0.25)

      

You only need one call geom_errorbar()

because it has aesthetics ymax

and ymin

that can be installed at the same time.

This gives

enter image description here

+5


source







All Articles