I have sports dataset which shows a team's result, win draw or loss, cumulative games played and league standing. A simple plot of position by games played is produced thus
df<- data.frame(played=c(1:5),
result=c("W","L","D","D","L"),
position=c(1,3,4,4,5))
ggplot() +
geom_line(data=df,aes(x=played,y=position)) +
scale_y_reverse()
I would like to add a rug on the x axis with a different colour for each result, say W
is green, L
red and D
, blue but cannot seem to solve it using geom_rug
or adding a geom_bar
.
This should do the trick:
##The data frame df is now inherited by
##the other geom's
ggplot(data=df,aes(x=played,y=position)) +
geom_line() +
scale_y_reverse() +
geom_rug(sides="b", aes(colour=result))
In the geom_rug
function, we specify that we only want a rug on the bottom and that we should colour the lines conditional on the result. To change the colours, look at the scale_colour_*
functions. For your particular colours, try:
+ scale_colour_manual(values=c("blue","red", "green"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With