ggplot facet_wrap 等距轴
ggplot facet_wrap with equally spaced axes
假设我有以下虚拟数据框:
df <- data.frame(let = LETTERS[1:13], value = sample(13),
group = rep(c("foo", "bar"), times = c(5,8)))
df
let value group
1 A 2 foo
2 B 1 foo
3 C 12 foo
4 D 8 foo
5 E 4 foo
6 F 13 bar
7 G 11 bar
8 H 3 bar
9 I 7 bar
10 J 5 bar
11 K 10 bar
12 L 9 bar
13 M 6 bar
将 ggplot 与 facet_wrap 结合使用可以让我为每个组制作一个面板...
library(ggplot2)
ggplot(df, aes(x= let, y = value)) +
geom_point() +
coord_flip() +
facet_wrap(~group, scales = "free")
..但垂直轴不是等距的,即左图比右图包含更多的垂直刻度。我想用(未标记的)刻度(没有绘制值)填充右侧的垂直轴。在这种情况下会添加 3 个空刻度,但它应该可以扩展到任何 df 大小。
完成此任务的最佳方法是什么?我应该更改数据框,还是有办法使用 ggplot 来做到这一点?
使用free_x
代替free
,像这样:
ggplot(df, aes(x= let, y = value)) +
geom_point() +
coord_flip() +
facet_wrap(~group, scales = "free_x")+
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank())
需要 magrittr
的笨拙解决方案(对于复合分配管道 %<>%
):
df %<>%
rbind(data.frame(let = c(" ", " ", " "),
value = NA,
group = "foo"))
我只是为 foo
添加了另外三个条目,它们是不同长度的空白字符串(即,只是空格)。不过,一定有更优雅的解决方案。
我不确定你为什么要像你那样在图表上排列分类变量,而不是为了美观(它看起来确实更好)。无论如何,一个似乎可以处理一般情况的简单解决方法是注意 ggplot
使用数字比例来绘制分类变量。然后,图表的解决方法是为每个 x 值在 y 值处绘制一个透明点,该点等于分类变量的数量。为所有 x 值绘制点,作为对每个组的 x 值范围不重叠的情况的简单解决方案。我已将另一个组添加到您的数据框中,以使该示例更加通用。
library(ggplot2)
set.seed(123)
df <- data.frame(let = LETTERS[1:19], value = c(sample(13),20+sample(6)),
group = rep(c("foo", "bar", "bar2"), times = c(5,8,6)))
num_rows <- xtabs(~ group, df)
max_rows <- max(num_rows)
sp <- ggplot(df, aes(y= let, x = value)) +
geom_point() +
geom_point(aes(y = max_rows +.5), alpha=0 ) +
facet_wrap(~group, scales = "free", nrow=1 )
plot(sp)
这给出了以下图表:
假设我有以下虚拟数据框:
df <- data.frame(let = LETTERS[1:13], value = sample(13),
group = rep(c("foo", "bar"), times = c(5,8)))
df
let value group
1 A 2 foo
2 B 1 foo
3 C 12 foo
4 D 8 foo
5 E 4 foo
6 F 13 bar
7 G 11 bar
8 H 3 bar
9 I 7 bar
10 J 5 bar
11 K 10 bar
12 L 9 bar
13 M 6 bar
将 ggplot 与 facet_wrap 结合使用可以让我为每个组制作一个面板...
library(ggplot2)
ggplot(df, aes(x= let, y = value)) +
geom_point() +
coord_flip() +
facet_wrap(~group, scales = "free")
..但垂直轴不是等距的,即左图比右图包含更多的垂直刻度。我想用(未标记的)刻度(没有绘制值)填充右侧的垂直轴。在这种情况下会添加 3 个空刻度,但它应该可以扩展到任何 df 大小。
完成此任务的最佳方法是什么?我应该更改数据框,还是有办法使用 ggplot 来做到这一点?
使用free_x
代替free
,像这样:
ggplot(df, aes(x= let, y = value)) +
geom_point() +
coord_flip() +
facet_wrap(~group, scales = "free_x")+
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank())
需要 magrittr
的笨拙解决方案(对于复合分配管道 %<>%
):
df %<>%
rbind(data.frame(let = c(" ", " ", " "),
value = NA,
group = "foo"))
我只是为 foo
添加了另外三个条目,它们是不同长度的空白字符串(即,只是空格)。不过,一定有更优雅的解决方案。
我不确定你为什么要像你那样在图表上排列分类变量,而不是为了美观(它看起来确实更好)。无论如何,一个似乎可以处理一般情况的简单解决方法是注意 ggplot
使用数字比例来绘制分类变量。然后,图表的解决方法是为每个 x 值在 y 值处绘制一个透明点,该点等于分类变量的数量。为所有 x 值绘制点,作为对每个组的 x 值范围不重叠的情况的简单解决方案。我已将另一个组添加到您的数据框中,以使该示例更加通用。
library(ggplot2)
set.seed(123)
df <- data.frame(let = LETTERS[1:19], value = c(sample(13),20+sample(6)),
group = rep(c("foo", "bar", "bar2"), times = c(5,8,6)))
num_rows <- xtabs(~ group, df)
max_rows <- max(num_rows)
sp <- ggplot(df, aes(y= let, x = value)) +
geom_point() +
geom_point(aes(y = max_rows +.5), alpha=0 ) +
facet_wrap(~group, scales = "free", nrow=1 )
plot(sp)
这给出了以下图表: