Following on from this How to add the results of applying a function to an existing data frame?
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()
gives me what I need.
What I'd like to do is plot this on a scatter plot with a horizontal line equal to one of the rates I've calculated. Rather than adding it manually.
I thought this might work
DATA%>%filter(DISEASE== "Chicky Pox")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(yintercept=20, linetype="dashed", color = "red")
Which it does. It gives me a line at 20. But how could I make it give me a line equal to the value of rate for area A (for example). This way you could quickly see which rates were above or below the rate of A.
Again. Apologies, this is simple. But it's late...
You can put some simple subsetting conditions in aes()
, which means this is pretty straightforward as long as the conditions aren't too complex:
DATA %>%
filter(DISEASE== "Chicky Pox") %>%
ggplot(aes(x=AREA, y=rate)) +
geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")