1
votes

I am plotting a curve using ggplot. I have the data which I calculated mean and se and stored in data. Then I used ggplot to plot the curve. So far everything looks good. But, I would like to scatter all three replicate data points for each time point and dose over their respective mean error bar. Can anyone help with my query. Here is my code:

structure(list(values = c(5L, 3L, 2L, 6L, 4L, 1L, 5L, 3L, 1L, 
25L, 15L, 10L, 30L, 17L, 9L, 27L, 14L, 8L, 75L, 45L, 20L, 80L, 
50L, 25L, 90L, 50L, 30L, 150L, 100L, 50L, 160L, 110L, 60L, 170L, 
120L, 70L), dose = structure(c(3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 
3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L), .Label = c("2.5", 
"5", "10"), class = "factor"), time = c(0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 20L, 
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 30L, 30L, 30L, 30L, 30L, 
30L, 30L, 30L, 30L)), row.names = c(NA, -36L), class = "data.frame")


my_data$dose <- as.factor(my_data$dose)
head(my_data)

library(dplyr)
data <- group_by(my_data, dose, time) %>%
 summarise(
  count = n(),
  mean = mean(values, na.rm = TRUE),
  sd = sd(values, na.rm = TRUE),
  se   = sd / sqrt(n())
  )

data

p <- ggplot(data, aes(x=time, y=mean, group=dose)) +
 geom_point(size = 1)+
 geom_smooth(aes(linetype=dose), color = "black", se = F, size = 0.5)+
 geom_errorbar(aes(ymin=mean - se, ymax=mean + se), width=1.8) + 
 labs(x ="sec", y = "Units") +
 scale_x_continuous(breaks = seq(0, 35, 5), limits = c(-1, 35), expand = c(0, 0))+
 scale_y_continuous(breaks = seq(0, 200, 20), limits = c(-10, 200), expand = c(0, 0))+
 scale_linetype_manual(values=c( "dashed",  "dotted", "solid"),
                        labels = c("2.5", "5", "10"))
p

enter image description here

1

1 Answers

1
votes

The points to be plotted are present in the original data set my_data but not in the aggregated data. In the latter only the mean values are present. So argument data = my_data must be passed to geom_point.

p <- ggplot(data, aes(x = time, y = mean, group = dose)) +
  geom_point(data = my_data, 
             mapping = aes(x = time, y = values), 
             size = 1) +
  geom_smooth(data = data, 
              mapping = aes(linetype = dose), 
              color = "black", 
              se = FALSE, size = 0.5)+
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width=1.8) + 
  labs(x ="sec", y = "Units") +
  scale_x_continuous(breaks = seq(0, 35, 5), 
                     limits = c(-1, 35), expand = c(0, 0)) +
  scale_y_continuous(breaks = seq(0, 200, 20), 
                     limits = c(-10, 200), expand = c(0, 0)) +
  scale_linetype_manual(values = c( "dashed",  "dotted", "solid"),
                        labels = c("2.5", "5", "10"))

p

enter image description here