ggplot:在geom_bar的每个方面添加不同的行

时间:2018-08-10 08:50:57

标签: r ggplot2 geom-bar facet-wrap geom-hline

这个问题是我之前的问题: How to ordering bars within all facets?

我想在geom_bar的每个方面添加不同的行。我使用了geom_hline函数,但是所有行都添加到了所有方面!

我的代码:

i <- data.frame(
nbr =c(15.18 ,11.53 ,13.37 ,9.2, 10.9, 12.23 ,9.53, 9.81, 7.86, 12.79,
 22.03 ,17.64 ,18.1, 16.78 ,17.53 ,16.97 ,17.76 ,18.35 ,12.82 ,20.91,
 22.09 ,19.18 ,17.54 ,18.45 ,19.83 ,16.99 ,19.69 ,19.45 ,13.07 ,21.41,
 12.13 ,9.76, 10.79 ,10.74 ,12.43 ,9.65, 12.18 ,11.63 ,6.74, 12.31,
 17.5, 14.75 ,15.2, 13.89 ,15.24 ,17.43 ,15.22 ,14.04,9.49, 15.86,
 8.09, 5.86, 6.68, 7.34, 8.01, 6.35, 8.4, 7.4, 3.88, 6.92 ),
x2=rep(c("a",  "b",   "c",  "d", "e", "f", "g", "h", "i", "j"),6),
s = c(rep(c(rep(c("3"),10),
                    rep(c("4"),10),
                    rep(c("5"),10),
                    rep(c("6"),10),
                    rep(c("7"),10),
                    rep(c("8"),10)),1)))
ii <- i[order(i$s, i$nbr ), ]
sn <- factor(x = 1:60, labels = ii$x2)
ii$sn <- sn
scale_x_reordered <- function(..., sep = "___") {
  reg <- paste0(sep, ".+$")
  ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}

reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
  new_x <- paste(x, within, sep = sep)
  stats::reorder(new_x, by, FUN = fun)
}

dummy2 <- data.frame(X = levels(i$s)[-1], Z = c( 4,16,16,8,4))
dummy2$X <- factor(dummy2$X)

ggplot(ii, aes(reorder_within(sn, nbr, s), nbr)) +
     geom_bar(stat = 'identity') +
     scale_x_reordered() +
     facet_wrap(.~ s, ncol=2,scales = "free_x") + 
     theme(axis.text.x=element_text(angle=90,hjust=1,vjust=.5,colour='gray50'))+
     geom_hline( aes(yintercept = Z),data = dummy2)

enter image description here

2 个答案:

答案 0 :(得分:2)

两个数据集都需要共享同一列。

dummy2 <- data.frame(s = levels(i$s)[-1], Z = c( 4,16,16,8,4))
dummy2$s <- factor(dummy2$s)

ggplot(ii, aes(reorder_within(sn, nbr, s), nbr)) +
     geom_bar(stat = 'identity') +
     scale_x_reordered() +
     facet_wrap(.~ s, ncol=2,scales = "free_x") + 
     theme(axis.text.x=element_text(angle=90,hjust=1,vjust=.5,colour='gray50'))+
     geom_hline( aes(yintercept = Z),data = dummy2)

enter image description here

答案 1 :(得分:1)

执行此操作的技巧是使用映射(或switch()甚至ifelse())函数根据您在其中使用的变量来映射z的值方面,即ii$s

数据帧ii

rbind(head(ii),tail(ii))

     nbr x2 s sn z
9   7.86  i 3  i 0
4   9.20  d 3  d 0
7   9.53  g 3  g 0
8   9.81  h 3  h 0
5  10.90  e 3  e 0
2  11.53  b 3  b 0
60  6.92  j 8  j 4
54  7.34  d 8  d 4
58  7.40  h 8  h 4
55  8.01  e 8  e 4
51  8.09  a 8  a 4
57  8.40  g 8  g 4

ggplot2代码:

ggplot(data = ii, aes(reorder_within(sn, nbr, s), nbr)) +
    geom_hline(aes(yintercept = z), col="dodgerblue4") + 
    geom_bar(stat = 'identity') +
     scale_x_reordered() +
     facet_wrap(.~ s, ncol=2,scales = "free_x") + 
     theme(axis.text.x=element_text(angle=90,
                                    hjust=1,
                                    vjust=.5,
                                    colour='gray50'))

打印以下内容: enter image description here