ggplot: using geom_smooth to plot variance - r

I am making a time series plot with ggplot. I have the mean and the variance of the 5 variables of interest. I can plot the variance with error bars, but when I try to do it with geom_smooth()/stat_smooth() it doesn't work well.
ggplot(data = df4, aes(x = times, y = value, colour = variable)) + geom_line()+
geom_errorbar(aes(ymin = value-var, ymax = value+var))+
geom_errorbarh(aes(xmin = value-var, xmax = value+var))+ geom_smooth()
Output figure

Related

Violin plot with confidence interval in r

How can I add a confidence interval to this violin plot?
df <- data.frame("Need" = c(3,4.3,4.5,2.2,5.1,5.2), "Condition" = c("A","A","A","B","B","B"))
ggplot(df,aes(x = Condition, y = Need, fill=Condition)) +
geom_violin() +
stat_summary(fun.data = "mean_cl_boot", geom = "pointrange",
colour = "red") +
ggtitle("Needs by condition violin plot"))
I can't attach pictures yet, but you get the gist. With this code I can create violin plots with standard deviation lines for each violin plot, but I'd add 95% confidence interval lines.
Any ideas?
What you can do is first calculate the error bars per condition and after that add them by using geom_errorbar like this:
library(tidyverse)
stats <- df %>%
group_by(Condition) %>%
summarise(Mean = mean(Need), SD = sd(Need),
CI_L = Mean - (SD * 1.96)/sqrt(6),
CI_U = Mean + (SD * 1.96)/sqrt(6))
ggplot() +
geom_violin(df, mapping = aes(x = Condition, y = Need, fill=Condition)) +
stat_summary(fun.data = "mean_cl_boot", geom = "pointrange",
colour = "red") +
geom_point(stats, mapping = aes(Condition, Mean)) +
geom_errorbar(stats, mapping = aes(x = Condition, ymin = CI_L, ymax = CI_U), width = 0.2) +
ggtitle("Needs by condition violin plot")
Output:

Confidence interval graph in ggplot: how to make it more clearer?

I have continuous data (spectra) and I would like to determine the significance of the differences. I tried to do it using confidence intervals in r, ggplot.
Here is the code:
ggplot(df, aes(x = df$wn, y = df$value)) +
geom_line(aes(x = df$wn, y = df$value, colour = group)) +
geom_ribbon(aes(x = df$wn, ymin = df$lower, ymax = df$upper, fill = group))
I have around 15 spectra and the graph looks indistinguishable:
How could I make it clearer?

bar graph (barplot) subdivided with standard error and 3 variables (Stage x Dose x mortality) and factors levels [duplicate]

I made a simple barplot with ggplot2 comparing the mean lifespan (age) of males and females for 2 insect species.
My code looks like this, with "dataset" being, well, my data set...
gplot(dataset, aes(Species, Age, fill=Sex))+
stat_summary(fun.y = mean, geom = "bar", position = "dodge")+
scale_fill_manual(values = c("Grey25", "Grey"))+
theme(legend.title = element_blank())+
scale_y_continuous(limits = c(0,15))
I tried using the following code to manually enter the value of the meanĀ±SE to set the limits for the error bar. For the sake of simplicity, let's assume mean=10 and SE=0.5 for males of species1.
geom_errorbar(aes(ymin=9.5, ymax=10.5),width=.2,position=position_dodge(.9))
This code does indeed work, but it sets the same error bars for each bar in my plot.
How can I add error bars equal to the corresponding SE for each bar in my plot?
I am fairly new to ggplot and R in general so any help/advice is welcome.
You don't need more than to add stat_summary(geom = "errorbar", fun.data = mean_se, position = "dodge") to your plot:
library(ggplot2)
ggplot(diamonds, aes(cut, price, fill = color)) +
stat_summary(geom = "bar", fun = mean, position = "dodge") +
stat_summary(geom = "errorbar", fun.data = mean_se, position = "dodge")
If you prefer to calculate the values beforehand, you could do it like this:
library(tidyverse)
pdata <- diamonds %>%
group_by(cut, color) %>%
summarise(new = list(mean_se(price))) %>%
unnest(new)
pdata %>%
ggplot(aes(cut, y = y, fill = color)) +
geom_col(position = "dodge") +
geom_errorbar(aes(ymin = ymin, ymax = ymax), position = "dodge")
You can add an error bar on your barplot with the geom_errorbar geom.
You need to supply the ymin and ymax, so you need to compute it manually.
From the geom_errorbar help page:
p + geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2)

ggplot2 - using two different color scales for same fill in overlayed plots

A very similar question to the one asked here. However, in that situation the fill parameter for the two plots are different. For my situation the fill parameter is the same for both plots, but I want different color schemes.
I would like to manually change the color in the boxplots and the scatter plots (for example making the boxes white and the points colored).
Example:
require(dplyr)
require(ggplot2)
n<-4*3*10
myvalues<- rexp((n))
days <- ntile(rexp(n),4)
doses <- ntile(rexp(n), 3)
test <- data.frame(values =myvalues,
day = factor(days, levels = unique(days)),
dose = factor(doses, levels = unique(doses)))
p<- ggplot(data = test, aes(x = day, y = values)) +
geom_boxplot( aes(fill = dose))+
geom_point( aes(fill = dose), alpha = 0.4,
position = position_jitterdodge())
produces a plot like this:
Using 'scale_fill_manual()' overwrites the aesthetic on both the boxplot and the scatterplot.
I have found a hack by adding 'colour' to geom_point and then when I use scale_fill_manual() the scatter point colors are not changed:
p<- ggplot(data = test, aes(x = day, y = values)) +
geom_boxplot(aes(fill = dose), outlier.shape = NA)+
geom_point(aes(fill = dose, colour = factor(test$dose)),
position = position_jitterdodge(jitter.width = 0.1))+
scale_fill_manual(values = c('white', 'white', 'white'))
Are there more efficient ways of getting the same result?
You can use group to set the different boxplots. No need to set the fill and then overwrite it:
ggplot(data = test, aes(x = day, y = values)) +
geom_boxplot(aes(group = interaction(day, dose)), outlier.shape = NA)+
geom_point(aes(fill = dose, colour = dose),
position = position_jitterdodge(jitter.width = 0.1))
And you should never use data$column inside aes - just use the bare column. Using data$column will work in simple cases, but will break whenever there are stat layers or facets.

R ggplot2: Add means as horizontal line in a boxplot

I have created a boxplot using ggplot2:
library(ggplot2)
dat <- data.frame(study = c(rep('a',50),rep('b',50)),
FPKM = c(rnorm(1:50),rnorm(1:50)))
ggplot(dat, aes(x = study, y = FPKM)) + geom_boxplot()
The boxplot shows the median as a horizontal line across each box.
How do I add a dashed line to the box representing the mean of that group?
Thanks!
You can add horizontal lines to plots by using stat_summary with geom_errorbar. The line is horizontal because the y minimum and maximum are set to be the same as y.
ggplot(dat, aes(x = study, y = FPKM)) +
geom_boxplot() +
stat_summary(fun.y = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
width = .75, linetype = "dashed")

Resources