0

继此 如何添加将函数应用到现有数据框的结果?

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 的费率。

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

4

1 回答 1

1

您可以在 中放置一些简单的子集条件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")
于 2019-03-24T23:30:59.680 回答