在 ggplot2 中分面数据时如何在刻度上设置不同的中断和标签?
How to set different breaks and labels on the scales when facetting data in ggplot2?
考虑以下代码:
library(ggplot2)
data = data.frame(x = c(1, 2,
3, 4,
5, 6),
label = c("foo", "bar",
"bar", "baz",
"baz", "boo"),
type = c(1, 1,
2, 2,
3, 3))
ggplot(data, aes(x = x, y = c(1))) +
labs(x = "", y = "") +
theme_bw() +
facet_wrap(~ type, ncol = 1, scales = "free_x") +
scale_x_discrete(aes(breaks = x, labels=label), limits = 1:6) +
geom_point()
它生成图像:
问题是我的scale_x_discrete()
被忽略了。我希望每个方面的 x 比例显示 data$label
中的标签,但仅在有数据的地方显示。换句话说,我想要这样的东西,但在一张图表上:
我该怎么做?
正在将参数更改为 scales = "free"
。我修改了示例,添加“y = type”而不是常量 c(1) 以获得不同的比例。
ggplot(data, aes(x = x, y = type)) +
labs(x = "", y = "") +
theme_bw() +
facet_wrap(~ type, ncol = 1, scales = "free") +
scale_x_discrete(aes(breaks = x), labels = data$label, limits = 1:6) +
geom_point()
使用scales = "free_y"
ggplot(data, aes(x = x, y = type)) +
labs(x = "", y = "") +
theme_bw() +
facet_wrap(~ type, ncol = 1, scales = "free_y") +
scale_x_discrete(aes(breaks = x), labels = data$label, limits = 1:6) +
geom_point()
ggplot(data, aes(x = label, y = type)) +
labs(x = "", y = "") +
theme_bw() +
facet_wrap(~ type, ncol = 1, scales = "free") +
geom_point()
您可能需要单独构建绘图,然后使用 grid.arrange
:
组合它们
library(ggplot2)
data = data.frame(x = c(1, 2,
3, 4,
5, 6),
label = c("foo", "bar",
"bar", "baz",
"baz", "boo"),
type = c(1, 1,
2, 2,
3, 3))
library(gridExtra) # Using V 2.0.0
p = list()
for(i in 1:3) {
df = subset(data, type == i)
p[[i]] = ggplot(df, aes(x = x, y = c(1)) ) +
labs(x = "", y = "") +
theme_bw() +
expand_limits(x = c(1, 6)) +
facet_wrap(~ type, ncol = 1) +
scale_x_continuous(breaks = df$x, labels = df$label) +
geom_point()
}
grid.arrange(grobs = p, ncol = 1)
考虑以下代码:
library(ggplot2)
data = data.frame(x = c(1, 2,
3, 4,
5, 6),
label = c("foo", "bar",
"bar", "baz",
"baz", "boo"),
type = c(1, 1,
2, 2,
3, 3))
ggplot(data, aes(x = x, y = c(1))) +
labs(x = "", y = "") +
theme_bw() +
facet_wrap(~ type, ncol = 1, scales = "free_x") +
scale_x_discrete(aes(breaks = x, labels=label), limits = 1:6) +
geom_point()
它生成图像:
问题是我的scale_x_discrete()
被忽略了。我希望每个方面的 x 比例显示 data$label
中的标签,但仅在有数据的地方显示。换句话说,我想要这样的东西,但在一张图表上:
我该怎么做?
正在将参数更改为 scales = "free"
。我修改了示例,添加“y = type”而不是常量 c(1) 以获得不同的比例。
ggplot(data, aes(x = x, y = type)) +
labs(x = "", y = "") +
theme_bw() +
facet_wrap(~ type, ncol = 1, scales = "free") +
scale_x_discrete(aes(breaks = x), labels = data$label, limits = 1:6) +
geom_point()
使用scales = "free_y"
ggplot(data, aes(x = x, y = type)) +
labs(x = "", y = "") +
theme_bw() +
facet_wrap(~ type, ncol = 1, scales = "free_y") +
scale_x_discrete(aes(breaks = x), labels = data$label, limits = 1:6) +
geom_point()
ggplot(data, aes(x = label, y = type)) +
labs(x = "", y = "") +
theme_bw() +
facet_wrap(~ type, ncol = 1, scales = "free") +
geom_point()
您可能需要单独构建绘图,然后使用 grid.arrange
:
library(ggplot2)
data = data.frame(x = c(1, 2,
3, 4,
5, 6),
label = c("foo", "bar",
"bar", "baz",
"baz", "boo"),
type = c(1, 1,
2, 2,
3, 3))
library(gridExtra) # Using V 2.0.0
p = list()
for(i in 1:3) {
df = subset(data, type == i)
p[[i]] = ggplot(df, aes(x = x, y = c(1)) ) +
labs(x = "", y = "") +
theme_bw() +
expand_limits(x = c(1, 6)) +
facet_wrap(~ type, ncol = 1) +
scale_x_continuous(breaks = df$x, labels = df$label) +
geom_point()
}
grid.arrange(grobs = p, ncol = 1)