使用ggplot创建geom_bar两个表时遇到问题。 我有两张桌子:
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
每张表都有不同年龄的每周收入数据。 我的目标是将两个表合并为一个 like this
x轴是特征列,y轴是weekly_earnings列。
现在我尝试了这个代码(对于男性表,它没有工作
ggplot(data = men) + geom_col(mapping = aes(x= characteristic,y= men_weekly_erning))
我现在能做什么?
谢谢。
答案 0 :(得分:2)
欢迎使用Stack Overflow!
我认为您最好的选择是将两个数据集堆叠在一起然后绘制它们。像这样:
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())
请注意我何时创建df_all
我是否添加了指定来源的列(&#34; men&#34; /&#34; women&#34;),具体取决于数据的来源从。这允许您在ggplot
调用中将其分解。另请注意,我必须在堆叠之前使两个数据集之间的列名一致。我使用了setNames
命令。
数据:强>
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")