我正在尝试使用Rstudio为PDF格式的工作创建标准的月度报告,并且希望将ggplot输出与数字表结合使用 - 一张新图表每行上的单元格。我是新来的降价,乳胶,潘多克和knitr,所以这是我的一个雷区。使用markdown,rstudio和knitr对齐表格中的图像
我已经找到了如何使用kable插入图表,但图像未与同一行上的文本对齐。
我已经把使用虚拟数据在我的问题的底部有些(rstudio降价)的代码,这里有一些图像,显示了我想要做的,这个问题我有
Example of graphic I want to incorporate into table
This is what the table looks like with the misaligned text and images
您可以看到文本和图像没有对齐。如果我将这些图像放在桌子上,桌子很漂亮而且很小巧,放置图像意味着桌子分布在多个页面上,尽管图像本身并不高。
任何建议欢迎 - 代码片段加倍。
非常感谢
title: "Untitled"
output: pdf_document
---
This example highlights the issue I'm having with formatting a nice table with the graphics and the vertical alignment of text.
```{r echo=FALSE, results='hide', warning=FALSE, message=FALSE}
## Load modules
library(dplyr)
library(tidyr)
library(ggplot2)
## Create a local function to plot the z score
varianceChart <- function(df, personNumber) {
plot <- df %>%
filter(n == personNumber) %>%
ggplot() +
aes(x=zscore, y=0) +
geom_rect(aes(xmin=-3.32, xmax=-1.96, ymin=-1, ymax=1), fill="orange2", alpha=0.8) +
geom_rect(aes(xmin=1.96, xmax=3.32, ymin=-1, ymax=1), fill="olivedrab3", alpha=0.8) +
geom_rect(aes(xmin=min(-4, zscore), xmax=-3.32, ymin=-1, ymax=1), fill="orangered3") +
geom_rect(aes(xmin=3.32, xmax=max(4, zscore), ymin=-1, ymax=1), fill="chartreuse4") +
theme(axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank()) +
geom_vline(xintercept=0, colour="black", alpha=0.3) +
geom_point(size=15, shape=4, fill="lightblue") ##Cross looks better than diamond
return(plot)
}
## Create dummy data
Person1 <- rnorm(1, mean=10, sd=2)
Person2 <- rnorm(1, mean=10, sd=2)
Person3 <- rnorm(1, mean=10, sd=2)
Person4 <- rnorm(1, mean=10, sd=2)
Person5 <- rnorm(1, mean=10, sd=2)
Person6 <- rnorm(1, mean=6, sd=1)
## Add to data frame
df <- data.frame(Person1, Person2, Person3, Person4, Person5, Person6)
## Bring all samples into one column and then calculate stats
df2 <- df %>%
gather(key=Person, value=time)
mean <- mean(df2$time)
sd <- sqrt(var(df2$time))
stats <- df2 %>%
mutate(n = row_number()) %>%
group_by(Person) %>%
mutate(zscore = (time - mean)/sd)
graph_directory <- getwd() #'./Graphs'
## Now to cycle through each Person and create a graph
for(i in seq(1, nrow(stats))) {
print(i)
varianceChart(stats, i)
ggsave(sprintf("%s/%s.png", graph_directory, i), plot=last_plot(), units="mm", width=50, height=10, dpi=1200)
}
## add a markup reference to this dataframe
stats$varianceChart <- sprintf('![](%s/%s.png)', graph_directory, stats$n)
df.table <- stats[, c(1,2,5)]
colnames(df.table) <- c("Person Name", "Time taken", "Variance Chart")
```
```{r}
library(knitr)
kable(df.table[, c(1,2)], caption="Rows look neat and a sensible distance apart")
kable(df.table, caption="Rows are separated a long way apart and images and text are misaligned")
```
感谢您抽出宝贵时间回答。如何将乳胶命令合并到.Rmd文件中并将它们解释为乳胶?我目前点击'KnitR',pdf是自动生成的。你的代码运行,但不是图像,我看到表中的乳胶指令。 –
而且你一对一地复制它?您在表中看到的LaTeX代码似乎是正确的?我可以毫无问题地运行这个文件。尝试保持tex文件,并看看源可能 –
谢谢 - 我再次尝试,它的工作。 –