英文:
How to keep the axis configuration on a plot made with ggplot with split axes using ggbreak?
问题
当使用 ggbreak
分割 x 轴时,图表显示正确。但是,如果我指定 y 轴的刻度,y 轴不会显示为没有指定时的样子。我该如何解决这个问题?
library(ggplot2)
library(ggbreak)
DF <- data.frame(x = 1:40,
y = c(rnorm(n = 10,
mean = 1000,
sd = 250),
rnorm(n = 10,
mean = 200,
sd = 10),
rnorm(n = 10,
mean = 1000,
sd = 250),
rnorm(n = 10,
mean = 200,
sd = 10)))
# 无对数轴
ggplot(data = DF,
aes(x = x,
y = y)) +
geom_point() +
scale_x_break(breaks = c(10, 20),
scales = "free")
注意,这里 y 轴被打印两次,不符合预期。
# 有对数轴
ggplot(data = DF,
aes(x = x,
y = y)) +
geom_point() +
scale_y_log10(name = "y 轴",
limits = c(0.1, 10000),
breaks = c(0.1, 1, 10, 100, 1000, 10000),
labels = c(expression(10^-1),
expression(10^0),
expression(10^1),
expression(10^2),
expression(10^3),
expression(10^4))) +
scale_x_break(breaks = c(10, 20),
scales = "free")
英文:
When I split the x-axis using ggbreak
, the plot is displayed correctly. However, if I specify the scale of the y-axis, the y-axis is not displayed as it would be without the specification. How can I solve this?
library(ggplot2)
library(ggbreak)
DF <- data.frame(x = 1:40,
y = c(rnorm(n = 10,
mean = 1000,
sd = 250),
rnorm(n = 10,
mean = 200,
sd = 10),
rnorm(n = 10,
mean = 1000,
sd = 250),
rnorm(n = 10,
mean = 200,
sd = 10)))
# Without log axis
ggplot(data = DF,
aes(x = x,
y = y)) +
geom_point() +
scale_x_break(breaks = c(10, 20),
scales = "free")
Note that here the y-axis is printed twice, unlike as desired.
# With log axis
ggplot(data = DF,
aes(x = x,
y = y)) +
geom_point() +
scale_y_log10(name = "y-axis",
limits = c(0.1, 10000),
breaks = c(0.1, 1, 10, 100, 1000, 10000),
labels = c(expression(10^-1),
expression(10^0),
expression(10^1),
expression(10^2),
expression(10^3),
expression(10^4))) +
scale_x_break(breaks = c(10, 20),
scales = "free")
答案1
得分: 1
Here is the translated code snippet:
尽管这看起来像是一个错误,但一个解决方法是明确指定一个重复的轴,然后删除 breaks
、labels
和 name
:
library(ggplot2)
library(ggbreak)
set.seed(123)
ggplot(
data = DF,
aes(
x = x,
y = y
)
) +
geom_point() +
scale_y_log10(
name = "y轴",
limits = c(0.1, 10000),
breaks = c(0.1, 1, 10, 100, 1000, 10000),
labels = c(
expression(10^-1),
expression(10^0),
expression(10^1),
expression(10^2),
expression(10^3),
expression(10^4)
),
sec.axis = dup_axis(breaks = NULL, labels = NULL, name = NULL)
) +
scale_x_break(breaks = c(10, 20), scales = "free")
英文:
While this looks like a bug to me, a workaround would be to explicitly specify a duplicated axis for which you remove the breaks
, labels
and the name
:
library(ggplot2)
library(ggbreak)
set.seed(123)
ggplot(
data = DF,
aes(
x = x,
y = y
)
) +
geom_point() +
scale_y_log10(
name = "y-axis",
limits = c(0.1, 10000),
breaks = c(0.1, 1, 10, 100, 1000, 10000),
labels = c(
expression(10^-1),
expression(10^0),
expression(10^1),
expression(10^2),
expression(10^3),
expression(10^4)
),
sec.axis = dup_axis(breaks = NULL, labels = NULL, name = NULL)
) +
scale_x_break(breaks = c(10, 20), scales = "free")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论