Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在ggplot曲线中添加数据点?_R_Ggplot2 - Fatal编程技术网

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

R 如何在ggplot曲线中添加数据点?,r,ggplot2,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

我正在使用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
结构(列表值=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),剂量=结构(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,2L,1L,
3L,2L,1L,3L,2L,1L,3L,2L,1L,3L,2L,1L),标签=c(“2.5”,
“5”,“10”,class=“factor”),时间=c(0L,0L,0L,0L,0L,
0L、0L、0L、10L、10L、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),row.names=c(NA,-36L),class=“data.frame”)

my_data$dose要绘制的点存在于原始数据集
my_data
中,但不存在于聚合数据中。在后者中,仅存在平均值。所以参数
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

p非常感谢。这真的很有帮助。我想知道,如果我可以显示额外的平均误差条(即一些点(平均)的中间上下胡须)。这可能吗?@user12582271是的,可能。使用相同的技巧,在
geom_point
中,将适当的数据集传递给参数
data
,并将
aes
设置为要绘制的值。