使用因子排序小平面图上的轴,但包括标签

时间:2017-07-11 23:37:11

标签: r ggplot2

我有这个方面的情节和x轴我想让x轴的排序为"在&favé"" ne""" mi&# 34;," fa","外面的"但它很棘手,因为我还想在括号中包含观察计数。

我使x成为一个因素,并按正确的顺序放置数据框:

 dat$x = factor(dat$x, levels = c("outside fav","ne","mi", "fa","outside un") , ordered = TRUE)
      levels(dat$x)
      dat = dat %>% arrange(country, x) # order by country and x 

然后在下面的ggplot中我这样做:

factor( x_label, levels = unique(dat$x_label), ordered = TRUE)

尝试保持标签的顺序,但当然唯一包括括号中的计数,因此绘图的输出不正确。您可以通过查看" J"绘制x轴上的顺序为fa(5),外部为fav(5)等的情节......

您知道如何以正确的顺序获取x轴标签上的顺序吗?包括括号中的计数?

谢谢。

  dat =  data.frame(country = c(
                                    rep("A",4),
                                    rep("H", 5),
                                    rep("I",4),
                                    rep("J", 5),
                                    rep("S", 2),
                                    rep("T",4)    ),
                       x = c(
                                     "outside fav","mi", "fa","outside un",
                                     "outside fav","ne","mi", "fa","outside un",
                                     "outside fav","mi", "fa","outside un",
                                     "outside fav","ne","mi", "fa","outside un",
                                      "fa","outside fa",
                                      "ne","mi", "fa","outside un"  ), 
                 x_label = c(
                                     "outside fav (1)","mi(3)", "fa (5)","outside un (3)",
                                     "outside fav (6)","ne (8)","mi (57)", "fa (22)","outside un (6)",
                                     "outside fav (27)","mi (2)", "fa (4)","outside un (41)",
                                     "outside fav (5)","ne (5)","mi (8)", "fa (5)","outside un (8)",
                                      "fa (3)","outside fa (2)",
                                      "ne (2)","mi (2)", "fa (2)","outside un (1)"  ),
                 y = rnorm(24)
                 )
      dat$x = factor(dat$x, levels = c("outside fav","ne","mi", "fa","outside un") , ordered = TRUE)
      levels(dat$x)
      dat = dat %>% arrange(country, x) # order by country and x 
      ggplot(dat, aes(x = factor( x_label, levels = unique(dat$x_label), ordered = TRUE),
                      y = y   ))+ 
        geom_point()  +
        facet_wrap(~country, scales="free_x")+theme(axis.text.x = element_text(angle = 90))

2 个答案:

答案 0 :(得分:1)

我认为您可以通过唯一的x值(id)和标签更改来获取图表。

dat$id <- as.factor(1:24)
ggplot(dat, aes(x = id,
                y = y)) + 
  geom_point()  +
  facet_wrap(~ country, scales = "free_x") +
  scale_x_discrete(breaks = dat$id, labels = dat$x_label) + 
  theme(axis.text.x = element_text(angle = 90))

enter image description here

答案 1 :(得分:0)

问题出现是因为你的x_label因子没有以你希望它出现在图中的方式排序(参见levels(dat $ x_label))。如果您想避免手动排序因子级别,可以使用以下方法:

library(ggplot2)
library(gridExtra)  

p = list()
countries <- unique(dat$country)
for(i in 1:length(countries)) {
  dat2 = dat[dat$country == countries[i],]
  p[[i]] <- ggplot(dat2, aes(x = x, y = y))+ 
    geom_point()  + scale_x_discrete(breaks = dat2$x, labels = dat2$x_label) +
    ggtitle(countries[i])+ labs(x ="") +
    theme(axis.text.x = element_text(angle = 90))
}
x11(); grid.arrange(grobs = p, nrow = 2)

enter image description here