darh78 darh78 - 1 month ago 7
R Question

facets in ggplot placing wrong observations in variables plots

I'm trying to plot a graph using the Facets feature from ggplot2 from a simple data.frame extracted from the Lahman package.
Nevertheless, it's placing some observations in the wrong variable plot.
I've tried to use several configurations in the facet_grid arguments but all of them have wrong placement of the observations.

Here below the code to reproduce the plot.

library(Lahman)
library(tidyverse)
library(plotly)

TmsStd <- Teams

TmsStd <- TmsStd %>% select(yearID, lgID, teamID, divID, Rank, W, L, DivWin, WCWin, LgWin, WSWin, name, teamIDBR)

TmsStd$WLPctg <- TmsStd$W / (TmsStd$W + TmsStd$L)

TmsStd <- TmsStd %>% arrange(yearID, desc(WLPctg))

TmsStd$OvSeaRank <- ave(TmsStd$WLPctg, TmsStd$yearID, FUN = seq_along)

TmPostS <- TmsStd %>% filter(OvSeaRank <= 4 & WSWin == "Y" & yearID > 1970) %>% select(yearID, teamIDBR, W, L, WLPctg, OvSeaRank)

Best_Post <- ggplot(data = TmPostS, aes(x = yearID)) +
geom_bar() +
ggtitle("ABC") +
xlab("Year") + ylab("") +
facet_grid(OvSeaRank ~ .) +
theme_light()

Best_Post


facet_grid plot

There is only one observation per year.

table(TmPostS$yearID)

1971 1972 1973 1974 1975 1976 1977 1978 1979 1981 1982 1983 1984 1986 1988 1989 1990 1991 1992 1993 1995 1996
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1997 1998 1999 2002 2004 2005 2007 2009 2013 2015
1 1 1 1 1 1 1 1 1 1


So it must exist only one line per year independently of the "OvSeaRank" variable.

Any hint of what I could be doing wrong?

Thanks in advance.

Joe Joe
Answer

By default geom_bar will count the number of occurrences of each year (which is always 1) rather than the value. You need to change the default behaviour with stat="identity" so it uses the column value.

ggplot(TmPostS, aes(x = yearID, y=OvSeaRank)) + geom_bar(stat="identity") + 
ggtitle("ABC") + xlab("Year") + ylab("") + facet_grid(OvSeaRank ~ .) +
theme_light()

enter image description here

It's actually better without faceting, because you don't really have enough variables in the plot. Leaving out facet_grid(OvSeaRank ~ .) gives the following: enter image description here

Idea How about using geom_line and reversing the y-axis for rank?

ggplot(TmPostS, aes(x = yearID, y=OvSeaRank)) + geom_line() + geom_point() + 
scale_y_reverse() + ggtitle("ABC") + xlab("Year") + ylab("Rank of champion") + theme_light()

enter image description here