Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在x轴两侧添加不同数量的额外空间_R_Ggplot2 - Fatal编程技术网

R 在x轴两侧添加不同数量的额外空间

R 在x轴两侧添加不同数量的额外空间,r,ggplot2,R,Ggplot2,我有一个带有离散x轴的绘图,我想调整比例两侧的额外空间,使其在左侧变小,在右侧变大,这样长标签就会合适scale\u x\u discrete(expand=c(0,1))在这里不是我的朋友,因为它总是同时在两侧工作。类似,但适用于连续刻度 我如何才能做到这一点? set.seed(0) L <- sapply(LETTERS, function(x) paste0(rep(x, 10), collapse="")) x <- data.frame(label=L[1:24],

我有一个带有离散x轴的绘图,我想调整比例两侧的额外空间,使其在左侧变小,在右侧变大,这样长标签就会合适
scale\u x\u discrete(expand=c(0,1))
在这里不是我的朋友,因为它总是同时在两侧工作。类似,但适用于连续刻度

我如何才能做到这一点?

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)) 
set.seed(0)

L这可能就是你想要的:

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)
库(ggplot2)
种子集(0)
L