在离散ggplot x轴的两侧添加不同数量的额外空间 [英] add different amount of extra space on both sides of discrete ggplot x-axis
本文介绍了在离散ggplot x轴的两侧添加不同数量的额外空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有离散x轴的图,我想调整比例尺两侧的多余空间,使其在左侧变小,在右侧变大,因此长标签将适合. scale_x_discrete(expand=c(0, 1))
在这里不是我的朋友,因为它总是同时在两侧工作. 此问题与之类似,但涉及的范围是连续的.
I have a plot with a discrete x-axis and I want to tweak the extra space on both sides of the scale, making it smaller on the left and bigger on the right, so the long labels will fit. scale_x_discrete(expand=c(0, 1))
is not my friend here, as it always works on both side simultaneously. This question is similar but addresses continuous scales.
我该如何实现?
set.seed(0)
L <- sapply(LETTERS, function(x) paste0(rep(x, 10), collapse=""))
x <- data.frame(label=L[1:24],
g2 = c("a", "b"),
y = rnorm(24))
x$g2 <- as.factor(x$g2)
x$xpos2 <- as.numeric(x$g2) + .25
# two groups
ggplot(x, aes(x=g2, y=y)) +
geom_boxplot(width=.4) +
geom_point(col="blue") +
geom_text(aes(x=xpos2, label=label, hjust=0))
推荐答案
这可能是您想要的:
library(ggplot2)
set.seed(0)
L <- sapply(LETTERS, function(x) paste0(rep(x, 10), collapse=""))
x <- data.frame(label=L[1:24],
g2 = c("a", "b"),
y = rnorm(24))
x$g2 <- factor(x$g2, levels=c("a", "b", "c"))
x$xpos2 <- as.numeric(x$g2) + .25
# two groups
ggplot(x, aes(x=g2, y=y)) +
geom_boxplot(width=.4) +
geom_point(col="blue") +
geom_text(aes(x=xpos2, label=label, hjust=0)) +
scale_x_discrete(expand=c(0.1,0),
breaks=c("a", "b"),
labels=c("a", "b"),
limits=c("a", "b", "c"), drop=FALSE)
这篇关于在离散ggplot x轴的两侧添加不同数量的额外空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文