如何使用ggplot创建两个表的geom_bar [英] How to create a geom_bar of two tables using ggplot
问题描述
我在使用ggplot创建geom_bar两个表时遇到问题。
我有两个表:
1)
典型的men_weekly_earnings
1 16至24岁493
2 16至19岁392
3 20至24岁507
4 25至34岁755
5 35至44岁964
6 45至54岁1011
7 55至64岁1021
8 65岁及以上942
2)
独有的women_weekly_earnings
1 16至24岁451
2 16至19岁357
3 20至24岁468
4 25至34岁679
5 35至44岁781
6 45至54岁780
7 55至64岁780
8 65岁及以上740
每个表都有不同年龄的每周收入数据。
我的目标是将两个表合并为一个表,成为
请注意如何创建 df_all
我要添加一列,根据数据来自何处来指定来源(男人 /女人)。这样,您就可以在 ggplot
调用中进行细分。还要注意,在堆叠之前,我必须使两个数据集之间的列名保持一致。我为此使用了 setNames
命令。
数据:
women_df<-结构(列表(特征= c( 16到24岁, 16至19岁,
20至24年, 25至34年, 35至44年, 45至54年,
55至64年, 65岁及以上),women_weekly_earnings = c(451L,
357L,468L,679L,781L,780L,780L,740L)),.Names = c( characteristic,
women_weekly_earnings) ,row.names = c(NA,-8L),class = data.frame)
men_df<-结构(list(characteristic = c( 16至24岁, 16至19年,
20至24年, 25至34年, 35至44年, 45至54年,
55至64年, 65岁以下),men_weekly_earnings = c(493L,
392L,507L,755L,964L,1011L,1021L,942L)),.Names = c( characteristic,
men_weekly_earnings), row.names = c(NA,-8L),class = data.frame)
I have a problem creating a geom_bar two tables using ggplot. I have two tables:
1)
characteristic men_weekly_earnings
1 16 to 24 years 493
2 16 to 19 years 392
3 20 to 24 years 507
4 25 to 34 years 755
5 35 to 44 years 964
6 45 to 54 years 1011
7 55 to 64 years 1021
8 65 years and older 942
2)
characteristic women_weekly_earnings
1 16 to 24 years 451
2 16 to 19 years 357
3 20 to 24 years 468
4 25 to 34 years 679
5 35 to 44 years 781
6 45 to 54 years 780
7 55 to 64 years 780
8 65 years and older 740
Each table have data of weekly earnings by a different age. my goal is to combine the two tables into one to be like this.
the x axis is the characteristic column and the y axis is the weekly_earnings column.
For now i tried this code (for the men table, and it's not working
ggplot(data = men) + geom_col(mapping = aes(x= characteristic,y= men_weekly_erning))
what can I do now?
Thank you.
Welcome to Stack Overflow!
I think your best option would be to stack the two datasets together and then plot them. Something like this:
df_all <- rbind(cbind(setNames(men_df, c("characteristic", "weekly_earnings")), source = "men"),
cbind(setNames(women_df, c("characteristic", "weekly_earnings")), source = "women"))
ggplot(data = df_all) +
geom_col(mapping = aes(x= source, y = weekly_earnings, fill = characteristic), position = position_dodge())
Notice how when I create df_all
I'm adding a column specifying the source (either "men"/"women") depending on where the data came from. This allows you to break it out in the ggplot
call. Also note the I had to make the column names consistent between the two datasets before stacking. I used the setNames
command for this.
Data:
women_df <- structure(list(characteristic = c("16 to 24 years", "16 to 19 years",
"20 to 24 years", "25 to 34 years", "35 to 44 years", "45 to 54 years",
"55 to 64 years", "65 years and older"), women_weekly_earnings = c(451L,
357L, 468L, 679L, 781L, 780L, 780L, 740L)), .Names = c("characteristic",
"women_weekly_earnings"), row.names = c(NA, -8L), class = "data.frame")
men_df <- structure(list(characteristic = c("16 to 24 years", "16 to 19 years",
"20 to 24 years", "25 to 34 years", "35 to 44 years", "45 to 54 years",
"55 to 64 years", "65 years and older"), men_weekly_earnings = c(493L,
392L, 507L, 755L, 964L, 1011L, 1021L, 942L)), .Names = c("characteristic",
"men_weekly_earnings"), row.names = c(NA, -8L), class = "data.frame")
这篇关于如何使用ggplot创建两个表的geom_bar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!