这是我的最后一个问题。我花了一个小时左右的时间来弄清楚如何将用于过滤数据框的变量传递给所生成图形的标题。
在我之前的问题之后。
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")
我认为这
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)
为什么不呢? 它为Marco Polio生成图表,但使用Chicky Pox作为标题。
我想要的是(错误代码) ggtitle == filter(疾病)
因为在此之后我要做的是走走走走,以获取每种感染的所有图表,并且我想自动为标题加上标题。
Ta。
编辑: 我尝试了以下建议,但效果不佳。
我已经尝试过了
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))
没有运气。
与疾病分组时的疾病是否有关?
答案 0 :(得分:3)
似乎您想要一个可以输入疾病并创建情节的函数。
<form name="Send-mail" action="sendEmail.php" method="post">
或者一次创建所有内容...
disease_plot <- function(disease_of_interest) {
DATA %>%
filter(DISEASE == disease_of_interest) %>%
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(disease_of_interest)
}
disease_plot("Marco Polio")
disease_plot("Chicky Pox")
disease_plot("Mumps")
答案 1 :(得分:1)
那是因为您将整个DATA$DISEASE
用作标题,并且似乎只是抓住了该column
中的最后一个值。更简单的方法是先制作经过过滤的dataframe
,然后将其输入我认为的plot
。
df <- DATA%>%filter(DISEASE== "Marco Polio")
ggplot(data = df, 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(df$DISEASE)
最后,尽管我的解决方案不是叫DATA $而是叫DISEASE 但是,这在过滤其他疾病时似乎无法正常工作。我认为您也必须在ggtitle中包含DISEASE子集,或者更好地使用第一个函数或其他用户发布的其他答案。
无法正常工作:
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(DISEASE)
答案 2 :(得分:1)
最后,我接受了斯蒂芬和马克的建议和帮助,将其与我最初的计划相提并论。
这里是:
walk(unique(DATA$DISEASE), function(disease_of_interest) {
p <- DATA%>%filter(DISEASE== !!disease_of_interest)%>%
ggplot(aes(x=AREA, y=rate,y=rate,
ymin = rate-lower, ymax = rate+upper))+
geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label),+
ggtitle(paste0("Number of ",disease_of_interest,
" in 2018"))+
geom_errorbar(aes(ymin=lower, ymax=upper), width=.1)
print(p)
ggsave(paste("drive path",disease_of_interest, "plot.png"))+
scale_x_discrete(limits=c("C","A","B"))
})