自定义多面barplot的美学 [英] Customizing aesthetics of faceted barplot

查看:235
本文介绍了自定义多面barplot的美学的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图对R&B

 选择<  -  draft [ c(团队,部门,职位)] 
团长(选拔)

团队部门职位
1坑NL中部P
2海AL West P
3 ari NL West P
4 bal AL东部P
5 kc AL中央O
6是东部非洲东部I

其中P = Pitcher,O = Outfield等。

我想显示玩家人数在每个部门内按位置选择

  p < -  ggplot(data = selection,aes(x = Team,fill =位置))+ geom_bar(position =stack)
p <-p + coord_flip()
p <-p + ylab(Players Selected)
p < - p + facet_wrap 〜部门)
p

这让我成为那里的一部分,但是非常没有吸引力

a)分组工作,但所有团队都显示在每个分区g中即使只有每个部门的5或6个球队实际 - 并且正确 - 显示数据

b)通过协同翻转,球队被反向列出字母顺序下页。我可以度假吗?这也将是很好的左对齐



c)我如何将传奇设置为Pitching,Outfield而不是P和O - 这是一个我不知何故需要的矢量设置和包括

d)看到每个球员选择对每种类型球员的比例也很有趣。这是通过设置position =fill来完成的。我可以设置坐标轴为%,而不是0到1.我还尝试设置geom_vline(aes(xintercept = 0.5)和yintercept以防止翻转因子在 -
中,但行并未出现在中途标记处x轴

非常感谢

解决方案

编辑:在抓取数据(并将其存储在名为 mlbtmp.txt 的文本文件中)和其他更多实验之后,完整的更新包括来自其他答案的信息:

 选择<  -  read.table(mlbtmp.txt,skip = 1)
名称(选择)< ; - c(row,League,Division,Position,Team)
##排列顺序/重新编码因子
选择< - 变换(选择,
团队=因素(团队,等级= rev(等级(团队))),
职位=因素(职位,等级= c(P,我,C,O),
labels = c(Pitching,I​​nfield,
Center,Outfield)))

我玩过各种各样的 facet_grid facet_wrap 比例 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

##似乎失败,无论是free_x还是free_y
p + facet_grid(Division〜。,scales =free)+ coord_flip()

##都可以工作,但不会保留'count'轴:
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 而不是默认的 .. count .. 计算的其他统计信息,但是我找不到组合这些工作。



取而代之的是(当通过ggplot的即时转换时常常是最好的解决方案)我自己重新设计了数据:

  ##拉出分区和联盟内的球队识别
stab < - 独特(子集(选择,选择= c(团队,分部,联盟)))
##按团队计算比例
s2 < - 熔化(ddply(选择,团队,函数(x)with(x,table(Position)/ nrow(x))) )
##修复名称
s2 < - 重命名(s2,c(变量=位置,值=比例))
##合并赛区/联赛信息数据
s3 < - 合并(s2,stab)

p2 < - ggplot(data = s3,aes(x = Team,fill = Position,y = propo rtion))+
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()



显然有这可以在这里完成,但这应该会让你在那里找到最好的方式......


I am trying to do some analysis of the recent MLB draft with some ggplots in R

selection <- draft[c("Team","Division","Position")]
head(selection)

  Team   Division Position
1  pit NL Central        P
2  sea AL West           P
3  ari NL West           P
4  bal AL East           P
5  kc  AL Central        O
6  was NL East           I

where P = Pitcher , O=Outfield etc.

I want to show the number of players selected by team by position within each division

p <- ggplot(data=selection, aes(x=Team, fill= Position))  + geom_bar(position="stack")
p <-  p + coord_flip()
p <- p+ ylab("Players Selected")
p <- p + facet_wrap(~Division)
p

This gets me part of the way there but is very unattractive

a) the groupings work but all teams are shown in each divison grid - even though only the 5 or 6 team in each division actually - and correctly - show data

b) With the co-ord flip, the teams are listed in reverse alphabetical order down page. can I resort. It would also be nice to have left justification

c) How do i set the legend to Pitching, Outfield rather than P and O - is this a vector i somehow need to set and include

d) It would also be interesting to see the proportion of each teams selection committed to each type of player. This is accomplished by setting position= "fill". Can i set the axes to % rather than 0 to 1. I also tried setting a geom_vline(aes(xintercept=0.5) -and yintercept in case the flip factored in - but the line did not appear at halfway mark along the x axis

Help much appreciated

解决方案

edit: complete revamping, including info from other answer, after grabbing the data (and storing it in a text file called mlbtmp.txt) and some more experimentation:

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

I played around with various permutations of facet_grid, facet_wrap, scales, coord_flip, etc.. Some worked as expected, some didn't:

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

I ended up with facet_wrap(...,scales="free") and used ylim to constrain the axes.

p + facet_wrap(~Division,scales="free") + coord_flip() +
  ylim(0,60) + opts(axis.text.y=theme_text(hjust=0))

In principle there might be a way to use ..density.., ..ncount.., ..ndensity.., or one of the other statistics computed by stat_bin instead of the default ..count.., but I couldn't find a combination that worked.

Instead (as is often the best solution when stuck with ggplot's on-the-fly transformations) I reshaped the data myself:

## 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()

There's obviously a little more prettying-up that could be done here, but this should get you most of the way there ...

这篇关于自定义多面barplot的美学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆