考虑这个问题中的数据集.甲阴影可使用被绘制geom_rect
在ggplot2
如下.
data <- structure(list(Time = c(20L, 40L, 60L, 80L, 100L, 120L, 20L,
40L, 60L, 80L, 100L), Average = c(5.8, 6.1, 6.4, 6.7, 7, 7.7,
8.47, 9.317, 10.2487, 11.27357, 12.40093), Test = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Control",
"Exp"), class = "factor"), n = c(9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L), se = c(0.12, 0.145, 0.188, 0.99, 0.44, 0.32,
0.5, 0.88, 0.9, 0.33, …
Run Code Online (Sandbox Code Playgroud) 我想从R
包中提取示例代码并rmarkdown
自动在文件中运行它.
我能够使用utils::example
如下函数提取代码.
example("geom_histogram", package = "ggplot2", ask = F,
prompt.prefix = "", give.lines = TRUE)[-(1:5)]
Run Code Online (Sandbox Code Playgroud)
我尝试使用results="asis"
如下的块选项,但结果是作为代码输出而不是代码块.
```{r,echo = FALSE, results="asis"}
cat("```{r}")
library(ggplot2)
cat(paste(example("geom_histogram", package = "ggplot2", ask = F,
prompt.prefix = "", give.lines = TRUE)[-(1:5)], collapse = "\n"))
cat("```")
```
Run Code Online (Sandbox Code Playgroud)
我想将代码作为代码块,输出与http://ggplot2.tidyverse.org/reference/geom_histogram.html中的相同.怎么做到这一点?
我正在尝试使用rmarkdown
和创建 pdf 文档knitr
。我需要使用xcolor
带有一些选项的tex 包(例如:[table]
或[svgnames]
)。
每当我尝试- \usepackage[table]{xcolor}
在 YAML 标头中或在 下提到的序言 tex 文件中使用时pdf_document
includes
in_header:
,我都会收到以下错误:
! LaTeX Error: Option clash for package xcolor
Run Code Online (Sandbox Code Playgroud)
选项冲突是因为,knitr
引擎pdf_document
也xcolor
直接或通过另一个包间接加载包。我怀疑是后者,因为最近我更新了几个 tex 包后问题出现了。
一个可能的解决方案是在 tex 文件的开头,在行之前添加\PassOptionsToPackage{table}{xcolor}
\documentclass[]{article}
。当我手动执行此操作时,问题已解决。
将它添加到序言 tex 文件或 YAML 标头中,仅\documentclass[]
在 tex 文件中的行之后添加它。
如何解决这个问题?
是否有任何knitr
hook
函数可以在 tex 文件中的\PassOptionsToPackage{}{}
行前添加行\documentclass[]
。?
---
title: "xcolor options clash"
author: "xcolor, options clash" …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 绘制如下所示的分箱散点图ggplot2
。
library(ggplot2)
bks = seq(from = 0, to = 10000, by = 1000)
d <- ggplot(diamonds, aes(carat, price)) + theme_bw()
d + geom_point(alpha = 0.01)
Run Code Online (Sandbox Code Playgroud)
当我使用 时geom_hexbin
,箱中的计数未准确映射到梯度比例。
d + geom_hex(aes(fill = after_stat(count)), bins = 30, colour = "white") +
scale_fill_distiller(palette = "Spectral", breaks = bks) +
geom_text(data = diamonds, aes(x = carat, y = price, label = after_stat(count)),
stat="binhex", bins=30, show.legend=FALSE,
colour="black", size=2.5)
Run Code Online (Sandbox Code Playgroud)
例如,计数为 5809 和 5556 的容器仍显示为蓝色。
然而,使用geom_bin_2d
,映射似乎是准确的
d + geom_bin_2d(aes(fill = …
Run Code Online (Sandbox Code Playgroud) 我有一个data.table G
如下.
d <- list( c("SD1:LUSH", "SD44:CANCEL", "SD384:FR563", "SD32:TRUMPET"), c("SD23:SWITCH", "SD1:LUSH", "SD567:TREK"), c("SD42:CRAYON", "SD345:FOX", "SD183:WIRE"), c("SD345:HOLE", "SD340:DUST", "SD387:ROLL"), c("SD455:TOMATO", "SD86:RAY", "SD39:MATURE", "SD86:COSMIC"), c("SD12:PAINTING", "SD315:MONEY31", "SD387:SPRING"), c("SD32:TRUMPET", "SD1:FIELD", "SD40:GREEN", "SD40:PARK"))
d2 <- lapply(d, function(x) sapply(strsplit(x, ":"), "[", 1))
d <- lapply(d, function(x) paste0(unique(x), collapse=", "))
d2 <- lapply(d2, function(x) paste0(unique(x), collapse=", "))
d <- as.data.frame(as.matrix(lapply(d, paste0, collapse=", ")))
d2 <- as.data.frame(as.matrix(lapply(d2, paste0, collapse=", ")))
d <- as.data.frame(cbind(d,d2))
colnames(d) <- c("sdw", "sd")
d$sd <- as.character(d$sd)
d$sdw <- as.character(d$sdw)
G <- data.table( …
Run Code Online (Sandbox Code Playgroud) 我有一些字符串数据如下R
.
DT <- structure(list(ID = c(1, 2, 3, 4, 5, 6), GKT = c("G1:GRST, G45:KRPT",
"G48932:KD56", "G7764:MGI45, K7786:IRE4R, K45:TG45", "K4512:3345, G51:56:34, K22:45I67",
"K678:RT,IG, G123:TGIF, G33:IG56", "T4534:K456")), .Names = c("ID",
"GKT"), class = "data.frame", row.names = c(NA, 6L))
DT
ID GKT
1 1 G1:GRST, G45:KRPT
2 2 G48932:KD56
3 3 G7764:MGI45, K7786:IRE4R, K45:TG45
4 4 K4512:3345, G51:56:34, K22:45I67
5 5 K678:RT,IG, G123:TGIF, G33:IG56
6 6 T4534:K456
Run Code Online (Sandbox Code Playgroud)
我想out
从DT$GKT
using gsub
和regex
in 获取输出R
.
out …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用ggplot2::geom_bar
背景着色(使用ggplot2::geom_rect()
)根据分类变量绘制堆叠的条形图,如下所示:
shading <- data.frame(min = seq(from = 0.5, to = max(as.numeric(as.factor(diamonds$clarity))), by = 1),
max = seq(from = 1.5, to = max(as.numeric(as.factor(diamonds$clarity))) + 0.5, by = 1),
col = c(0,1))
ggplot() +
theme(panel.background = element_rect(fill = "transparent")) +
geom_bar(data = diamonds, mapping = aes(clarity, fill=cut)) +
geom_rect(data = shading,
aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf,
fill = factor(col), alpha = 0.1)) +
geom_bar(data = diamonds, mapping = aes(clarity, fill=cut)) + …
Run Code Online (Sandbox Code Playgroud) 考虑 adata.table
DT
如下。
DT <- iris
setDT(DT)
ad <- address(DT)
DT[, a := NA_integer_]
identical(address(DT), ad)
Run Code Online (Sandbox Code Playgroud)
我试图使用DT$a
循环顺序插入一些信息。
a1 <- sample(1:1000, 50)
a2 <- sample(1:1000, 50)
a3 <- sample(1:1000, 50)
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这会导致复制DT
以下方法。
DT$a[1:50] <- a1
identical(address(DT), ad)
Run Code Online (Sandbox Code Playgroud)
如何使用data.table
避免复制来做到这一点DT
?
我正在尝试使用ggplot2
以下方法绘制条形图:
library(ggplot2)
ggplot(mtcars, aes(factor(carb))) +
geom_bar() +
coord_flip()
Run Code Online (Sandbox Code Playgroud)
x轴是连续变量,而y轴是绝对变量(factor
)。
我想在每个条形后面添加备用阴影区域,以区分y轴上的因子。我知道我可以用geom_rect()
这个。当它是a时,如何计算该区域的y轴限制factor
?X轴为矩形限制将是-Inf
到Inf
。
我正在寻找与此图像类似的东西,但要寻找条形图而不是盒形图。
我有一些表格数据如下。
| | A | B | C | D |
|---|----------|--------------|------------------|---------------------------------------------------|
| | | p1 | p2 | pn |
| 1 | Lanterns | Bruce Wayne | Jean-Paul Valley | Dick Grayson; Terry McGinnis; Jean-Paul Valley |
| 2 | Bats | Alan Scott | Hal Jordan | Guy Gardner; John Stewart; Kyle Rayner; Simon Baz |
| 3 | Fates | Kent Nelson | Khalid Nassour | Hector Hall; Khalid Nassour; Khalid Ben-Hassin |
| 4 | …
Run Code Online (Sandbox Code Playgroud)