如何设置分面时显示的y轴值个数
How to set the number of y-axis values displayed when faceting
我有一个多面条形图。
dat <- data.frame(ID = c("A", "A", "B", "B", "C", "C"),
A = c("Type 1", "Type 2", "Type 1", "Type 2", "Type 1", "Type 2"),
B = c(1, 2, 53, 87, 200, 250))
ggplot(data = dat, aes(x = A, y = B)) +
geom_bar(stat = "identity") +
facet_wrap(~ID, scales= "free_y")
如何编码使每个图表只显示 3 个 y 轴值?
我试过了
+scale_y_continuous(breaks=3)
要更好地控制中断,您可以编写自己的中断函数。以下代码恰好为您提供了 3 次休息。但是,这种非常基本的方法不一定会导致 "pretty" 中断:
library(ggplot2)
my_breaks <- function(x) {
seq(0, round(max(x), 0), length.out = 3)
}
my_limits <- function(x) {
c(x[1], ceiling(x[2]))
}
# Dataset 2
dat <- data.frame(
ID = c("A", "A", "A","B", "B", "B", "C", "C", "C"),
A = c("Type 1", "Type 2", "Type 3", "Type 1", "Type 2", "Type 3","Type 1", "Type 2", "Type 3"),
B = c(1.7388, 4.2059, .7751, .9489, 2.23405, .666589, 0.024459, 1.76190, 0.066678))
ggplot(data = dat, aes(x = A, y = B)) +
geom_bar(stat = "identity") +
facet_wrap(~ID, scales= "free_y") +
scale_y_continuous(breaks = my_breaks, limits = my_limits)
# Dataset 1
dat <- data.frame(ID = c("A", "A", "B", "B", "C", "C"),
A = c("Type 1", "Type 2", "Type 1", "Type 2", "Type 1", "Type 2"),
B = c(1, 2, 53, 87, 200, 250))
ggplot(data = dat, aes(x = A, y = B)) +
geom_bar(stat = "identity") +
facet_wrap(~ID, scales= "free_y") +
scale_y_continuous(breaks = my_breaks, limits = my_limits)
由 reprex package (v0.3.0)
于 2020 年 4 月 10 日创建
我有一个多面条形图。
dat <- data.frame(ID = c("A", "A", "B", "B", "C", "C"),
A = c("Type 1", "Type 2", "Type 1", "Type 2", "Type 1", "Type 2"),
B = c(1, 2, 53, 87, 200, 250))
ggplot(data = dat, aes(x = A, y = B)) +
geom_bar(stat = "identity") +
facet_wrap(~ID, scales= "free_y")
如何编码使每个图表只显示 3 个 y 轴值?
我试过了
+scale_y_continuous(breaks=3)
要更好地控制中断,您可以编写自己的中断函数。以下代码恰好为您提供了 3 次休息。但是,这种非常基本的方法不一定会导致 "pretty" 中断:
library(ggplot2)
my_breaks <- function(x) {
seq(0, round(max(x), 0), length.out = 3)
}
my_limits <- function(x) {
c(x[1], ceiling(x[2]))
}
# Dataset 2
dat <- data.frame(
ID = c("A", "A", "A","B", "B", "B", "C", "C", "C"),
A = c("Type 1", "Type 2", "Type 3", "Type 1", "Type 2", "Type 3","Type 1", "Type 2", "Type 3"),
B = c(1.7388, 4.2059, .7751, .9489, 2.23405, .666589, 0.024459, 1.76190, 0.066678))
ggplot(data = dat, aes(x = A, y = B)) +
geom_bar(stat = "identity") +
facet_wrap(~ID, scales= "free_y") +
scale_y_continuous(breaks = my_breaks, limits = my_limits)
# Dataset 1
dat <- data.frame(ID = c("A", "A", "B", "B", "C", "C"),
A = c("Type 1", "Type 2", "Type 1", "Type 2", "Type 1", "Type 2"),
B = c(1, 2, 53, 87, 200, 250))
ggplot(data = dat, aes(x = A, y = B)) +
geom_bar(stat = "identity") +
facet_wrap(~ID, scales= "free_y") +
scale_y_continuous(breaks = my_breaks, limits = my_limits)
由 reprex package (v0.3.0)
于 2020 年 4 月 10 日创建