在过去,我使用RStudio创建ggplot2,然后从RSudio中将它们作为PDF导出.这非常有效.
我现在正在尝试使用knitr自动化,但我无法确定在何处设置图形高度和重量以创建高质量输出.
这是我目前的尝试,但"并排"图表不是,旋转的风景图不是,分辨率似乎很低.
我很感激任何建议.似乎ggplot2和knitr都在积极开发中,这很好,但是,互联网搜索已经引领我的步伐.LaTeX对我来说是新的.我也很欣赏这套工具的一般工作流程策略.提前致谢.
\documentclass[letterpaper]{article}
\usepackage{lscape}
\begin{document}
<<load, echo=FALSE, results='hide', warning=FALSE, message=FALSE>>=
require(ggplot2)
@
Two on the first page.
<<first, echo=FALSE, fig.height=3, fig.cap="This is first", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
Blah, blah, blah.
<<second, echo=FALSE, fig.height=3, fig.cap="This is second", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\newpage
Second page.
Side by side images:
<<third, echo = FALSE, out.width="2in", fig.cap='Side by side'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\newpage
\begin{landscape} …Run Code Online (Sandbox Code Playgroud) 我有一个数据集,包括年和月的案例.缺少几个月,我想创建那些月份的案例数为零的行.
这是一个例子,我目前的蛮力方法.谢谢你的任何指示.显然,我是新手.
# fake data
library(plyr)
rm(FakeData)
FakeData <- data.frame(DischargeYear=c(rep(2010, 7), rep(2011,7)),
DischargeMonth=c(1:7, 3:9),
Cases=trunc(rnorm(14, mean=100, sd=20)))
# FakeData is missing data for some year/months
FakeData
# Brute force attempt to add rows with 0 and then total
for(i in 1:12){
for(j in 1:length(unique(FakeData$DischargeYear))){
FakeData <- rbind(FakeData, data.frame(
DischargeYear=unique(FakeData$DischargeYear)[j],
DischargeMonth=i,
Cases=0))
}
}
FakeData <- ddply(FakeData, c("DischargeYear","DischargeMonth"), summarise, Cases=sum(Cases))
# FakeData now has every year/month represented
FakeData
Run Code Online (Sandbox Code Playgroud)