我正在尝试生成一个条形图,使得x轴是患者,每个患者都有多个样本.因此,例如(使用mtcars数据作为数据的模板):
library("ggplot2")
ggplot(mtcars, aes(x = factor(cyl), group = factor(gear))) +
geom_bar(position = position_dodge(width = 0.8), binwidth = 25) +
xlab("Patient") +
ylab("Number of Mutations per Patient Sample")
Run Code Online (Sandbox Code Playgroud)
这会产生这样的东西:
每个条形图代表每个患者的样本.
我想通过使用颜色填充条形图来添加关于每个患者样本的其他信息(例如,每个患者样本中的不同类型的突变).我以为我可以像这样指定fill参数:
ggplot(mtcars, aes(x = factor(cyl), group = factor(gear), fill = factor(vs))) +
geom_bar(position = position_dodge(width = 0.8), binwidth = 25) +
xlab("Patient") +
ylab("Number of Mutations per Patient Sample")
Run Code Online (Sandbox Code Playgroud)
但这并不会为每个患者样本条形图产生"堆积条形图".我假设这是因为position_dodge()已设置.反正有没有绕过这个?基本上,我想要的是:
ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) +
geom_bar() +
xlab("Patient") +
ylab("Number of Mutations per Patient Sample")
Run Code Online (Sandbox Code Playgroud)
但是我列出的第一个图中有这些颜色.这可能与ggplot2有关吗?
我刚刚开始使用 rmarkdown、pandoc 和 knitr。我在尝试让 pandoc 获取比 rmarkdown 文档高一级的图像时遇到了很多困难。例如,考虑我们的项目目录是 ~/test,下面的 rmarkdown 位于 ~/test/scripts:
---
title: "test"
---
```{r global_options, include=FALSE}
library('knitr')
opts_knit$set(root.dir = '~/test')
```

Run Code Online (Sandbox Code Playgroud)
然后我Rscript -e "rmarkdown::render('scripts/test.Rmd')"从 ~/test 目录运行命令。它给了我错误:
pandoc: Could not fetch figures/test.svg
figures/test.svg: openBinaryFile: does not exist (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
我本以为通过将 root.dir 设置为项目目录,pandoc 将获取与该目录相关的文件?但似乎“工作目录”总是设置为相对于 rmarkdown 文档所在的位置?任何帮助,将不胜感激。谢谢,
>sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_CA.UTF-8 LC_NUMERIC=C LC_TIME=en_CA.UTF-8 LC_COLLATE=en_CA.UTF-8 LC_MONETARY=en_CA.UTF-8 LC_MESSAGES=en_CA.UTF-8
[7] LC_PAPER=en_CA.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] …Run Code Online (Sandbox Code Playgroud) 在我的rmarkdown文件中,我想知道是否可以在ar块内部并使用r-chunk本身内的fig.cap选项值.
例如:
```{r fig.cap = 'test'}
code
.
.
print(options$fig.cap)?
````
Run Code Online (Sandbox Code Playgroud)
在此先感谢任何帮助或建议,从哪里开始寻找
在dplyr v0.7.0中,.data引入了代词,允许我们用字符串引用变量。我只是好奇这种方法是否比“quosure”方法更受欢迎。例如,这是一种使用.data代词的方法:
varname <- "gear"
data_pronoun_method_df <- dplyr::mutate(mtcars, new_col = .data[[varname]] + 2)
Run Code Online (Sandbox Code Playgroud)
这与使用该quosure方法的示例进行了比较:
quo_varname <- rlang::quo(gear)
quo_method_df <- dplyr::mutate(mtcars, new_col = !! quo_varname + 2)
Run Code Online (Sandbox Code Playgroud)
两种方法产生相同的输出:
data_pronoun_method_df
# mpg cyl disp hp drat wt qsec vs am gear carb new_col
# 1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 6
# 2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 6
# 3 22.8 …Run Code Online (Sandbox Code Playgroud) 我使用bookdown::html_document2输出格式来呈现单个文档,以对图/表/方程进行自动编号,并交叉引用图/表/方程/部分。
有没有办法获取使用分页打印打印的数据框的表格标题。例如:
---
title: "Use caption with df_print set to page"
date: "`r Sys.Date()`"
output:
bookdown::html_document2:
df_print: paged
---
```{r}
knitr::kable(mtcars, caption = "mtcars caption")
```
```{r}
# Is there an option for a caption?
mtcars
```
Run Code Online (Sandbox Code Playgroud)
有了knitr::kable,我可以使用标题参数,它将出现在自动编号的 HTML 文件中。但我不清楚这是否可以通过分页打印实现?
我一直在尝试使用ggplots/reshape2生成一个2D矩形图,代码如下:
library(reshape2)
library(ggplot2)
m <- matrix( c('SNV', 'SNV', NA, NA, 'INDEL', 'SNV', 'INDEL', 'SNV', 'SNV/INDEL'), 3, 3 )
ggplot(melt(m), aes(Var1,Var2, fill=value)) + geom_raster() + xlab('Patient') + ylab('Gene')
Run Code Online (Sandbox Code Playgroud)
请注意,对于具有SNV/INDEL的图块,它将蓝色作为单独的类别进行颜色处理.我只是想知道是否有办法让它实际上有一个分色瓷砖,使瓷砖颜色为栗色/绿色(如瓷砖的一半是栗色,另一半是绿色)?
谢谢,
我一直在玩阅读器的read_delim_chunked功能。根据文档,不清楚如何或是否可能将参数传递到回调函数中。例如,来自文档示例:
# Cars with 3 gears
f <- function(x, pos) {
dplyr::filter(x, .data[["gear"]] == 3)
}
readr::read_csv_chunked(
readr::readr_example("mtcars.csv"),
readr::DataFrameCallback$new(f),
chunk_size = 5)
# A tibble: 15 x 11
mpg cyl disp hp drat wt qsec vs am gear carb
<dbl> <int> <int> <int> <dbl> <dbl> <dbl> <int> <int> <int> <int>
1 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
2 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
3 18.1 …Run Code Online (Sandbox Code Playgroud)