What is this knitr magic that produces a plot by calling <<plot_this>>?

余生长醉 提交于 2019-12-22 08:04:05

问题


I am trying to work out how this report on a STAN model for the 2016 US presidential elections is generated using rmarkdown:

https://raw.githubusercontent.com/pkremp/polls/master/report.Rmd

I have looked at the source code available on github, which contains lines like:

# Electoral College 

```{r echo=FALSE, message=FALSE, fig.align='center'}
<<plot_ev>>
```

The command <<plot_ev>> seems to generate plots from code in graphs.R:

# Not accounting for the EV allocation rules of Nebraska and Maine

# @knitr plot_ev

ggplot() + 
    geom_histogram(data = data.frame(ev = result_ev_all_states), aes(ev, fill = ifelse(ev >= 270,"Clinton","Trump")), binwidth = 1) + 
    scale_fill_manual(values=c("#6E90F8", "#FF6666"), guide = guide_legend(title = "Winner")) +
    xlab("Electoral Votes for Clinton") + 
    theme(axis.ticks.y = element_blank(), axis.text.y = element_blank(), axis.line.y = element_blank(), axis.title.y = element_blank()) +
    ggtitle(paste("EV distribution - Pr(Clinton wins) = ", round(mean(result_ev_all_states >= 270)*100, 0), "%", sep = "")) 

How does this work?

来源:https://stackoverflow.com/questions/40967975/what-is-this-knitr-magic-that-produces-a-plot-by-calling-plot-this

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!