【问题标题】:How to change the order of x-axis in multiple boxplots in R如何在R中的多个箱线图中更改x轴的顺序
【发布时间】:2017-02-14 11:06:07
【问题描述】:

我正在尝试更改此箱线图中的 x 轴顺序。

[现在的顺序是放大镜、显微镜和视频,我想把它改为显微镜、放大镜然后视频]

数据框示例是这样的

 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 

这是我现在拥有的箱线图 boxplot1

这是我的 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")

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

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")

请帮我一个忙!真的很感激!

【问题讨论】:

  • 必须给出一个示例数据框。同时这可能会有所帮助cookbook-r.com/Graphs。第 3、6、7 和 9 部分将让您接近您想要的。
  • 刚刚添加了示例数据框,感谢您的回复

标签: r graph ggplot2 boxplot


【解决方案1】:

您需要使用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)

更多信息请参见 cookbook-r.com:http://www.cookbook-r.com/Manipulating_data/Changing_the_order_of_levels_of_a_factor/

【讨论】:

  • 感谢您的回复!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 1970-01-01
  • 1970-01-01
  • 2020-02-04
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
相关资源
最近更新 更多