如何显示非常高的ggplot?
How to display really tall ggplot?
例如,在尝试显示包含一百行的多面图时:
library(ggplot2)
set.seed(111)
tmp_d <- data.frame(group = rep(1:100, each = 5),
x = rep(1:5, times = 100),
y = runif(1:500))
## looks like
# group x y
# 1 1 1 0.5929813
# 2 1 2 0.7264811
# 3 1 3 0.3704220
# 4 1 4 0.5149238
# 5 1 5 0.3776632
# 6 2 1 0.4183373
# ...
ggplot(tmp_d, aes(x,y)) +
geom_point() +
facet_wrap(~ group, ncol = 1)
我在 Rstudio 中绘制 window 时一团糟,无法放大或滚动:
不方便的解决方法涉及 ggsave
或 knitr
:
使用ggsave
:
ggsave('tmp_20170104_long_ggplot.png', p, height = 100, limitsize = F)
使用knitr
:
将绘图嵌入 knitr
代码块中,其中 fig.height
:
具有较大的任意值
---
title: "Test plot of tall ggplot2"
output:
html_document
---
```{r, fig.height = 100}
library(ggplot2)
set.seed(111)
tmp_d <- data.frame(group = rep(1:100, each = 5),
x = rep(1:5, times = 100),
y = runif(1:500))
ggplot(tmp_d, aes(x,y)) +
geom_point() +
facet_wrap(~ group, ncol = 1)
```
两个问题:
- 如何轻松显示此类图?
- 如何确定每个解决方法的高度值?
1- 尝试 dev.new(width=3, height=10, noRStudioGD = TRUE)
。如果您的绘图对于屏幕而言太高,您可以将其绘制在 pdf 文件上并使用 pdf 查看器滚动,
library(ggplot2)
set.seed(111)
tmp_d <- data.frame(group = rep(1:100, each = 5),
x = rep(1:5, times = 100),
y = runif(1:500))
p <- ggplot(tmp_d, aes(x,y)) +
geom_point() +
facet_wrap(~ group, ncol = 1)
nc <- length(unique(ggplot_build(p)[["data"]][[1]][["PANEL"]]))
ggsave("tall.pdf", p, width=7, height = nc * 7 + 2, limitsize = FALSE)
2- 如果你有 N 行面板并且每个面板应该高 H 英寸,那么像 N x H 加上一些轴等的空间应该很好。
例如,在尝试显示包含一百行的多面图时:
library(ggplot2)
set.seed(111)
tmp_d <- data.frame(group = rep(1:100, each = 5),
x = rep(1:5, times = 100),
y = runif(1:500))
## looks like
# group x y
# 1 1 1 0.5929813
# 2 1 2 0.7264811
# 3 1 3 0.3704220
# 4 1 4 0.5149238
# 5 1 5 0.3776632
# 6 2 1 0.4183373
# ...
ggplot(tmp_d, aes(x,y)) +
geom_point() +
facet_wrap(~ group, ncol = 1)
我在 Rstudio 中绘制 window 时一团糟,无法放大或滚动:
不方便的解决方法涉及 ggsave
或 knitr
:
使用ggsave
:
ggsave('tmp_20170104_long_ggplot.png', p, height = 100, limitsize = F)
使用knitr
:
将绘图嵌入 knitr
代码块中,其中 fig.height
:
---
title: "Test plot of tall ggplot2"
output:
html_document
---
```{r, fig.height = 100}
library(ggplot2)
set.seed(111)
tmp_d <- data.frame(group = rep(1:100, each = 5),
x = rep(1:5, times = 100),
y = runif(1:500))
ggplot(tmp_d, aes(x,y)) +
geom_point() +
facet_wrap(~ group, ncol = 1)
```
两个问题:
- 如何轻松显示此类图?
- 如何确定每个解决方法的高度值?
1- 尝试 dev.new(width=3, height=10, noRStudioGD = TRUE)
。如果您的绘图对于屏幕而言太高,您可以将其绘制在 pdf 文件上并使用 pdf 查看器滚动,
library(ggplot2)
set.seed(111)
tmp_d <- data.frame(group = rep(1:100, each = 5),
x = rep(1:5, times = 100),
y = runif(1:500))
p <- ggplot(tmp_d, aes(x,y)) +
geom_point() +
facet_wrap(~ group, ncol = 1)
nc <- length(unique(ggplot_build(p)[["data"]][[1]][["PANEL"]]))
ggsave("tall.pdf", p, width=7, height = nc * 7 + 2, limitsize = FALSE)
2- 如果你有 N 行面板并且每个面板应该高 H 英寸,那么像 N x H 加上一些轴等的空间应该很好。