我正在尝试制作一个分面条形图,每个方面代表一个就业部门,每个显示三个时间点。
这是我的代码和示例数据
Year <- c("2002", "2010", "2012", "2002", "2010", "2012", "2002", "2010", "2012")
Year <- ordered(Year, levels = c("2002", "2010", "2012"))
Sector <- c("Production", "Processing", "Distribution", "Production", "Processing", "Distribution",
"Production", "Processing", "Distribution")
Sector <- ordered(Sector, levels = c("Production", "Processing", "Distribution"))
Avg.Emp <- c(363, 4458, 2962, 225, 4767, 3552, 148, 5137, 3996)
df <- data.frame(Year, Sector, Avg.Emp)
检查数据类型时,一切都会检查出来。我的因素排序正确,我的就业数据与其适当的因素一致。但是,当我尝试使用 ggplot 时,我遇到了一个问题。ggplot 代码:
library(ggplot2)
library(scales)
sampleplot <- ggplot(df, aes(x=Year, y=Avg.Emp)) + geom_bar(stat = "identity") +
facet_wrap(~Sector) + ylim(0,5200)
我得到的图只在各个方面显示了每年的一个值。我在哪里犯错?(我为缺乏形象道歉,我没有足够的声誉)无论如何我可以让我每年的价值观在适当的方面表现出来?
编辑 我意识到考虑到我制作数据框的方式,我每年只有一个部门。更新的语法在这里并解决了问题
Year <- c("2002", "2002", "2002", "2010", "2010","2010","2012","2012","2012")
Year <- ordered(Year, levels = c("2002", "2010", "2012"))
Sector <- c("Production", "Processing", "Distribution", "Production", "Processing", "Distribution",
"Production", "Processing", "Distribution")
Sector <- ordered(Sector, levels = c("Production", "Processing", "Distribution"))
Avg.Emp <- c(363, 4458, 2962, 225, 4767, 3552, 148, 5137, 3996)
df <- data.frame(Year, Sector, Avg.Emp)