将xtable和ggplot2输出并排放置在Beamer中

时间:2018-11-28 19:53:01

标签: r ggplot2 knitr xtable beamer

MWE:

\documentclass[pdf, t, 10pt]{beamer}
\usetheme{Antibes}
\mode<presentation>{}
\usepackage{array}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash\hspace{0pt}}p{#1}}

\begin{document}
\begin{frame}[fragile]
<<echo=FALSE, fig.show='hold', results='asis', fig.width=3, fig.height=3, fig.align='right'>>=
# Ficticious data
grad <- c(0.846, 0.863, 0.852, 0.873)
counts <- c(500, 485, 520, 545)
year <- c(1, 2, 3, 4)
library(ggplot2)
library(xtable)

format_pct <- function(x){
  paste0(round(x * 100, 1), "%")
}
df <- data.frame(Year = as.integer(year),
                 Counts = as.integer(counts),
                 Grad_Rate = grad,
                 stringsAsFactors = FALSE)
p <- ggplot(df, aes(x = Year, y = Grad_Rate, weight = counts)) + 
  geom_point() +
  geom_smooth(method = "lm") + 
  theme_bw() + 
  scale_y_continuous(labels = format_pct, breaks = seq(0.5, 1, by = 0.05),
                     limits = c(0.8, 1)) + 
  xlab("Year") + 
  ylab("Graduation Rate")
print(xtable(df, align = c("l", "R{0.05\\textwidth}", "R{0.15\\textwidth}", "R{0.12\\textwidth}")), 
      include.rownames = FALSE,
      latex.environments="flushleft"
      )
p
@
\end{frame}

\end{document}

输出:

enter image description here

所需的输出:

我想摆脱出现在xtable列中的(出于某种奇怪的原因)多余的一行,并希望将xtable和ggplot2输出并排放置。如果必须调整宽度就很好。

1 个答案:

答案 0 :(得分:1)

您可以使用minipage获得所需的输出。例如。

PMPI_Waitall(392): MPI_Waitall(count=-1, req_array=0x7fff6c8c17a0, status_array=0x1) failed
PMPI_Waitall(359): Negative count, value is -1
[unset]: write_line error; fd=-1 buf=:cmd=abort exitcode=940193794
:
system msg for write_line failure : Bad file descriptor

输出为:

{{0}}