我最终非常需要这个,并且已经切换到geom_segment() 用于许多传统的条形图。
library(hrbrthemes)
library(ggplot2)
library(dplyr)
data_frame(
measure = c("Measure A","Measure B","Measure C","Measure D","Measure E","Measure F","Measure G"),
domain = c("Efficiency","Efficiency","Satisfaction","Satisfaction", "Satisfaction","Satisfaction","Satisfaction"),
overall = c(56, 78, 19, 87, 45, 51, 19),
company = c(45, 89, 18, 98, 33, 55, 4)
) %>%
mutate(fill = ifelse(overall > company, " Below Overall "," Above Overall ")) %>%
ggplot() +
geom_segment(aes(x=company, xend=0, y=measure, yend=measure, color=fill), size=4) +
geom_point(aes(x=overall, y=measure, color="Overall"), size=6, shape=124, show.legend = FALSE) +
scale_x_comma() +
scale_color_manual(name=NULL,
values=c(`Overall`="grey3", " Below Overall " = "lightpink2",
" Above Overall " = "lightblue2")) +
facet_wrap(~domain, ncol=1, scales="free_y") +
labs(x="Measure", y=NULL) +
theme_ipsum(grid="X")
这样做的另一个好处是不需要coord_flip()。
如果您真的需要单独的Overall 指南,也有办法做到这一点。
更新
刻面排序和间距…
library(hrbrthemes)
library(ggplot2)
library(dplyr)
library(grid)
library(gridExtra)
data_frame(
measure = c("Measure A","Measure B","Measure C","Measure D","Measure E","Measure F","Measure G"),
domain = c("Efficiency","Efficiency","Satisfaction","Satisfaction", "Satisfaction","Satisfaction","Satisfaction"),
overall = c(56, 78, 19, 87, 45, 51, 19),
company = c(45, 89, 18, 98, 33, 55, 4)
) %>%
mutate(fill = ifelse(overall > company, " Below Overall "," Above Overall ")) %>%
arrange(desc(measure)) %>%
mutate(measure = factor(measure, levels=unique(measure))) %>%
ggplot() +
geom_segment(aes(x=company, xend=0, y=measure, yend=measure, color=fill), size=4) +
geom_point(aes(x=overall, y=measure, color="Overall"), size=6, shape=124, show.legend = FALSE) +
scale_x_comma() +
scale_color_manual(name=NULL,
values=c(`Overall`="grey3", " Below Overall " = "lightpink2",
" Above Overall " = "lightblue2")) +
facet_wrap(~domain, ncol=1, scales="free_y") +
labs(x="Measure", y=NULL) +
theme_ipsum(grid="X") -> gg
gb <- ggplot_build(gg)
gt <- ggplot_gtable(gb)
gt$heights[[7]] <- unit(2/5, "cm") # top panel has 2 out of 5 factors vs 5 out of 5 in the bottom one
grid.newpage() ;
grid.draw(gt)