0

I am attempting to make a faceted barchart with each facet representing a sector of employment with three time points for each display.

Here's my code and sample data

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)

When checking data types everything checks out. My factors are ordered properly and my employment figures line up with their proper factors. When I attempt to use ggplot, though, I run into an issue. The ggplot code:

library(ggplot2)
library(scales)
sampleplot <- ggplot(df, aes(x=Year, y=Avg.Emp)) + geom_bar(stat = "identity") + 
facet_wrap(~Sector) + ylim(0,5200) 

My resulting plot is only showing a single value for each year in the facets. Where am I making my mistake? (I apologize for the lack of an image, I don't have enough reputation for it) Is there anyway I can get my values for each year to show in the proper facets?

EDIT I realize I only had one sector per year given the way I made my dataframe. The updated syntax is here and fixed the issue

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)

elmuertefurioso
  • 278
  • 1
  • 9
  • 2
    your data only has one Year per Sector type, so after splitting by Sector (facet_wrap), you only have one bar to plot, examine `table(df$Sector, df$Year)` – Rorschach Aug 13 '15 at 20:53
  • after nearly giving up and asking this question I found that error. i am going to edit the question now. thank you. boneheaded on my part – elmuertefurioso Aug 13 '15 at 20:56

0 Answers0