我不是 100% 确定,这是你想要的吗?
library(ggplot2)
library(scales)
ggplot( mtcars[ mtcars$vs==1,], aes(factor(vs) )) +
geom_bar(aes(y= ..count../ sum(..count..) )) +
facet_grid(. ~ am,) +
scale_y_continuous(labels = percent_format())
编辑:要删除一个级别,但仍然有相对于所有数字的百分比,我会先汇总数据,然后将其传递给ggplot
. 好处:
1) 速度。ggplot
内部函数可能会比任何其他拆分应用组合方法慢。如果您决定使用它们,它们会慢 100% data.table
(强烈推荐)
2) 易于使用。在允许您使用更多工具重塑和转换数据之前处理数据,然后将所有行数据推送到ggplot
所以,
library(ggplot2)
library(scales)
library(plyr)
df1 <- ddply(mtcars, .(am, vs), summarise,
percentage = length(vs[vs==1])/nrow(mtcars)) # percentages calculated
#respected the whole table. nrow(mtcars) can be changed with a subset of it in case
#of NAs not to be used taken into account for example.
> df1
am vs percentage
1 0 0 0.00000
2 0 1 0.21875
3 1 0 0.00000
4 1 1 0.21875
ggplot( df1[ df1$vs==1, ], aes(factor(vs), percentage )) +
geom_bar(sta="identity") +
facet_grid(. ~ am,) +
scale_y_continuous(labels = percent_format())
df2 <- ddply(mtcars, .(am, vs), summarise,
percentage = length(vs))
如果您需要稍微不同的东西,例如在每个am
级别内计算的百分比,那么:
df2 <- ddply(mtcars, .(am, vs), summarise,
percentage = length(vs))
df2$tot <- ave(df2$percentage, df2$am, FUN=sum)
df2$percentage <- df2$percentage / df2$tot
df2$tot <- NULL
> df2
am vs percentage
1 0 0 0.6315789
2 0 1 0.3684211
3 1 0 0.4615385
4 1 1 0.5384615
ggplot( df2[ df2$vs==1, ], aes(factor(vs), percentage )) +
geom_bar(sta="identity") +
facet_grid(. ~ am,) +
scale_y_continuous(labels = percent_format())