简体   繁体   English

如何在R中的多个箱图中更改x轴的顺序

[英]How to change the order of x-axis in multiple boxplots in R

I am trying to change the order x-axis in this boxplot. 我正在尝试更改此箱图中的x轴顺序。

[Now the order is loupe, microscope and video, and I want to change it to microscope, loupe then video] [现在的订单是放大镜,显微镜和视频,我想将其更改为显微镜,放大镜和视频]

The dataframe example is like this 数据框示例如下所示

 Label      Mental Physical Temporal Performance Effort Frustration sum
 Microscope  10     10      10       10     10      10    60
 Microscope  10     10      10       10     10      10    60
 Loupe       20     20      20       20     20      20    120 
 Loupe       20     20      20       20     20      20    120 
 Video       15     15      15       20     20      20    105 
 Video       15     15      15       20     20      20    105 

This is boxplot i have right now boxplot1 这是我现在拥有的boxplot boxplot1

This is my code for ggplot 这是我的ggplot代码

  mydata <- read.csv("boxplotyiyu2.csv",header=TRUE)
  dfm <- melt(mydata, id.var = "Label")
  ggplot(data = dfm, aes(x=variable, y=value)) + geom_boxplot(aes(fill=Label),width=0.5)+ xlab("Demand") + ylab("NASA-TLX Scores")

And I have tried this, but the result is not correct. 我已经尝试过了,但是结果不正确。

dfm$variable <- factor(dfm$variable,levels = c("Microscope","Loupe","Video"))

Another question is how to modify the y-axis for multiple boxplots. 另一个问题是如何修改多个箱形图的y轴。 I have this seven boxplots together, but i want to change the y-axis for each small plot. 我将这七个箱形图放在一起,但是我想为每个小图更改y轴。 boxplot2 boxplot2

(The dataframe is similar with above one, just replace mental,physical...with angle data) (数据框与上面的数据框相似,只是将角度,物理...替换为角度数据)

The code I have is 我的代码是

  df.m <- melt(mydata, id.var = "Label")
  p <- ggplot(data = df.m, aes(x=variable, y=value))
  p <- p + geom_boxplot(aes(fill=Label))
  p <- p + facet_wrap( ~ variable, scales="free")
  p <- p + xlab("Angle") + ylab("Degree")

Please do me a favor! 请帮我一个忙! Really appreciate it! 真的很感激!

You will need to redefine the order of the factors with the factor function. 您将需要使用factor函数重新定义因素的顺序。

#Sample data
Label<-c("Microscope", "Microscope", "Loupe", "Loupe", "Video", "Video")
mydata<-data.frame(Label)

#print out
levels(mydata$Label)

mydata$Label<-factor(mydata$Label, levels=c("Microscope", "Loupe",  "Video"))
#print out
levels(mydata$Label)

See the cookbook-r.com for more information: http://www.cookbook-r.com/Manipulating_data/Changing_the_order_of_levels_of_a_factor/ 更多信息请参见该cookbook-r.com: http://www.cookbook-r.com/Manipulating_data/Changing_the_order_of_levels_of_a_factor/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM