Reputation: 439
I've got the following code (using ggplot2) which outputs the table below:
ggplot(final,aes(x=final$Players,y=final$AvgPts,ymin=final$min,ymax=final$max)) +
geom_pointrange() + theme_bw() + coord_flip() +
geom_hline(yintercept = min(final$min)-1,linetype="dotted") + ylab("") +
xlab("") +geom_text(aes(label=final$AvgPts),hjust=0.5, vjust=-0.8, size=3) +
ggtitle(paste("2016 Moneyball Average Spreads for ",pos," averaging more than",points)) +
theme(plot.title = element_text(size=10, face="bold"))
What I want to be able to do is add a couple of columns to the right of the table, for example like in a forest plot, the two columns would be:
column1<-final$salary/final$AvgPts
column2<-final$sd
I would also like to be able to re-order the chart on either of these columns if this is possible.
Is there also a way to reorder the existing chart on final$AvgPts?
final is structured as follows:
Gms AvgPts max min sd Moneyball Team salary position playing.status Players
(dbl) (dbl) (dbl) (dbl) (dbl) (chr) (fctr) (dbl) (fctr) (fctr) (chr)
1 3 92.00 107 81 13.45 Adam Oxley COL 6100 MID Start Adam Oxley, COL, MID, 6100
2 4 107.00 130 88 20.30 Adam Treloar COL 8500 MID Start Adam Treloar, COL, MID, 8500
3 3 97.67 110 91 10.69 Bernie Vince MEL 7300 MID Not Named Bernie Vince, MEL, MID, 7300
4 4 91.00 119 59 28.66 Jack Viney MEL 6500 MID Start Jack Viney, MEL, MID, 6500
5 4 107.50 142 71 33.91 Jack Ziebell NM 7500 MID Start Jack Ziebell, NM, MID, 7500
6 4 101.25 130 68 25.53 Lachie Neale FRE 8500 MID Start Lachie Neale, FRE, MID, 8500
7 4 110.75 143 55 38.40 Nat Fyfe FRE 8400 MID Start Nat Fyfe, FRE, MID, 8400
8 4 102.00 114 86 11.78 Scott Pendlebury COL 9200 MID Start Scott Pendlebury, COL, MID, 9200
9 2 108.00 128 88 28.28 Steele Sidebottom COL 8500 MID Start Steele Sidebottom, COL, MID, 8500
10 4 104.75 114 95 8.14 Taylor Adams COL 8000 MID Start Taylor Adams, COL, MID, 8000
Upvotes: 0
Views: 2893
Reputation: 1237
To add other plots, there are two solutions:
ggplot2
, use the function facet_wrap
griExtra
: grid.arrange
can create multiple ggplots.On the specific code to reorder, the easiest way is to re-order the factor levels.
final$Players<-factor(final$Players, levels = unique(final$Players)[order_vector])
Furthermore, in aes ggplot
only the name of the column should be passed not the vector item. For example, you can rewrite:
ggplot(data = final,aes(x=Players,y=AvgPts,ymin=min,ymax=max))
I suppose you want to order on the mean of each player.
Mean<-sapply(unique(final$Players),function(z){
mean(final$AvgPoints[as.character(final$Players) == as.character(z)),]
}
)
final$Players<-factor(final$Players, levels = unique(final$Players)[order(Mean)])
library(gridExtra)
g1<-ggplot(final,aes(x=Players,AvgPts,ymin=min,ymax = max)) + ...
g2<-tableGrob(head(final[,c("salary",sd)]))
grid.arrange(g1,g2, ncol=1)
More information on ggplot
and gridExtra
can be found here : https://github.com/hadley/ggplot2/wiki/Mixing-ggplot2-graphs-with-other-graphical-output
Upvotes: 1