0

I'm a bit stumped by some irregular plotting from barchart from the lattice package in R. Specifically, I'm attempting to do a multiplot of barcharts of frequency data for some demographic information.

The following code should load my data correctly, and reproduce (hopefully) the issue I'm seeing...

library(lattice)

compareSamples <- structure(list(Variable = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 18L, 19L, 21L, 
20L, 23L, 22L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 17L, 17L, 43L, 
41L, 6L, 40L, 39L, 38L, 37L, 35L, 44L, 36L, 33L, 34L, 32L, 42L, 
31L), .Label = c("Educator", "Engine Boss/ Operator", "Fire Management Officer", 
"Firefighter", "Natural Resource Manager", "Other", "Prescribed Fire Burn Boss", 
"Ranger", "Technician", "A.A. degree", "Bachelors degree", "Doctorate", 
"High school diploma", "Masters degree", "Some college", "Some high school", 
"", "Alabama", "Arkansas", "Florida", "Georgia", "Kentucky", 
"Louisiana", "Mississippi", "North Carolina", "South Carolina", 
"Tennessee", "Texas", "Virginia", "West Virginia", "Cty Extension", 
"Cty Fire / Resource Mgmt", "Dept. of Env. Protection", "DoD", 
"NGO", "NPS", "Private, commercial", "Private, contractor", "Private, non-commerical", 
"State Fish and Wildlife ", "State Forest Service", "State Water Mgmt Dist", 
"USFS", "USFWS"), class = "factor"), Count = c(6L, 2L, 68L, 11L, 
165L, 72L, 59L, 16L, 14L, 39L, 181L, 12L, 11L, 100L, 28L, 0L, 
29L, 10L, 38L, 147L, 15L, 5L, 23L, 73L, 30L, 9L, 16L, 10L, 0L, 
NA, NA, 66L, 57L, 54L, 51L, 31L, 27L, 25L, 23L, 18L, 17L, 16L, 
15L, 10L, 8L, 2L), Percent = c(0.0145278450363196, 0.00484261501210654, 
0.164648910411622, 0.026634382566586, 0.399515738498789, 0.174334140435835, 
0.142857142857143, 0.0387409200968523, 0.0338983050847458, 0.105121293800539, 
0.487870619946092, 0.032345013477089, 0.0296495956873315, 0.269541778975741, 
0.0754716981132075, 0, 0.0716049382716049, 0.0246913580246914, 
0.0938271604938272, 0.362962962962963, 0.037037037037037, 0.0123456790123457, 
0.0567901234567901, 0.180246913580247, 0.0740740740740741, 0.0222222222222222, 
0.0395061728395062, 0.0246913580246914, 0, NA, NA, 0.157142857142857, 
0.135714285714286, 0.128571428571429, 0.121428571428571, 0.0738095238095238, 
0.0642857142857143, 0.0595238095238095, 0.0547619047619048, 0.0428571428571429, 
0.0404761904761905, 0.0380952380952381, 0.0357142857142857, 0.0238095238095238, 
0.019047619047619, 0.00476190476190476), Measurement = structure(c(3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Agency", 
"Education", "Job Title", "State"), class = "factor"), Grouping = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
"2"), class = "factor")), .Names = c("Variable", "Count", "Percent", 
"Measurement", "Grouping"), row.names = c(NA, 46L), class = "data.frame")

barchart((Percent*100)~Variable|Measurement, #Plot Percent / measured variable
         data = compareSamples,
         as.table=FALSE,
         scales=list(x="free", y="free", rot=45, cex=expandVar),
         ylab="Percent Response",
         ylim=c(0,50),
         drop.unused.levels = TRUE,
         main = "Demographic Information",
         par.settings=simpleTheme(col="grey"))

With that, I see the following plot.

enter image description here

As you can see, the plots are evenly distributed on 3/4 of the panels, but the barchart for Agency demonstrates irregular grouping.

I'd like to have the bars evenly distributed on all of them.

Subsetting the data to just the "Agency" specific information and plotting a barchart then does not yield the result.

agencySpecific <- subset(compareSamples, Measurement == "Agency")

barchart((Percent*100)~Variable, 
         data = agencySpecific,
         as.table=FALSE,
         scales=list(x="free", y="free", rot=45, cex=.7),
         ylab="Percent Response",
         ylim=c(0,50),
         drop.unused.levels = TRUE,
         main = "Demographic Information",
         par.settings=simpleTheme(col="grey"))

Thus, I'm at a loss. I recreated the dataframe with random values and was unable to reproduce the error.

danielg7
  • 1
  • 1
  • I produced random data for my results and ended up not being able to reproduce my error. This makes me think they the error I was seeing was related more to something spooking in my dataframes. – danielg7 Jul 15 '13 at 23:36
  • and that's precisely why it's a good exercise to try and produce a minimal self-contained reproducible example. Can you narrow it down to a subset of your data? – baptiste Jul 15 '13 at 23:40
  • Good point. Using a dput, I was able to reproduce the error (see above), but the error falls out when I subset the data. – danielg7 Jul 16 '13 at 00:18
  • if you take out the `scales=free` line, you'll notice that 3 panels have contiguous bars, but the one that's problematic doesn't. That's why it doesn't spread out with scales=free. Presumably you can change the variable from a factor to a character class to ignore the order. – baptiste Jul 16 '13 at 00:24
  • maybe [this can help](http://r.789695.n4.nabble.com/drop-unused-levels-in-lattice-dotplot-axis-td2336810.html) – baptiste Jul 16 '13 at 00:33

0 Answers0