如何在R中的多个箱形图中更改x轴的顺序 [英] How to change the order of x-axis in multiple boxplots in R

查看:4260
本文介绍了如何在R中的多个箱形图中更改x轴的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变这个boxplot的订单x轴。



[现在订单是放大镜,显微镜和视频,我想将其更改为显微镜,放大镜,然后视频]



数据框示例就像这样

 标签心理物理时间表现努力挫折总和
显微镜10 10 10 10 10 10 60
显微镜10 10 10 10 10 10 60
放大镜20 20 20 20 20 120 120
放大镜20 20 20 20 20 20 120
视频15 15 15 20 20 20 105
视频15 15 15 20 20 20 105

这是我现在拥有的boxplot
boxplot1



这是我的代码ggplot

  mydata <  -  read.csv(boxplotyi (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分数)

我试过这个,但结果不正确。

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

另一个问题是如何修改多个箱形图的y轴。
我有这七个箱形图,但我想改变每个小图的y轴。
boxplot2



< (数据框与上面的数据框相似,只是用角度数据代替智力,物理......)

我有的代码是

  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)

帮我个忙!真的很感谢!

解决方案

您需要重新定义因子< code $>

#示例数据
标签< -c(Microscope,Microscope ,放大镜,放大镜,视频,视频)
mydata <-data.frame(标签)

#打印出
等级(mydata $标签)

mydata $标签< -factor(mydata $ Label,levels = c(Microscope,Loupe,Video))
#打印
等级( mydata $ Label)

查看cookbook-r.com了解更多信息: http://www.cookbook-r.com/Manipulating_data/Changing_the_order_of_levels_of_a_factor/


I am trying to change the order x-axis in this boxplot.

[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

This is my code for 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. I have this seven boxplots together, but i want to change the y-axis for each small plot. 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.

#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/

这篇关于如何在R中的多个箱形图中更改x轴的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆