使用ggplot2;如何让position_dodge()使用这个例子?

时间:2018-01-30 20:28:42

标签: r ggplot2 tidyverse

背景

我从Stephen Few Example获取了数据,并希望为每个条形添加标签,以便从图形的侧面拉出图例。

" Hack Graphic"中的代码部分让我在那里,因为我无法让position_dodge()使用文本标签。

加载数据

library(tidyverse)
library(forcats)

### Build data from his table
candidates <- tibble::tibble(`Rating Areas` = c("Experience", 
"Communication", "Friendliness", "Subject matter knowledge", "Presentation", 
"Education"), `Karen Fortou` = c(4,3.5, 4, 4, 3, 3.5), `Mike Rafun` = c(4.5, 
2, 2, 5, 1.5, 4.5), `Jack Nymbul` = c(2.5, 5, 4.5, 2.5, 2.75, 2)) %>%
gather("Candidates", "Score", -`Rating Areas`) 

# The totals for each candidate
totals <- candidates %>% group_by(Candidates) %>% summarise(Score = 
sum(Score))

Hack Graphic

注意我如何使用手动创建的x轴值(x = c(seq(.6,1.35, by = .15), seq(1.6,2.35, by = .15), seq(2.6,3.35, by = .15)))来放置标签,而不是使用this post中所述的position = position_dodge()

candidates %>% 
  ggplot(aes(x = fct_reorder(Candidates, Score), y = Score)) +
  geom_col(data = totals, alpha = .45) +
  geom_col(aes(fill = `Rating Areas`), position = position_dodge(.9), color = "black", 
           show.legend = FALSE) +
  geom_text(label = rep(c("Experience", "Communication", "Friendliness", 
           "Subject matter knowledge", "Presentation", "Education"),3), 
            x = c(seq(.6,1.35, by = .15), seq(1.6,2.35, by = .15), 
            seq(2.6,3.35, by = .15)), y = 5.1, angle = 90, color = "black", 
            hjust = "left", size = 4, fontface = "bold") +
  scale_fill_brewer(type = "qual") + 
  scale_y_continuous(breaks = seq(0, 25, by = 2)) +
  theme_bw() + 
  labs(x = "\nCandidates", y = "Rating Score") +
  theme(axis.text.x = element_text(size = 14, color = "black"), legend.text = element_text(size = 14),
    legend.title = element_text(size = 15), axis.title = element_text(size = 15))

不起作用的图形代码

当我使用geom_text(aes(label =评分区域), position = position_dodge(width = 0.9), angle = 90, color = "black", hjust = "left", size = 4, fontface = "bold")按照上一个堆栈答案中的示例时,它不会将标签展开到每个栏中。

我必须遗漏一些明显的东西。 请帮助我们了解position_dodge()如何使用此示例?

candidates %>% 
  ggplot(aes(x = fct_reorder(Candidates, Score), y = Score)) +
  geom_col(data = totals, alpha = .45) +
  geom_col(aes(fill = `Rating Areas`), position = position_dodge(.9), color = "black", show.legend = FALSE) +
  geom_text(aes(label = `Rating Areas`), position = position_dodge(width = 0.9), angle = 90, color = "black", hjust = "left", size = 4, fontface = "bold") +
  scale_fill_brewer(type = "qual") + 
  scale_y_continuous(breaks = seq(0, 25, by = 2)) +
  theme_bw() + 
  labs(x = "\nCandidates", y = "Rating Score") +
  theme(axis.text.x = element_text(size = 14, color = "black"), legend.text = element_text(size = 14), legend.title = element_text(size = 15), axis.title = element_text(size = 15))

1 个答案:

答案 0 :(得分:1)

我认为您需要为geom_colgeom_text设置相同的映射。您可以将fill = Rating Areas添加到geom_text的美学中。你会收到警告。

candidates %>% 
    ggplot(aes(x = fct_reorder(Candidates, Score), y = Score)) +
    geom_col(data = totals, alpha = .45) +
    geom_col(aes(fill = `Rating Areas`), position = position_dodge(.9), color = "black", show.legend = FALSE) +
    geom_text(aes(fill = `Rating Areas`, label = `Rating Areas`), position = position_dodge(width = 0.9), angle = 90, color = "black", hjust = "left", size = 4, fontface = "bold") +
    scale_fill_brewer(type = "qual") + 
    scale_y_continuous(breaks = seq(0, 25, by = 2)) +
    theme_bw() + 
    labs(x = "\nCandidates", y = "Rating Score") +
    theme(axis.text.x = element_text(size = 14, color = "black"), legend.text = element_text(size = 14), legend.title = element_text(size = 15), axis.title = element_text(size = 15))

编辑:以下是一种没有警告的方法:

candidates %>% 
    ggplot(aes(x = fct_reorder(Candidates, Score), y = Score, fill = `Rating Areas`)) +
    geom_col(data = totals, aes(x = fct_reorder(Candidates, Score), y = Score), alpha = .45, inherit.aes = FALSE) +
    geom_col(position = position_dodge(.9), color = "black", show.legend = FALSE) +
    geom_text(aes(label = `Rating Areas`), position = position_dodge(width = 0.9), angle = 90, color = "black", hjust = "left", size = 4, fontface = "bold") +
    scale_fill_brewer(type = "qual") + 
    scale_y_continuous(breaks = seq(0, 25, by = 2)) +
    theme_bw() + 
    labs(x = "\nCandidates", y = "Rating Score") +
    theme(axis.text.x = element_text(size = 14, color = "black"), legend.text = element_text(size = 14), legend.title = element_text(size = 15), axis.title = element_text(size = 15))