我需要在我的盒子和胡须图中使用drop.levels函数。我使用的数据是Dance,数据帧是dance$new和dance$type,我想要包含的变量是Contra、Blues和Swing。还有另外3个我不想包含的变量: Lindy、Salsa和Tango。
这就是我所拥有的:
box.labels<-c("Blues","Contra","Swing")
boxplot(dance$new~dance$type, ylab="Dance Count",
xlab="Type", name=box.labels, drop.levels(Lindy, Salsa, Tango),
main="Dancing for a Healthier You")我合并drop.levels是不是错了?
发布于 2015-11-25 01:06:57
猜测一下,你可能想要
box.labels<-c("Blues","Contra","Swing")
boxplot(new~type, data=droplevels(subset(dance,type %in% box.labels)),
ylab="Dance Count",
xlab="Type", name=box.labels,
main="Dancing for a Healthier You")这里的要点是:
drop.levels和droplevels都用于从数据集中删除未使用的级别。上面的subset()命令更有可能做你想做的事情,droplevels(),而不是gdata::drop.levels。在base R中,droplevels()已经有几年的历史了;gdata::drop.levels的历史可以追溯到它出现之前)。new~type,data=...而不是dance$new~dance$type;在可能的情况下使用data参数通常更具可读性和健壮性。您甚至可能不需要droplevels();boxplot()默认情况下可能会忽略未使用的级别。name参数可能也是多余的。
发布于 2015-11-27 05:35:04
谢谢你的帮助。你的建议帮我弄明白了。
我最终绘制了子集数据,这让我避免了drop levels函数。下面是我使用的代码:
dancenew<-subset(Dance, Type=="Lindy" | Type== "Blues" | Type=="Contra")
box.labels<-c("Lindy","Blues","Contra")
boxplot(dancenew$Count~dancenew$Type, ylab="Dance Count", data=ausportnew, xlab="Type", name=box.labels, main="Dancing for a healthier you")https://stackoverflow.com/questions/33897303
复制相似问题