格子里的条形图“聚集”

时间:2013-07-15 22:26:45

标签: r data-visualization lattice

我对来自barchart包中的lattice的一些不规则的绘图感到有些困惑。具体来说,我正在尝试为一些人口统计信息做一个频率数据的多层条形图。

以下代码应该正确加载我的数据,并重现(希望)我看到的问题......

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"))

有了这个,我看到以下plot

enter image description here

如您所见,图表均匀分布在3/4的面板上,但代理商的条形图显示了不规则的分组。

我想让所有这些酒吧均匀分布。

将数据子集化为“代理”特定信息并绘制条形图然后不会产生结果。

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"))

因此,我很茫然。我使用随机值重新创建了数据框,但无法重现错误。

0 个答案:

没有答案