[英]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.