简体   繁体   English

R:堆叠 geom_bar (ggplot) 中每个 X 轴元素的多列

[英]R: Multiple Columns for Each X-axis Element in Stacked geom_bar (ggplot)

I have data on employment in different skill classes for different provinces over time.我有不同省份不同技能等级的就业数据。 I'd like to show the employment over time in different provinces and classes in one graph.我想在一张图中显示不同省份和班级的就业情况。 The following figure shows what I want but only for one year (2000)下图显示了我想要的但只有一年(2000)

ggplot(df, aes(fill=classes, y=total/10^6, x=province)) + 
  geom_bar(position="stack", stat="identity") 

在此处输入图片说明

But I'd like to have each bar (showing each year) being repeated two times (in the example I have 2 years) for each province in the same graph.但是我希望在同一图表中为每个省的每个条形(每年显示)重复两次(在示例中我有 2 年)。 In other words, I'd like to show the 2001 data in the same graph as shown above beside the bars for 2000.换句话说,我想在上面显示的同一图表中显示 2001 年的数据,旁边是 2000 年的条形图。

Here is part of the data:以下是部分数据:

df <- structure(list(year = c(2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L), province = c("Alberta", "Alberta", "Alberta", "Alberta", 
"Manitoba", "Manitoba", "Manitoba", "Manitoba", "Alberta", "Alberta", 
"Alberta", "Alberta", "Manitoba", "Manitoba", "Manitoba", "Manitoba"
), classes = structure(c(2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L, 1L, 2L, 3L, 4L, 1L), .Label = c("[0,0.2]", "(0.2,0.4]", 
"(0.4,0.6]", "(0.6,0.8]", "(0.8,1)", "1"), class = "factor"), 
    total = c(11387250L, 4373500L, 18250L, 3215500L, 3984750L, 
    1414750L, 2000L, 1222750L, 11838250L, 4390000L, 21250L, 3272750L, 
    4019750L, 1331750L, 7750L, 1237000L)), row.names = c(NA, 
-16L), vars = c("year", "province", "classes"), drop = TRUE, class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), indices = list(3L, 0L, 1L, 2L, 
    7L, 4L, 5L, 6L, 11L, 8L, 9L, 10L, 15L, 12L, 13L, 14L), group_sizes = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
    year = c(2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
    2000L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L
    ), province = c("Alberta", "Alberta", "Alberta", "Alberta", 
    "Manitoba", "Manitoba", "Manitoba", "Manitoba", "Alberta", 
    "Alberta", "Alberta", "Alberta", "Manitoba", "Manitoba", 
    "Manitoba", "Manitoba"), classes = structure(c(1L, 2L, 3L, 
    4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("[0,0.2]", 
    "(0.2,0.4]", "(0.4,0.6]", "(0.6,0.8]", "(0.8,1)", "1"), class = "factor")), row.names = c(NA, 
-16L), vars = c("year", "province", "classes"), drop = TRUE, class = "data.frame"))

Is it what you are looking for ?这是您要找的吗?

ggplot(df, aes(fill=classes, y=total/10^6, x=as.factor(year))) + 
  geom_bar(position="stack", stat="identity") +
  facet_wrap(.~province)

在此处输入图片说明

As suggested by @user12728748, you can modify margin of the panel to make it more looks like a single plot:正如@user12728748 所建议的,您可以修改面板的边距,使其更像一个图:

ggplot(df, aes(fill=classes, y=total/10^6, x=as.factor(year))) + 
  geom_bar(position="stack", stat="identity") +
  facet_wrap(.~province)+
  theme(panel.margin = grid::unit(-1.25, "lines"))

在此处输入图片说明

NB: Be cautious because this trick can't be used if you set scales = free in your facet_wrap .注意:要小心,因为如果您在facet_wrap设置scales = free ,则无法使用此技巧。

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

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