如何在ggplot曲线中添加数据点?

时间:2020-01-13 05:25:49

标签: r ggplot2

我正在使用ggplot绘制曲线。我拥有我计算出的均值和se并存储在数据中的数据。然后,我使用ggplot绘制曲线。到目前为止,一切看起来都不错。但是,我想将每个时间点和剂量的所有三个重复数据点分散在各自的平均误差条上。谁能帮我查询。这是我的代码:

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 个答案:

答案 0 :(得分:1)

要绘制的点显示在原始数据集data = my_data中,但不存在于汇总数据中。在后者中,仅存在平均值。因此,参数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

public static ArrayList < Integer > bubbleSort(ArrayList < Integer > ar) {

    for (int i = 0; i < ar.size() - 1; i++) {
        int indexMax = i;
        for (int j = 1; j < ar.size(); j++) {
            if (ar.get(indexMax) > ar.get(j)) {
                indexMax = j;
            }
        }
        if (indexMax != i) {
            int temp = ar.get(i);
            ar.set(i, ar.get(indexMax));
            ar.set(indexMax, temp);
        }
    }
    return ar;

}

enter image description here