Reputation: 145
my data frame goes like this:
NUMBER MEDIUM DATE DAY TREAT NITRAT SILI PHOSPHAT
1 NUT1 15/07/2016 two NBNA 20.831 20.946 0.518
2 NUT2 15/07/2016 two NBNA 26.594 25.075 1.059
3 NUT3 15/07/2016 two NBNA 47.565 42.088 7.733
4 NUT4 15/07/2016 two NBNA 61.320 53.264 6.331
5 NUT5 15/07/2016 two NBNA 64.918 52.058 4.889
6 NUT1 15/07/2016 two NBNA 19.876 22.000 0.852
7 NUT2 15/07/2016 two NBNA 29.642 29.572 1.184
8 NUT3 15/07/2016 two NBNA 37.969 35.015 4.293
9 NUT4 15/07/2016 two NBNA 56.724 45.986 6.880
10 NUT5 15/07/2016 two NBNA 66.006 53.175 5.893
11 NUT1 15/07/2016 two NBNA 21.835 20.710 0.770
12 NUT2 15/07/2016 two NBNA 25.996 26.213 1.149
13 NUT3 15/07/2016 two NBNA 48.526 46.167 3.977
14 NUT4 15/07/2016 two NBNA 56.767 47.089 6.684
15 NUT5 15/07/2016 two NBNA 64.841 54.039 6.628
16 NUT1 15/07/2016 two NBA1 13.179 18.723 0.826
17 NUT2 15/07/2016 two NBA1 18.631 23.537 0.859
I would like to put in a ggplot the values of NITRAT, SILI and PHOSPHAT(with error bars) sorted by MEDIUM. In summary, something like this(but also with error bars): to do so, I have tried the function melt:
df <- melt(nut, id.vars='MEDIUM')
however using this function does not allow me to use facet_grid afterwards
ggplot() +
geom_bar(data=df, aes(x=MEDIUM, y=value, fill=MEDIUM),
stat="identity", colour="black") + facet_grid(~TREAT)
Upvotes: 1
Views: 125
Reputation: 93761
You've got a few issues to clean up here:
First, only the columns that you want to "stack" (that is, use for the fill
aesthetic) should not be in id.vars
:
df <- melt(nut, id.vars=c('MEDIUM','NUMBER','DATE','DAY','TREAT')
Try the above code with different combinations of id.vars
to get some intuition for how it works.
fill=variable
will now give you bars for each of the three types of measurements (NITRAT, SILI, and PHOSPHAT). TREAT
will still be a column in the melted data frame because it was one of the id.vars
, so you can use it for facetting.
One other issue is that it looks like you actually want the bars to be the mean of all the values that go into that bar. So you don't want stat="identity"
. Instead, use stat_summary
to calculate the means and plot them as bars.
To add errorbars, use another call to stat_summary
. I've used bootstrapped confidence intervals (mean_cl_boot
). For classical confidence intervals, use mean_cl_normal
.
pd=position_dodge(0.8)
ggplot(df, aes(x=MEDIUM, y=value, fill=variable)) +
stat_summary(fun.y=mean, geom="bar", width=0.7, position=pd) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", position=pd, width=0.3) +
facet_grid(. ~ TREAT) +
theme_bw()
For a plot like this, points might be better than bars. Here we plot the error bars first instead of second because we want the points "on top of" the bars. It won't matter here because both are the same color, but I do it out of habit as I often use different colors or transparency for the error bars:
pd=position_dodge(0.4)
ggplot(df, aes(x=MEDIUM, y=value, colour=variable)) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", position=pd, width=0.3) +
stat_summary(fun.y=mean, geom="point", position=pd) +
facet_grid(. ~ TREAT) +
theme_bw()
Upvotes: 2