ggplot facet网格的轴标签错误

时间:2017-06-27 18:22:01

标签: r plot ggplot2 facet axis-labels

answering question我遇到了一个奇怪的问题;

我想制作一个带有ggplot2的条形图,并将x轴标签作为Group列的最后一个字符。为此目的使用substring(Group, 3, 3)

substring(df$Group, 3, 3)
# [1] "1" "2" "3" "1" "2" "1" "2" "3" "4"

但是当我在ggplot中使用它时,它会在最后一刻打印1而不是4;

ggplot(data=df, aes(x=Group, y=Value)) +
  geom_bar(stat="identity") + 
  scale_x_discrete(labels = substring(Group, 3, 3), expand=c(0.1,0.1)) +
  facet_grid(~ substring(Group, 1, 1), space="free_x", scales="free_x", switch="x") + 
  theme_bw() +
  theme(strip.placement = "outside",
        strip.background = element_rect(fill=NA,colour="grey50"),
        panel.spacing=unit(0,"cm"))

barplot

我可以使用labels =unique(substring(Group, 3, 3)让它工作,但有人会解释发生了什么吗?

数据:

  df <- structure(list(Group = structure(1:9, .Label = c("1_1", "1_2",  
  "1_3", "2_1", "2_2", "3_1", "3_2", "3_3", "3_4"), class = "factor"),  
      Value = c(-1.23, 2.34, 0.56, 1.87, -2.4, 5.54, -0.98, -2.31,  
      6)), .Names = c("Group", "Value"), row.names = c(NA, -9L), class = "data.frame")

# > df

#   Group Value 
# 1   1_1 -1.23 
# 2   1_2  2.34 
# 3   1_3  0.56 
# 4   2_1  1.87 
# 5   2_2 -2.40 
# 6   3_1  5.54 
# 7   3_2 -0.98 
# 8   3_3 -2.31 
# 9   3_4  6.00

2 个答案:

答案 0 :(得分:3)

让我们简化并开始新的R会议:

df <- structure(list(Group = structure(1:9, .Label = c("1_1", "1_2",  
                                                       "1_3", "2_1", "2_2", "3_1", "3_2", "3_3", "3_4"), class = "factor"),  
                     Value = c(-1.23, 2.34, 0.56, 1.87, -2.4, 5.54, -0.98, -2.31,  
                              6)), .Names = c("Group", "Value"), row.names = c(NA, -9L), class = "data.frame")

library(ggplot2)
ggplot(data=df, aes(x=Group, y=Value)) +
    geom_bar(stat="identity") + 
    scale_x_discrete(labels = substring(Group, 3, 3), expand=c(0.1,0.1))
# Error in substring(Group, 3, 3) : object 'Group' not found

scale_x_discrete个参数无权访问数据内环境。 (我认为只有两个地方可以安全地引用ggplot2中的数据列:内部aes()以及作为分面调用中formula的一部分。)您的代码必须找到{来自全局环境的{1}}对象,可能是整个列。这就是为什么你的问题没有出错,但是当我使用新的R会话时我就是这样。

通常最简单的方法是让Group做你想做的就是修复你的数据,使它符合你的要求:

ggplot2

此时,您可以将您的方面和主题添加回来,一切都会有效。

答案 1 :(得分:2)

我认为@Gregor的答案是要走的路,但我可以解释错误。即使您在df$之前添加group,也会获得您描述的行为。你可以看到为什么你不让比例变化:

ggplot(data=df, aes(x=Group, y=Value)) +
  geom_bar(stat="identity") + 
  scale_x_discrete(labels = substring(df$Group, 3, 3), expand=c(0.1,0.1)) +
  facet_grid(~ substring(Group, 1, 1), switch="x") + 
  theme_bw() +
  theme(strip.placement = "outside",
        strip.background = element_rect(fill=NA,colour="grey50"),
        panel.spacing=unit(0,"cm"))

给出: enter image description here

正如你所看到的,这里给出了&#34; 4&#34;正确。但是,当您直接设置标签时,当您允许刻度变化时,它会分别为每个刻面设置它们(按顺序)。实质上,您将标签设置为&#34; 1,2,3,1,2,1,2,3,4和34;每一次,但只使用前几个。

如果你想更接近当前,你还需要设置休息时间,例如:

ggplot(data=df, aes(x=Group, y=Value)) +
  geom_bar(stat="identity") + 
  scale_x_discrete(breaks = df$Group, labels = substring(df$Group, 3, 3), expand=c(0.1,0.1)) +
  facet_grid(~ substring(Group, 1, 1), space="free_x", scales="free_x", switch="x") + 
  theme_bw() +
  theme(strip.placement = "outside",
        strip.background = element_rect(fill=NA,colour="grey50"),
        panel.spacing=unit(0,"cm"))

哪个给出了

enter image description here