在R中为两个分类点之间添加虚线

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

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

然而,如下所示的图表并不符合要求:this

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

1个回答

1
你应该在 ggplot 的映射中添加 group=Area,然后只需要调用 geom_line。在 geom_errorbar 中也不需要使用 y=Position
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

Example


谢谢你的帮助。这条线可以是虚线吗?还是需要使用不同的函数? - juansalix
1
当然可以,只需使用 geom_line(size=2, linetype="dotted") - Arienrhod

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接