ggplot构面网格的错误轴标签 [英] Wrong axis labels for ggplot facet grid

查看:118
本文介绍了ggplot构面网格的错误轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回答此<我遇到一个奇怪的问题的href ="https://stackoverflow.com/questions/44786138/add-vertical-separator-and-labels-to-r-barplot/44786956#44786956">问题;

我想用ggplot2绘制条形图,并用x轴标签作为Group列的最后一个字符.为此使用substring(Group, 3, 3):

I want to make a bar plot with ggplot2 and have x axis labels as the last character of Group column. Use substring(Group, 3, 3) for this purpose:

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

但是当我在如下所示的ggplot中使用它时,它将在最后一刻打印1而不是4

But when I use it in ggplot like below, it prints 1 instead of 4 at the last tick;

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"))

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

I can get it to work by using labels =unique(substring(Group, 3, 3) but would someone explain what's going on?

数据:

Data:

  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

推荐答案

我认为@Gregor的答案是正确的方法,但我可以解释该错误.即使在group之前添加df$,您也会得到所描述的行为.您会看到为什么不让比例变化的原因:

I think that @Gregor's answer is the way to go, but I can explain the error. Even if you add df$ before group, you get the behavior you described. You can see why if you don't let the scales vary:

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"))

给出:

如您所见,此处正确给出了"4".但是,当您直接设置标签时,如果允许比例变化,则会分别(按顺序)为每个构面设置标签.本质上,您每次都将标签设置为"1、2、3、1、2、1、2、3、4",但只能使用前几个标签.

As you can see, here it gives the "4" correctly. However, when you are setting the labels directly, it is setting them (in order) for each facet separately when you allow the scales to vary. In essence, you are setting the labels to "1, 2, 3, 1, 2, 1, 2, 3, 4" each time, but only using the first few.

如果您想更贴近潮流,还需要设置休息时间,例如:

If you want to stick closer to your current, you'll also need to set the breaks, e.g.:

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"))

哪个给

这篇关于ggplot构面网格的错误轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆