3

我是 R 新手,我编写了下面的交互图,为此我想要两条虚线分别连接两个点"coral"和两个"darkgoldenrod2"点:


df <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Area,~Time,
                      invlogit(-0.033886), invlogit(-0.517223067), invlogit(0.449451067), "SNP", "Day",
                      (invlogit(-0.9231219)+invlogit(-0.3786)), 0.5727 ,0.8087, "SNP", "Night",
                      invlogit(-0.9231219), invlogit(-1.406458967), invlogit(-0.439784833),"LGCA", "Day",
                      invlogit(-0.1604356), invlogit(-0.643772667) ,invlogit(0.322901467), "LGCA","Night")
df


dfnew <- df %>% 
  mutate(ymin = Proportion - Lower,
         ymax = Proportion + Upper)

p <-   ggplot(data = dfnew, aes(x = Time, y = Proportion, color=Area)) +

  geom_point(size = 6, stroke = 0, shape = 16, 
             position = position_dodge(width = 0.1))+
  geom_errorbar(aes(y=Proportion, ymin = Lower, ymax = Upper),width=0.1,size=1,
                position = position_dodge(width = 0.1)) + 
  theme(axis.text=element_text(size=15),
        axis.title=element_text(size=20)) +
  scale_color_manual(values = c("SNP" = "coral", 
                                "LGCA" = "darkgoldenrod2"))
p

在 SO 上阅读其他帖子时,我使用了命令行:+geom_line(aes(group = 1),size=2)

然而,这并没有产生所需的情节,如下所示:这个

对此的任何帮助都非常感谢!

4

1 回答 1

1

您应该添加group=Area到您的ggplot映射中,然后您只需要调用geom_line. 你也不需要y=Positiongeom_errorbar.

p <- ggplot(data = dfnew, aes(x = Time, y = Proportion, color=Area, group=Area)) +
    geom_point(size = 6, stroke = 0, shape = 16, 
               position = position_dodge(width = 0.1))+
    geom_errorbar(aes(ymin = Lower, ymax = Upper), width=0.1, size=1,
                  position = position_dodge(width = 0.1)) + 
    theme(axis.text=element_text(size=15),
          axis.title=element_text(size=20)) +
    scale_color_manual(values = c("SNP" = "coral", 
                                  "LGCA" = "darkgoldenrod2")) +
    geom_line(size=2)
p

例子

于 2019-08-21T13:40:46.100 回答