在 R 中重新排序箱线图和须线图

发布于 2024-12-11 01:40:49 字数 374 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的往事 2024-12-18 01:40:49

您可以使用 relevel 函数将引用排序为“简单”。我必须首先将 data.difficulty 转换为因子,因为它是字符。

data.difficulty<-relevel(as.factor(data.difficulty), ref="simple")
bwplot(data.time ~ data.difficulty, xlab="Difficulty", ylab="Time") 

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.

data.difficulty<-relevel(as.factor(data.difficulty), ref="simple")
bwplot(data.time ~ data.difficulty, xlab="Difficulty", ylab="Time") 
不再让梦枯萎 2024-12-18 01:40:49

我不知道将难度向量转换为因子是否违反了不更改数据的约束。其他函数很可能将其视为与原始字符向量相同的基础。

dfrm <- data.frame(time=data.time, difficulty=data.difficulty) 
 dfrm$difficulty <- factor(dfrm$difficulty, levels=c("simple", "hard") )
 bwplot(time ~ difficulty, data=dfrm, xlab="Difficulty", ylab="Time")

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.

dfrm <- data.frame(time=data.time, difficulty=data.difficulty) 
 dfrm$difficulty <- factor(dfrm$difficulty, levels=c("simple", "hard") )
 bwplot(time ~ difficulty, data=dfrm, xlab="Difficulty", ylab="Time")
不爱素颜 2024-12-18 01:40:49

遵循其他答案:这通过将数据组织为数据框来稍微打破您的规则(以便我们可以为 bwplot 动态转换它),但是如果你可以忍受,这可能是一种更好的组织分析的方法……当来自同一组的变量被打包到数据帧中而不是作为单独的对象放置在 R 中时,R 中的很多事情(例如模型预测)会变得更容易。工作区。只要正确使用 data= 参数,其余命令也会变得更易于阅读。

data.time = c(1, 4, 8, 9, 2, 4, 1)  
data.difficulty = c("hard", "simple", "simple", "hard", 
                  "simple", "simple", "hard")  
mydata <- data.frame(time=data.time,difficulty=data.difficulty)
library(lattice)  
bwplot(time ~ difficulty, xlab="Difficulty", ylab="Time",
   data = transform(mydata,
       difficulty=relevel(difficulty,"simple")))

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 for bwplot), 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 use data= arguments appropriately.

data.time = c(1, 4, 8, 9, 2, 4, 1)  
data.difficulty = c("hard", "simple", "simple", "hard", 
                  "simple", "simple", "hard")  
mydata <- data.frame(time=data.time,difficulty=data.difficulty)
library(lattice)  
bwplot(time ~ difficulty, xlab="Difficulty", ylab="Time",
   data = transform(mydata,
       difficulty=relevel(difficulty,"simple")))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文