Facet Wrap ggplot geom_col具有不同的条宽

时间:2018-01-18 22:11:12

标签: r ggplot2

我正在创建一个使用geom_col的条形图,使用gem_point(line)来比较性能与“基准”。性能测量属于​​不同的域,因此我使用facet_wrap将域可视地分割成组,以便更容易查看。但是,由于每个域有不同数量的度量,因此条形“高度”/宽​​度不同。

    df = 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)
)

ggplot(df %>% mutate(fill = ifelse(overall > company, " Below Overall  "," Above Overall  ")), aes(measure)) + 
  geom_col(aes(y=company, fill= fill)) + geom_point(aes(y=overall, color="overall"), size=6, shape=124) + coord_flip() + 
  scale_color_manual(values=c("grey3"),labels=c("Overall")) + scale_fill_manual(values=c(" Below Overall  "="lightpink2"," Above Overall  "="lightblue2")) + facet_wrap(~domain, ncol=1, scales="free_y")

enter image description here

我从一个较旧的问题中看到,有关计算每个域的条数,然后将宽度乘以一个因子。但我无法弄清楚如何做到这一点并将其应用到我的图表中。

2 个答案:

答案 0 :(得分:2)

也许将<template> <div id="app-loading"> <div class="words"> <span v-for="setting in settings" v-html="setting.lettersHTML" :id="setting.id" class="word"></span> </div> </div> </template> <script> export default { data() { return { settings: [ { word: 'WordOne', id: 1, lettersArray: null, lettersHTML: null }, { word: 'WordTwo', id: 2, lettersArray: null, lettersHTML: null } ], currentWord: 1 } }, created() { this.splitLetters(); }, mounted() { setInterval(this.changeWord, 1500); }, methods: { splitLetters() { this.settings.forEach((setting) => { let letters = []; for (let i = 0; i < setting.word.length; i++) { let letter = `<span class="letter">${ setting.word.charAt(i) }</span>`; letters.push(letter); } setting.lettersArray = letters; setting.lettersHTML = letters.join(''); }); }, changeWord() { let current = document.getElementById(this.currentWord).getElementsByTagName('span'); let next = (this.currentWord == this.settings.length) ? document.getElementById(1).getElementsByTagName('span') : document.getElementById(this.currentWord + 1).getElementsByTagName('span'); // Animate the letters in the current word. for (let i = 0; i < current.length; i++) { this.animateLetterOut(current, i); } // Animate the letters in the next word. for (let i = 0; i < next.length; i++) { this.animateLetterIn(next, i); } this.currentWord = (this.currentWord == this.settings.length) ? 1 : this.currentWord + 1; }, animateLetterOut(current, index) { setTimeout(() => { current[index].className = 'letter out'; }, index * 300); }, animateLetterIn(next, index) { setTimeout(() => { next[index].className = 'letter in'; }, 340 + (index * 300)); } } } </script> <style lang="scss" scoped> #app-loading { font-size: 4rem; } .words, .word { border: 1px solid rosybrown; } .letter { text-decoration: underline; // Not working. } .letter.in { color: red; // Not working. } .letter.out { color: blue; // Not working. } </style> facet_grid一起使用会令人满意:

space = "free"

enter image description here

答案 1 :(得分:2)

我最终需要这么多,并且已经切换到许多传统条形图的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")

enter image description here

这样做的另一个好处是不需要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)

enter image description here