R reprex 没有为非常高的图呈现正确的 ggplot 输出
R reprex not rendering correct ggplot output for very tall plots
当使用 ggplot2
创建非常高的水平条形图时,使用包 reprex
呈现的图会截断一些数据,而轴标签仍保持在正确的位置。这与相同代码的 ggplot 输出行为不同。
reprex
输出:
library(babynames)
library(dplyr)
library(ggplot2)
data("babynames")
bn <- babynames %>%
filter(year == 2015) %>%
arrange(-n) %>%
head(400) %>%
mutate(highlight = ifelse(n>12000, TRUE, FALSE)) %>%
arrange(name)
breaks <- bn %>% filter(highlight == TRUE) %>% pull(name)
ggplot(bn, aes(x=name, y=n, fill=highlight)) +
geom_col() +
scale_x_discrete(breaks = breaks)+
coord_flip() +
theme_classic()
由 reprex package (v0.2.1)
于 2018-09-19 创建
使用 ggsave()
保存 png 并将其上传到 Whosebug:
ggsave("long_example.png",
width = 4,
height = 6,
dpi=200)
在 ggsave()
版本中,Abigail 的高亮条显示正确,而底部的几个条(包括 Abigail 的)在 reprex 版本中消失了。这是怎么回事?
正如 Nick Larsen 所指出的,问题不在于底栏从图中掉落了;这是图表中的一些条形图,当图像尺寸和分辨率设置为默认值时未呈现。这种行为的必要性是一个明显的例子 pigeonhole principle (if my bars were pigeons and pixels were holes). reprex
is using knitr
under the hood to create markdown output, and offers a syntax to modify these restrictive defaults via markdown chunk options (see more reprex examples here):
reprex({
#+ setup, include = FALSE
knitr::opts_chunk$set(fig.width = 4, fig.height = 6, dpi = 200, warning = FALSE)
#+ actual-reprex-code
#{INSERT CODE HERE}
}, venue = 'so')
使用上面定义的图形选项,我的图形将有 1200 个垂直像素 space,足以容纳 400 个柱和周围的图表区域。将问题中的示例代码替换为 {INSERT CODE HERE}
,我们得到了期望的 reprex
输出,并应用了 knitr
代码块选项:
library(babynames)
library(dplyr)
library(ggplot2)
data("babynames")
bn <- babynames %>%
filter(year == 2015) %>%
arrange(-n) %>%
head(400) %>%
mutate(highlight = ifelse(n > 12000, TRUE, FALSE)) %>%
arrange(name)
breaks <- bn %>% filter(highlight == TRUE) %>% pull(name)
ggplot(bn, aes(x = name, y = n, fill = highlight)) +
geom_col() +
scale_x_discrete(breaks = breaks)+
coord_flip() +
theme_classic()
由 reprex package (v0.2.1)
创建于 2018-09-21
当使用 ggplot2
创建非常高的水平条形图时,使用包 reprex
呈现的图会截断一些数据,而轴标签仍保持在正确的位置。这与相同代码的 ggplot 输出行为不同。
reprex
输出:
library(babynames)
library(dplyr)
library(ggplot2)
data("babynames")
bn <- babynames %>%
filter(year == 2015) %>%
arrange(-n) %>%
head(400) %>%
mutate(highlight = ifelse(n>12000, TRUE, FALSE)) %>%
arrange(name)
breaks <- bn %>% filter(highlight == TRUE) %>% pull(name)
ggplot(bn, aes(x=name, y=n, fill=highlight)) +
geom_col() +
scale_x_discrete(breaks = breaks)+
coord_flip() +
theme_classic()
由 reprex package (v0.2.1)
于 2018-09-19 创建使用 ggsave()
保存 png 并将其上传到 Whosebug:
ggsave("long_example.png",
width = 4,
height = 6,
dpi=200)
在 ggsave()
版本中,Abigail 的高亮条显示正确,而底部的几个条(包括 Abigail 的)在 reprex 版本中消失了。这是怎么回事?
正如 Nick Larsen 所指出的,问题不在于底栏从图中掉落了;这是图表中的一些条形图,当图像尺寸和分辨率设置为默认值时未呈现。这种行为的必要性是一个明显的例子 pigeonhole principle (if my bars were pigeons and pixels were holes). reprex
is using knitr
under the hood to create markdown output, and offers a syntax to modify these restrictive defaults via markdown chunk options (see more reprex examples here):
reprex({
#+ setup, include = FALSE
knitr::opts_chunk$set(fig.width = 4, fig.height = 6, dpi = 200, warning = FALSE)
#+ actual-reprex-code
#{INSERT CODE HERE}
}, venue = 'so')
使用上面定义的图形选项,我的图形将有 1200 个垂直像素 space,足以容纳 400 个柱和周围的图表区域。将问题中的示例代码替换为 {INSERT CODE HERE}
,我们得到了期望的 reprex
输出,并应用了 knitr
代码块选项:
library(babynames)
library(dplyr)
library(ggplot2)
data("babynames")
bn <- babynames %>%
filter(year == 2015) %>%
arrange(-n) %>%
head(400) %>%
mutate(highlight = ifelse(n > 12000, TRUE, FALSE)) %>%
arrange(name)
breaks <- bn %>% filter(highlight == TRUE) %>% pull(name)
ggplot(bn, aes(x = name, y = n, fill = highlight)) +
geom_col() +
scale_x_discrete(breaks = breaks)+
coord_flip() +
theme_classic()
由 reprex package (v0.2.1)
创建于 2018-09-21