ggplot2

时间:2015-04-25 19:08:41

标签: r plot ggplot2 facet

我遇到了这个问题:我想构建一个具有分面功能的堆积条形图,因此我可以比较两个不同对象中五个常见类别的频率分布,这三个分类按照三个组分开。我有六个对象,五个类别和三个组。问题是每个组只有两个不同的和独占的对象要绘制,但到目前为止我只能生成一个图,其中六个对象在三个组中绘制。这不是最佳的,因为对于每个组,我有四个没有数据的对象。

是否可以使用刻面功能为每个组仅绘制两个对象?

EDITED

这是我的数据:

structure(list(Face = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
5L, 5L, 6L, 6L, 6L, 6L, 6L), .Label = c("LGH002", "LGH003", "LGM009", 
"SCM018", "VAH022", "VAM028"), class = "factor"), Race = structure(c(1L, 
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), 
.Label = c("1. Amerindian", "2. White", "3. Mestizo", "4. Other races", 
"5. Cannot tell"), class = "factor"), Count = c(19L, 0L, 13L, 8L, 0L, 2L, 
7L, 23L, 6L, 2L, 1L, 1L, 29L, 6L, 3L, 29L, 0L, 11L, 0L, 0L, 0L, 38L, 1L, 0L,
1L, 0L, 30L, 9L, 0L, 1L), Density = c(0.475, 0, 0.325, 0.2, 0, 
0.05, 0.175, 0.575, 0.15, 0.05, 0.025, 0.025, 0.725, 0.15, 
0.075, 0.725, 0, 0.275, 0, 0, 0, 0.95, 0.025, 0, 0.025, 0, 
0.75, 0.225, 0, 0.025), School = structure(c(1L, 1L, 1L, 
1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Municipal", 
"Private Fee-Paying", "Private-Voucher"), class = "factor")), 
.Names =c("Face", "Race", "Count", "Density", "School"), 
class = "data.frame", row.names = c(NA, -30L))

这是我用来构建情节的代码:

P <- ggplot(data = races.df, aes(x = Face, y = Density, fill = Race)) +
geom_bar(stat="identity") +
scale_y_continuous(labels=percent)
P + facet_grid(School ~ ., scales="free") + coord_flip()

你可以想象,我只想在“市政”中看到x值“SCM018”和“LGH002”; “私人代金券”中的“LGM009”和“LGH003”;和“私人付费”中的“VAH022”和“VAM028”(每组只有两个对象)。可能吗?有什么帮助吗?

一切顺利,

毛。

0 个答案:

没有答案