根据颜色绘制棒球球场作为定性变量

时间:2011-04-19 17:44:01

标签: r ggplot2 data-visualization

我正在考虑在R中这样做,但我是新手,并希望得到任何帮助

我有一个由...识别的棒球场的数据集(球场) 'pitchNumber'和'results',例如S =摆动打击,B =球,H =打击 等。

e.g。     1 B;     2 H;     3 S;     4 S;     5 X;     6 H;等

我想要做的就是有一个图表,将它们绘制成一行,参见BHSSXB 但是用一个小的条形代替字母,用一个字母代表字母,用一个图例,并且可选地将音高数字放在颜色之上。有点像迷你吧。

有关如何实施此建议的任何建议

3 个答案:

答案 0 :(得分:4)

使用ggplot的相同图表。

数据由@GavinSimpson提供。

ggplot(baseball, aes(x=pitchNumber, y=1, ymin=0, ymax=1, colour=outcome)) + 
    geom_point() + 
    geom_linerange() +
    ylab(NULL) +
    xlab(NULL) + 
    scale_y_continuous(breaks=c(0, 1)) +
    opts(
        panel.background=theme_blank(),
        panel.grid.minor=theme_blank(),
        axis.text.y = theme_blank()
    )

enter image description here

答案 1 :(得分:3)

这是一个可以使用的基本图形理念。首先是一些虚拟数据:

set.seed(1)
baseball <- data.frame(pitchNumber = seq_len(50),
                       outcome = factor(sample(c("B","H","S","S","X","H"), 
                                               50, replace = TRUE)))
> head(baseball)
  pitchNumber outcome
1           1       H
2           2       S
3           3       S
4           4       H
5           5       H
6           6       H

接下来我们定义我们想要的颜色:

## better colours - like ggplot for the cool kids
##cols <- c("red","green","blue","yellow")
cols <- head(hcl(seq(from = 0, to = 360,
                    length.out = nlevels(with(baseball, outcome)) + 1), 
                 l = 65, c = 100), -1)

然后将pitchNumber绘制为高度1 直方图条(type = "h"),抑制法线轴,我们将点添加到条形图的顶部帮助可视化:

with(baseball, plot(pitchNumber, y = rep(1, length(pitchNumber)), type = "h", 
                    ylim = c(0, 1.2), col = cols[outcome],
                    ylab = "", xlab = "Pitch", axes = FALSE, lwd = 2))
with(baseball, points(pitchNumber, y = rep(1, length(pitchNumber)), pch = 16, 
                      col = cols[outcome]))

在x轴和绘图框上添加一个图例:

axis(side = 1)
box()
## note: this assumes that the levels are in alphabetical order B,H,S,X...
legend("topleft", legend = c("Ball","Hit","Swinging Strike","X??"), lty = 1,
       pch = 16, col = cols, bty = "n", ncol = 2, lwd = 2)

给出这个:

baseball sparkline

答案 2 :(得分:3)

这是对你对@ Gavin答案的最后评论的回应。我要建立@Gavin提供的数据和@Andrie的ggplot2情节。 ggplot()支持变量或变量的分面概念。在这里你想要投手以及每排50的节距限制。我们将创建一个新变量,它对应于我们想要分别绘制的每一行。基本图形中的等效代码需要调整mfrow中的mfcolpar(),并为每组数据调用单独的图。

#150 pitches represents a somewhat typical 9 inning game. 
#Thanks to Gavin for sample data.
longGame <- rbind(baseball, baseball, baseball)
#Starter goes 95 pitches, middle relief throws 35, closer comes in for 20 and the glory
longGame$pitcher <- c(rep("S", 95), rep("M", 35), rep("C",20))
#Adjust pitchNumber accordingly
longGame$pitchNumber <- c(1:95, 1:35, 1:20)
#We want to show 50 pitches at a time, so will combine the pitcher name 
#with which set of pitches this is
longGame$facet <- with(longGame, paste(pitcher, ceiling(pitchNumber / 50), sep = ""))
#Create the x-axis in increments of 1-50, by pitcher
longGame <- ddply(longGame, "facet", transform, pitchFacet = rep(1:50, 5)[1:length(facet)])
#Convert facet to factor in the right order
longGame$facet <- factor(longGame$facet, levels = c("S1", "S2", "M1", "C1"))

#Thanks to Andrie for ggplot2 function. I change the x-axis and add a facet_wrap
ggplot(longGame, aes(x=pitchFacet, y=1, ymin=0, ymax=1, colour=outcome)) + 
    geom_point() + 
    geom_linerange() +
    facet_wrap(~facet, ncol = 1) +
    ylab(NULL) +
    xlab(NULL) + 
    scale_y_continuous(breaks=c(0, 1)) +
    opts(
        panel.background=theme_blank(),
        panel.grid.minor=theme_blank(),
        axis.text.y = theme_blank()
    )

您显然可以更改facet变量的标签,但上面的代码将生成:

enter image description here