This follows on from my last question. I've spent an hour or so trying to work out how to pass the variable I use to filter my dataframe to the title of the graph that is generated.
Following on from my previous questions.
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<-
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== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")
I thought that this
x_label = "Area!!!"
y_label = "Rate!!!"
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(DATA$DISEASE)
Why doesn't it? It generates a chart for Marco Polio but uses Chicky Pox as the title.
What I want is (false code) ggtitle == filter(disease)
Because what I'm going to do after this is walk and purr to get every chart for every infection and I'd like to title automatically.
Ta.
EDIT: I've tried the suggestion below and it doesn't quite work.
I've tried this
DATA%>%filter(DISEASE== "Mumps")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
ggtitle(paste(DISEASE))
DATA%>%filter(DISEASE== "Mumps")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
ggtitle(as.character(DISEASE))
and no luck.
Does it have something to do with DISEASE becoming a FACTOR when it gets grouped?