简体   繁体   English

R 中 Y 轴上带有两个变量的 bin 频率分布直方图

[英]Frequency Distribution Histogram with Bins with Two Variables on the Y-axis in R

I was given this dataframe, which is at the same time a frequency distribution, and was given a task of plotting a histogram of the age distribution of the whole population adding to the plot the male and female profile.我得到了这个数据框,它同时是一个频率分布,并被赋予了绘制整个人口年龄分布的直方图的任务,并将男性和女性的轮廓添加到图中。 What I need to achieve is a histogram like this one for example: Two-variable frequency bar plot with the male and female profile overlapping, but with the AgeClasses on the x axis.我需要实现的是像这样的直方图,例如:男性和女性轮廓重叠的双变量频率条形图,但在 x 轴上有 AgeClasses。 This is my code:这是我的代码:

AgeClasses <- c('0-9','10-19','20-29','30-39','40-49', '50-59', '60-69','70-79','80-89', '90-99')
Frequencies <- c(1000,900,800,700,600,500,400,300,200,100)
SexRatioFM <- c(0.4,0.42,0.44,0.48,0.52,0.54,0.55,0.58,0.6,0.65)
df$Females <- c(SexRatioFM*Frequencies)
df$Males <- c(Frequencies-Females)

library(ggplot2)


ggplot(df) +
    geom_bar(mapping = aes(x = AgeClasses, y = Females), stat = "identity")

I would really appreciate your help in solving this task.我非常感谢您帮助解决此任务。

This type of plot is a stacked bar plot .这种类型的图是堆积条形图 To produce it most easily with ggplot2, you need to transform your data into long format, so that one column has all the counts for both male and female, and another column contains a factor variable with the labels "Male" and "Female".为了使用 ggplot2 最轻松地生成它,您需要将数据转换为长格式,以便一列包含男性和女性的所有计数,另一列包含一个带有标签“男性”和“女性”的因子变量。 You can do this using tidyr::pivot_longer :您可以使用tidyr::pivot_longer执行此操作:

library(ggplot2)
library(tidyr)

pivot_longer(df, cols = c(Females, Males)) %>%
  ggplot() +
  geom_col(mapping = aes(x = AgeClasses, y = value, fill = name)) +
  labs(x = "Age", y = "Count", fill = "Gender")

在此处输入图片说明

Try the following code:试试下面的代码:

AgeClasses <- c('0-9','10-19','20-29','30-39','40-49', '50-59', '60-69','70-79','80-89', '90-99')
Frequencies <- c(1000,900,800,700,600,500,400,300,200,100)
SexRatioFM <- c(0.4,0.42,0.44,0.48,0.52,0.54,0.55,0.58,0.6,0.65)
Females <- SexRatioFM*Frequencies
Males <- Frequencies-Females
df <- data.frame(AgeClasses=AgeClasses, Females=Females, Males=Males)
df <- reshape2::melt(df, id.vars = 'AgeClasses')
library(ggplot2)


ggplot(df) +
  geom_bar(mapping = aes(x = AgeClasses, y = value, fill=variable), stat = "identity")

Allan is right, but to make the one in the plot, you need the bars superposed rather than stacked. Allan 是对的,但要在情节中制作一个,您需要将条形叠加而不是堆叠。 I did it like this:我是这样做的:


library(ggplot2)
library(dplyr)
AgeClasses <- c('0-9','10-19','20-29','30-39','40-49', '50-59', '60-69','70-79','80-89', '90-99')
Frequencies <- c(1000,900,800,700,600,500,400,300,200,100)
SexRatioFM <- c(0.4,0.42,0.44,0.48,0.52,0.54,0.55,0.58,0.6,0.65)
df <- tibble(
Females = c(SexRatioFM*Frequencies),
Males = c(Frequencies-Females), 
AgeClasses = AgeClasses, 
Frequencies=Frequencies, 
SexRatioFM = SexRatioFM)

df %>% select(AgeClasses, Males, Females) %>% 
  tidyr::pivot_longer(cols=c(Males, Females), names_to = "gender", values_to="val") %>% 
ggplot() +
  geom_bar(mapping = aes(x = AgeClasses, y=val, fill=gender, alpha=gender), stat="identity", position="identity") + 
  scale_alpha_manual(values=c(.5, .4))

在此处输入图片说明

You'll need to revamp how you create your sample dataframe.您需要修改创建示例数据框的方式。 Here's one way to do it:这是一种方法:

df <- data.frame(
  AgeClasses = c('0-9','10-19','20-29','30-39','40-49', '50-59', '60-69','70-79','80-89', '90-99'),
  Frequencies = c(1000,900,800,700,600,500,400,300,200,100),
  SexRatioFM = c(0.4,0.42,0.44,0.48,0.52,0.54,0.55,0.58,0.6,0.65))

df$Females = df$SexRatioFM*df$Frequencies
df$Males = df$Frequencies-df$Females 

library(ggplot2)

ggplot(df) +
  geom_bar(mapping = aes(x = AgeClasses, y = Females), fill="purple", stat = "identity", alpha=.8) +
  geom_bar(mapping = aes(x = AgeClasses, y = Males), fill="navy blue", stat = "identity", alpha=.4)

And you should get something like this:你应该得到这样的东西:

示例输出

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

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