如何在使用ggplot2分面数据时设置不同的断点和标签?

7

Consider the following code:

library(ggplot2)    
data = data.frame(x = c(1, 2,
                        3, 4,
                        5, 6),
                  label = c("foo", "bar",
                            "bar", "baz",
                            "baz", "boo"),
                  type = c(1, 1,
                           2, 2,
                           3, 3))
ggplot(data, aes(x = x, y = c(1))) +
    labs(x = "", y = "") +
    theme_bw() +
    facet_wrap(~ type, ncol = 1, scales = "free_x") +
    scale_x_discrete(aes(breaks = x, labels=label), limits = 1:6) +
    geom_point()

它生成了该图像: image plot 问题是我的scale_x_discrete()被忽略了。我想让每个facet的x轴缩放显示data$label标签,但仅在有数据时才显示。换句话说,我想要像这样的东西,但在一个单独的图表上: facet facet facet 我该怎么做?

1
我认为你的代码中有一个错别字,在生成那些图表时,需要使用ggplot(data, aes(x = x, y = c(1)))而不是ggplot(data, aes(x = x, y = c(0))) - mpalanco
你说得对。我已经修复了代码,谢谢。 - Vitor Baptista
2个回答

3

您可能需要分别构建图形,然后使用grid.arrange将它们组合起来:

library(ggplot2)    
data = data.frame(x = c(1, 2,
                        3, 4,
                        5, 6),
                  label = c("foo", "bar",
                            "bar", "baz",
                            "baz", "boo"),
                  type = c(1, 1,
                           2, 2,
                           3, 3))

library(gridExtra)  # Using V 2.0.0

p = list()

for(i in 1:3) {

df = subset(data, type == i)

p[[i]] = ggplot(df, aes(x = x, y = c(1)) ) +
    labs(x = "", y = "") +
    theme_bw() +
    expand_limits(x = c(1, 6)) + 
    facet_wrap(~ type, ncol = 1) +
    scale_x_continuous(breaks = df$x, labels = df$label) +
    geom_point()
   }

grid.arrange(grobs = p, ncol = 1)

enter image description here


嗨 Sandy,你能保持所有 x 轴的限制相同吗?我尝试修改你的代码,但无法做到。 - Vitor Baptista
我已经修改了情节和代码。这次我理解了吗? - Sandy Muspratt
是的,我最终也是这样做的。似乎没有办法用一个图表来实现我们想要的功能。我会接受你的答案。感谢你的帮助! - Vitor Baptista

2
修改参数为scales = "free"。我修改了示例,使用'y = type'代替常量c(1),以获得不同的比例尺。
ggplot(data, aes(x = x, y = type)) +
  labs(x = "", y = "") +
  theme_bw() +
  facet_wrap(~ type, ncol = 1, scales = "free") +
  scale_x_discrete(aes(breaks = x), labels = data$label, limits = 1:6) +
  geom_point()

enter image description here

Using scales = "free_y"

ggplot(data, aes(x = x, y = type)) +
  labs(x = "", y = "") +
  theme_bw() +
  facet_wrap(~ type, ncol = 1, scales = "free_y") +
  scale_x_discrete(aes(breaks = x), labels = data$label, limits = 1:6) +
  geom_point()

enter image description here

ggplot(data, aes(x = label, y = type)) +
  labs(x = "", y = "") +
  theme_bw() +
  facet_wrap(~ type, ncol = 1, scales = "free") +
  geom_point()

enter image description here


@nongkrong 不确定这是否更接近OP所想要的问题。 - mpalanco
有一个PANEL变量,你也许可以使用它。 - Rorschach
@mpalanco 谢谢你的回答,但这对我没有用。我想要的是你的第一个图表,但每个分面的轴线中断点不同。第一个应该只有foo和bar,第二个是bar和baz,第三个是baz和boo。y也应该是恒定的。我试图展示的是随时间变化的数据。每个“type”代表一个时间:t1、t2和t3,数据的位置随时间而变化。你可以看到,“bar”从t1的2变为t2的3,“baz”从t2的3变为t3的4。这就是为什么每个分面的中断点必须不同的原因。 - Vitor Baptista
@VítorBaptista 在最后一个图表中,您可以通过查看y轴来看到时间的变化。例如,bar在t1时从1变为t2时的2。 - mpalanco
@mpalanco 我明白了...不过,我认为在x轴上保持时间会更清晰易懂。不确定是否可以在ggplot的单个图表中实现。 - Vitor Baptista

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接