[英]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. 我正在尝试使用ggplot2中的Facets功能从拉曼程序包提取的简单data.frame中绘制图形。 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.
我尝试在facet_grid参数中使用几种配置,但是所有这些配置都有错误的观察值位置。
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. 因此,与“ OvSeaRank”变量无关,每年仅必须存在一行。
Any hint of what I could be doing wrong? 有什么暗示我可能做错了吗?
Thanks in advance. 提前致谢。
By default geom_bar
will count the number of occurrences of each year (which is always 1) rather than the value. 默认情况下,
geom_bar
将计算每年发生的次数(始终为1)而不是数值。 You need to change the default behaviour with stat="identity"
so it uses the column value. 您需要使用
stat="identity"
更改默认行为,以便它使用列值。
ggplot(TmPostS, aes(x = yearID, y=OvSeaRank)) + geom_bar(stat="identity") +
ggtitle("ABC") + xlab("Year") + ylab("") + facet_grid(OvSeaRank ~ .) +
theme_light()
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: 省略
facet_grid(OvSeaRank ~ .)
会得到以下结果:
Idea How about using geom_line
and reversing the y-axis for rank? 想法如何使用
geom_line
并反转y轴的排名?
ggplot(TmPostS, aes(x = yearID, y=OvSeaRank)) + geom_line() + geom_point() +
scale_y_reverse() + ggtitle("ABC") + xlab("Year") + ylab("Rank of champion") + theme_light()
Thanks to Joe support, I could found what I wanted to show on this question. 多亏了Joe的支持,我才能找到我想在这个问题上展示的内容。 I was modifying
stat = "identity"
by stat = "bin"
and defining a bindwidth = 1
我正在通过
stat = "bin"
修改stat = "identity"
stat = "bin"
并定义bindwidth = 1
ggplot(TmPostS, aes(x = yearID)) + geom_bar(stat="bin", binwidth = 1, color = "red", fill = "darkblue") +
ggtitle("World Series Champions based on their regular season W-L% overall rank") + xlab("Season") + ylab("") + facet_grid(OvSeaRank ~ .) +
theme_bw() +
theme(axis.text.y=element_blank(),
axis.ticks = element_blank())
Wished graph using facets 使用构面的图
On this case now the data frame considers all the MLB champions since 1884. 现在,在这种情况下,数据框考虑自1884年以来的所有MLB冠军。
Finally, using geom_line idea from Joe: 最后,使用乔的geom_line想法:
ggplot(TmPostS, aes(x = yearID, y=OvSeaRank)) + geom_line(colour = "darkblue") + geom_point(colour = "red") +
scale_y_reverse() + ggtitle("World Series Champions based on their regular season W-L% overall rank") + xlab("Year") + ylab("Rank of champion") + theme_light()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.