在 R 中重新排序箱线图和须线图
我在 R 中有以下最小示例代码:
data.time = c(1, 4, 8, 9, 2, 4, 1)
data.difficulty = c("hard", "simple", "simple", "hard", "simple", "simple", "hard")
library(lattice)
bwplot(data.time ~ data.difficulty, xlab="Difficulty", ylab="Time")
如果将其绘制成图表,您可以看到“困难”位于左侧,“简单”位于右侧。我希望情况相反。有没有办法在不转换或修改我的原始数据的情况下做到这一点?我不想更改它,因为数据稍后将用于其他图表和分析。
谢谢
I have the following minimal example code in R:
data.time = c(1, 4, 8, 9, 2, 4, 1)
data.difficulty = c("hard", "simple", "simple", "hard", "simple", "simple", "hard")
library(lattice)
bwplot(data.time ~ data.difficulty, xlab="Difficulty", ylab="Time")
If you graph it, you can see "hard" is on the left and "simple" is on the right. I want that reversed. Is there a way to do this without transforming or modifying my original data? I don't want to change it because the data is used later on for other graphs and analysis.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 relevel 函数将引用排序为“简单”。我必须首先将 data.difficulty 转换为因子,因为它是字符。
You could use the relevel function to order the reference to to be "simple". I had to convert the data.difficulty to factor first because it's character.
我不知道将难度向量转换为因子是否违反了不更改数据的约束。其他函数很可能将其视为与原始字符向量相同的基础。
I don't know if converting the difficulty vector to a factor violates your constraint about not changing the data. Ever other function will most likely treat this on the same basis as the original character vector.
遵循其他答案:这通过将数据组织为数据框来稍微打破您的规则(以便我们可以为
bwplot
动态转换它),但是如果你可以忍受,这可能是一种更好的组织分析的方法……当来自同一组的变量被打包到数据帧中而不是作为单独的对象放置在 R 中时,R 中的很多事情(例如模型预测)会变得更容易。工作区。只要正确使用data=
参数,其余命令也会变得更易于阅读。Following the other answers: this breaks your rules a little bit by organizing the data as a data frame (so that we can
transform
it on the fly forbwplot
), but if you can stand it, it's probably a better way to organize your analyses in general ... lots of things in R (e.g. model prediction) get easier when variables from the same set are packed into data frames rather than lying around as individual objects in the workspace. The rest of your commands get easier to read, too, as long as you usedata=
arguments appropriately.