绘制一个分组在一起的条形图 [英] Plotting a bar chart with years grouped together

查看:149
本文介绍了绘制一个分组在一起的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是fivethirtyeight bechdel数据集,位于 https://github.com/rudeboybert/fivethirtyeight,并试图重新创建文章中显示的第一个阴谋 https://fivethirtyeight.com/features/the-dollar-and-cents-case-against-hollywoods-exclusion-of-women/ 。我很难让这些年来与他们在文章中的做法类似。

I am using the fivethirtyeight bechdel dataset, located here https://github.com/rudeboybert/fivethirtyeight, and am attempting to recreate the first plot shown in the article here https://fivethirtyeight.com/features/the-dollar-and-cents-case-against-hollywoods-exclusion-of-women/. I am having trouble getting the years to group together similarly to how they did in the article.

这是我现有的代码:

ggplot(data = bechdel, aes(year)) +
geom_histogram(aes(fill = clean_test), binwidth = 5, position = "fill") +
scale_fill_manual(breaks = c("ok", "dubious", "men", "notalk", "nowomen"), 
            values=c("red", "salmon", "lightpink", "dodgerblue", 
"blue")) +
theme_fivethirtyeight()


推荐答案

我使用直方图几何看到你要去的地方,但这看起来更像一个分类条形图。一旦你采取了这种方法,就会更容易,在一些难看的代码之后得到年份列上的正确标签。

I see where you were going with using the histogram geom but this really looks more like a categorical bar chart. Once you take that approach it's easier, after a bit of ugly code to get the correct labels on the year columns.

,并且需要将某些格式应用于538图表,但我会将其保留。

The bars are stacked in the wrong order on this one, and there needs to be some formatting applied to look like the 538 chart, but I'll leave that for you.

library(fivethirtyeight)
library(tidyverse)
library(ggthemes)
library(scales)

# Create date range column
bechdel_summary <- bechdel %>%
mutate(date.range = ((year %/% 10)* 10)  + ((year %% 10) %/% 5 * 5)) %>%
mutate(date.range = paste0(date.range," - '",substr(date.range + 5,3,5)))

ggplot(data = bechdel_summary, aes(x = date.range, fill = clean_test)) + 
geom_bar(position = "fill", width = 0.95) +
scale_y_continuous(labels = percent) +
theme_fivethirtyeight()

gplot

这篇关于绘制一个分组在一起的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆