R 使用ggplot2对箱线图进行分组和重新排序

R 使用ggplot2对箱线图进行分组和重新排序,r,ggplot2,boxplot,R,Ggplot2,Boxplot,我需要将箱线图(子组)分组,并根据子组所属的组对它们(组)重新排序 我使用的R脚本是: data<-read.delim("clipboard") p <- ggplot(data, aes(Class2,cM)) p <- p + geom_boxplot(aes(fill = factor(Class1))) + geom_jitter(alpha = 0.4, position = position_jitter(height = .01, width =

我需要将箱线图(子组)分组,并根据子组所属的组对它们(组)重新排序

我使用的R脚本是:

data<-read.delim("clipboard")

p <- ggplot(data, aes(Class2,cM))

p <- p + geom_boxplot(aes(fill = factor(Class1))) +
     geom_jitter(alpha = 0.4, position = position_jitter(height = .01, width = .35)) +
     coord_flip()

数据请以文本格式提供数据,而不是xls格式

解决方案1:将Class2转换为因子,并按要求的顺序设置级别

data$Class2 <- factor(
    data$Class2, 
    levels = c("group1:b", "group1:c", "group2:a")
)

最好转移到堆栈溢出?
ggplot(data, aes(x= Class2, y = cM)) + geom_boxplot() + 
  geom_jitter(alpha = 0.4, position = position_jitter(height = .01, width = .35)) +
  coord_flip() + 
  facet_wrap(~Class1)
ggplot(data, aes(x= Class2, y = cM)) + geom_boxplot() + 
  geom_jitter(alpha = 0.4, position = position_jitter(height = .01, width = .35)) +
  coord_flip() + 
  facet_wrap(~Class1, scales = "free_x")