强制因子变量的x轴限制与边框齐平[重复]

时间:2023-01-06 19:31:06

This question already has an answer here:

这个问题在这里已有答案:

library(dplyr)
library(tidyr)
library(ggplot2)
dummy.data <- data.frame(
  X1 = c(1,2,-1,0),
  X2 = c(2,3,-2,-1),
  Month = c("January", "June", "January", "June"),
  Colour = c("A", "B", "B", "A")
  ) %>% 
  gather(key, value, X1:X2)

ggplot(dummy.data) + 
  geom_line(aes(x = Month, y = value, colour = Colour, group = Colour)) +
  facet_grid(~variable)

强制因子变量的x轴限制与边框齐平[重复]

I would like to have January at the left edge of each panel; June at the right edge. There should be no grey band between the endpoints of the coloured lines and the edges of the panel.

我希望1月份在每个小组的左边缘;六月在右边缘。在彩色线的端点和面板的边缘之间应该没有灰色带。

1 个解决方案

#1


1  

You can use the expand variable in scale_x_discrete. Note that you probably also need to adjust the labels, because otherwise they overlap. You can use hjust in theme to do this:

您可以在scale_x_discrete中使用expand变量。请注意,您可能还需要调整标签,否则它们会重叠。您可以在主题中使用hjust来执行此操作:

ggplot(dummy.data) + 
  geom_line(aes(x = Month, y = value, colour = Colour, group = Colour)) +
  facet_grid(~variable) + 
  scale_x_discrete(expand=c(0,0)) +
  theme(axis.text.x = element_text(hjust=c(0, 1)))

#1


1  

You can use the expand variable in scale_x_discrete. Note that you probably also need to adjust the labels, because otherwise they overlap. You can use hjust in theme to do this:

您可以在scale_x_discrete中使用expand变量。请注意,您可能还需要调整标签,否则它们会重叠。您可以在主题中使用hjust来执行此操作:

ggplot(dummy.data) + 
  geom_line(aes(x = Month, y = value, colour = Colour, group = Colour)) +
  facet_grid(~variable) + 
  scale_x_discrete(expand=c(0,0)) +
  theme(axis.text.x = element_text(hjust=c(0, 1)))