减少ggplot中y轴和导管之间的空间

时间:2019-10-18 06:09:04

标签: r ggplot2

我用ggplot制作了下图。我想减少y轴和第一类(a)之间的距离。我应该使用哪个功能?谢谢! :)

library(ggplot2)
library(reshape2)

data <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10), group = 1:10)
data <- melt(data, id = "group")

ggplot(data, aes(x = variable, y = value, group = group, color = as.factor(group))) + geom_point() + geom_line() + theme_minimal() + theme(axis.line = element_line(), panel.grid = element_blank())

enter image description here

1 个答案:

答案 0 :(得分:2)

假设我们有以下情节:

library(ggplot2)

df <- data.frame(x = rep(LETTERS[1:3], 3),
                 y = rnorm(9),
                 z = rep(letters[1:3], each = 3))

ggplot(df, aes(x, y, colour = z, group = z)) +
  geom_line() +
  geom_point()

enter image description here

我们可以通过在比例函数中调整expand自变量来减小端点和面板边缘之间的距离:

ggplot(df, aes(x, y, colour = z, group = z)) +
  geom_line() +
  geom_point() +
  scale_x_discrete(expand = c(0,0.1))

enter image description here

设置expand = c(0,0)会完全删除空格。第一个参数是相对数,第二个参数是绝对数;因此,在上面的示例中,我们将扩展设置为0.1 x轴单位。