How to plot a bloxplot in R with subsets

我只是一个虾纸丫 提交于 2019-12-11 10:38:33

问题


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.)

I have tried

boxplot(olympics$height[olympics$sex == "M" & olympics$sport %in% c("basketball", "football")])

but I keep getting errors saying that finite ylim values are needed. How would you get the correct boxplot?


回答1:


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)



来源:https://stackoverflow.com/questions/36409627/how-to-plot-a-bloxplot-in-r-with-subsets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!