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
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.
stat="identity"
. Will add the answer now. – Joe