[英]geom_bar position width issue in ggplot2
為了學習ggplot2
一個有趣的方式,我試圖在鮑勃羅斯繪畫上重現第五十八條情節 。 我在下面附加了我嘗試的代碼的代表:
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
由reprex軟件包 (v0.2.0)於2018-07-14創建。
這里的問題是我不斷收到警告:
警告:position_dodge需要x間隔不重疊
誰能告訴我代碼中的問題, tag
變量是一個因素,即絕對的,所以我認為上面的應該起作用。
注意:為提供用於重現其工作的數據,我們將全數歸功於fourthirtyeight!
條形寬度太大,因此很難彼此“躲避”。 我將寬度設置為1,但未收到錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.