在ggplot2中向散点图添加水平线

Adding a horizontal line to a scatterplot in ggplot2

从这里开始

library (tidyverse)
library (epitools)


# here's my made up data

DISEASE = c("Marco Polio","Marco Polio","Marco Polio","Marco Polio","Marco Polio",
            "Mumps","Mumps","Mumps","Mumps","Mumps",
            "Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox")
YEAR = c(2011, 2012, 2013, 2014, 2015,
         2011, 2012, 2013, 2014, 2015,
         2011, 2012, 2013, 2014, 2015)
VALUE = c(82,89,79,51,51,
          79,91,69,89,78,
          71,69,95,61,87)
AREA =c("A", "B","C")

DATA = data.frame(DISEASE, YEAR, VALUE,AREA)


DATA %>%
    mutate(POPN = case_when(
        AREA == "A" ~ 2.5,
        AREA == "B" ~ 3,
        AREA == "C" ~ 7,
        TRUE ~ 0)) %>%
    group_by(DISEASE,AREA,POPN) %>%
    count(AREA) %>%
    mutate(res = list(pois.byar(n, POPN))) %>%
    unnest()

给我我需要的。

我想做的是将其绘制在散点图上,水平线等于我计算的其中一个比率。而不是手动添加它。

我认为这可能有效

DATA%>%filter(DISEASE== "Chicky Pox")%>%
  ggplot(aes(x=AREA, y=rate)) +geom_point() +
  geom_hline(yintercept=20, linetype="dashed", color = "red")

它是做什么的。 它在 20 处给了我一条线。 但是我怎样才能让它给我一条等于区域 A 的费率值的线(例如)。 这样您就可以快速查看哪些汇率高于或低于 A 的汇率。

再次。抱歉,这很简单。但是已经晚了...

您可以在 aes() 中放置一些简单的子集条件,这意味着只要条件不太复杂,这就非常简单:

DATA %>%
    filter(DISEASE== "Chicky Pox") %>%
    ggplot(aes(x=AREA, y=rate)) +
    geom_point() +
    geom_hline(aes(yintercept=rate[AREA == "A"]), 
               linetype="dashed", color = "red")