简体   繁体   English

ggplot2中的geom_bar位置宽度问题

[英]geom_bar position width issue in ggplot2

For a fun way to learn ggplot2 I'm trying to reproduce the fivethirtyeight barplot on bob ross paintings . 为了学习ggplot2一个有趣的方式,我试图在鲍勃罗斯绘画上重现第五十八条情节 I've attached the reprex of my attempted code below: 我在下面附加了我尝试的代码的代表:

library(tidyverse)
library(ggthemes)

# OPTION 1 - Download raw data directly from source
ross_url <- "https://raw.githubusercontent.com/fivethirtyeight/data/master/bob-ross/elements-by-episode.csv"
ross_dat <- read_csv(file = ross_url)

ross_tidy <- ross_dat %>%
                gather(key = tag, value = indicator, APPLE_FRAME:WOOD_FRAMED)
new_tag_fmt <- function(str){
    str_replace_all(string = str, pattern = "_",
                             replacement = " ") %>%
        str_trim() %>%
        str_to_title() %>%
        return()
}

tot_episodes <- ross_tidy %>% select(EPISODE) %>% n_distinct()
ross_tidy2 <- ross_tidy %>%
                group_by(tag) %>%
                summarize(total = sum(indicator)) %>%
                ungroup() %>%
                mutate(tag = as.factor(new_tag_fmt(str = tag)),
                              perc = round(total/tot_episodes, 2),
                              perc_fmt = scales::percent(perc)) %>%
                arrange(desc(total)) %>%
                filter(total >= 5)


ggplot(ross_tidy2, aes(x = reorder(tag, perc), y = perc)) +
    geom_bar(stat = "identity", fill = "#198DD1",
                      width = 2, position = "dodge") +
    coord_flip() +
    labs(title = "The Paintings of Bob Ross",
                  subtitle = "Percentage containing each element") +
    geom_text(data = ross_tidy2, nudge_y = 0.02, angle = 270,
                           aes(reorder(tag, total), y = perc, label = perc_fmt),
                       family = "Courier", color = "#3E3E3E") +
    scale_color_fivethirtyeight("cyl") +
    theme_fivethirtyeight() +
    theme(panel.border = element_blank(),
                   panel.grid.major = element_blank(),
                   panel.grid.minor = element_blank(),
                   axis.line = element_blank(),
                   axis.ticks.x = element_blank(),
                   axis.text.x = element_blank(),
                   plot.title = element_text(size = 18, hjust=-0.5),
                   plot.subtitle = element_text(size = 14, hjust=-0.5),
                   axis.text.y = element_text(size = 12))
#> Warning: position_dodge requires non-overlapping x intervals

Created on 2018-07-14 by the reprex package (v0.2.0). reprex软件包 (v0.2.0)于2018-07-14创建。

The problem here is I keep getting the warning: 这里的问题是我不断收到警告:

Warning: position_dodge requires non-overlapping x intervals 警告:position_dodge需要x间隔不重叠

Could anyone please show me the issue in the code, the tag variable is a factor ie categorical so I thought the above should work. 谁能告诉我代码中的问题, tag变量是一个因素,即绝对的,所以我认为上面的应该起作用。

NOTE: Full credit to fivethirtyeight for providing the data to reproduce their work! 注意:为提供用于重现其工作的数据,我们将全数归功于fourthirtyeight!

ross_tidy2 %>%
 ggplot(data = ., aes(x = reorder(tag, perc), y = perc)) +
  geom_bar(stat = "identity",width = 0.9) +
  coord_flip() 

is enough 足够
you don't need position_dodge (you don't have several groups) 您不需要position_dodge(您没有几个组)

在此处输入图片说明

The bar widths are too large, so it's tough for them to "dodge" each other. 条形宽度太大,因此很难彼此“躲避”。 I set the width to 1 and didn't get the error. 我将宽度设置为1,但未收到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM