을 가장 낮은 가장 높은에서 바의 순서를, 나는 시도ggplot2는 각면에 아래 <code>df</code>에서
각면에서 가장 높은에서 가장 낮은 막대를 다시 정렬 할
df <- df %>% tidyr::gather("var", "value", 2:4)
ggplot(df, aes (x = reorder(id, -value), y = value, fill = id))+
geom_bar(stat="identity")+facet_wrap(~var, ncol =3)
각 패싯에서 막대를 가장 높은 순서로 정렬하지 않았습니다.
내가 원하는 것을 얻는 다른 방법을 찾았습니다. 그때 grid.arrange()
#I got this function from @eipi10's answer
#http://stackoverflow.com/questions/38637261/perfectly-align-several-plots/38640937#38640937
#Function to extract legend
# https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
g_legend<-function(a.gplot) {
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)
}
p1 <- ggplot(df[df$var== "A", ], aes (x = reorder(id, -value), y = value, fill = id))+
geom_bar(stat="identity") + facet_wrap(~var, ncol =3)
fin_legend <- g_legend(p1)
p1 <- p1 + guides(fill= F)
p2 <- ggplot(df[df$var== "B", ], aes (x = reorder(id, -value), y = value, fill = id))+
geom_bar(stat="identity") + facet_wrap(~var, ncol =3)+guides(fill=FALSE)
p3 <- ggplot(df[df$var== "C", ], aes (x = reorder(id, -value), y = value, fill = id))+
geom_bar(stat="identity") + facet_wrap(~var, ncol =3)+guides(fill=FALSE)
grid.arrange(p1, p2, p3, fin_legend, ncol =4, widths = c(1.5, 1.5, 1.5, 0.5))
결과를 사용하여 모든 플롯을 결합, 한 번에 각 변수를 음모 나 막대를 주문 도울 수있는 간단한 방법이 있다면 내가 궁금해
를 원하는 것입니다했다 개별 변수를 개별적으로 플롯하지 않고 모든 패싯에서 가장 높은 값에서 가장 낮은 값으로 정렬 한 다음 결합합니다. 어떤 제안이라도 대단히 감사하겠습니다.
데이터
df <- read.table(text = c("
id A B C
site1 10 15 20
site2 20 10 30
site3 30 20 25
site4 40 35 40
site5 50 30 35"), header = T)
시간과 도움에 감사드립니다. – aelwan
코드의 각 줄에 대한 자세한 설명을 정말 고맙게 생각합니다. 많은 분들께 감사드립니다. – aelwan