如何在R中使用子集绘制bloxplot [英] How to plot a bloxplot in R with subsets
问题描述
我的数据集奥运会有4列:病历号,身高,运动和性别(女= F,男= M),每行对应一位运动员。
My data set "olympics" has 4 columns: case number, height, sport, and sex (female=F, male=M), and each row corresponds to an athlete.
我需要制作一个箱形图,比较男性篮球运动员和男性足球运动员之间的身高分布。 (两种运动都在一个地块上进行,但没有其他运动。)
I need to produce a box plot comparing the height distributions among the male basketball players and male football players. (Both sports on a single plot, but with no others.)
我已经尝试过
boxplot(olympics$height[olympics$sex == "M" & olympics$sport %in% c("basketball", "football")])
,但我不断收到错误消息,指出需要有限的 ylim
值。您将如何获得正确的箱线图?
but I keep getting errors saying that finite ylim
values are needed. How would you get the correct boxplot?
推荐答案
由于我找到了您的数据集并弄清了问题所在,因此将其重写。 。你有很多错字。 R区分大小写。运行此代码,它将产生您想要的箱线图。
Going to rewrite this since I found your data set and figured out what your issue was. You have a ton of typos. R is case sensitive. Run this code and it will produce the boxplots that you want.
library(VGAMdata)
data(oly12)
dat = oly12
dat = dat[dat$Sport %in% c("Basketball","Football"),]
dat$Sport = droplevels(dat$Sport)
dat = dat[dat$Sex == "M",]
boxplot(dat$Height ~ dat$Sport)
这篇关于如何在R中使用子集绘制bloxplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!