编辑mlbtmp.txt
:在获取数据(并将其存储在名为 的文本文件中)和更多实验之后,完成改造,包括来自其他答案的信息:
selection <- read.table("mlbtmp.txt",skip=1)
names(selection) <- c("row","League","Division","Position","Team")
## arrange order/recode factors
selection <- transform(selection,
Team=factor(Team,levels=rev(levels(Team))),
Position=factor(Position,levels=c("P","I","C","O"),
labels=c("Pitching","Infield",
"Center","Outfield")))
facet_grid
我玩弄了, facet_wrap
, scales
,等的各种排列coord_flip
。有些按预期工作,有些则没有:
library(ggplot2)
p <- ggplot(data=selection, aes(x=Team, fill= Position)) +
geom_bar(position="stack")
p + facet_grid(.~Division,scales="free_x") + coord_flip() ## OK
## seems to fail with either "free_x" or "free_y"
p + facet_grid(Division~.,scales="free") + coord_flip()
## works but does not preserve 'count' axis:
p + facet_wrap(~Division,scales="free")
我最终使用facet_wrap(...,scales="free")
并用于ylim
约束轴。
p + facet_wrap(~Division,scales="free") + coord_flip() +
ylim(0,60) + opts(axis.text.y=theme_text(hjust=0))
原则上,可能有一种方法可以使用..density..
, ..ncount..
,..ndensity..
或由计算的其他统计信息之一stat_bin
而不是 default ..count..
,但我找不到有效的组合。
相反(当坚持 ggplot 的即时转换时,这通常是最好的解决方案)我自己重塑了数据:
## pull out Team identification within Division and League
stab <- unique(subset(selection,select=c(Team,Division,League)))
## compute proportions by team
s2 <- melt(ddply(selection,"Team",function(x) with(x,table(Position)/nrow(x))))
## fix names
s2 <- rename(s2,c(variable="Position",value="proportion"))
## merge Division/League info back to summarized data
s3 <- merge(s2,stab)
p2 <- ggplot(data=s3, aes(x=Team, fill= Position,y=proportion)) +
geom_bar(position="stack")+scale_y_continuous(formatter="percent")+
geom_hline(yintercept=0.5,linetype=3)+ facet_wrap(~Division,scales="free") +
opts(axis.text.y=theme_text(hjust=0))+coord_flip()
显然可以在这里完成更多的美化工作,但这应该会让你大部分时间都在那里......