Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2-按颜色排序条_R_Ggplot2 - Fatal编程技术网

R ggplot2-按颜色排序条

R ggplot2-按颜色排序条,r,ggplot2,R,Ggplot2,这是我现在的条形图: 相应的代码如下所示: ggplot(data, aes(x = ID, y = f0.mean, fill = Entscheidung )) + geom_bar(stat = "identity") 我希望按填充属性对条进行排序(例如,所有红色条应在左侧结束,所有绿色条应在右侧结束) 有什么方法可以做到这一点吗?解决方案是对ID因子的级别进行重新排序 # Generate data for the example set.seed(2345) data <

这是我现在的条形图:

相应的代码如下所示:

ggplot(data, aes(x = ID, y = f0.mean, fill = Entscheidung )) +
  geom_bar(stat = "identity")
我希望按填充属性对条进行排序(例如,所有红色条应在左侧结束,所有绿色条应在右侧结束)


有什么方法可以做到这一点吗?

解决方案是对
ID
因子的级别进行重新排序

# Generate data for the example
set.seed(2345)
data <- data.frame(f0.mean=rnorm(50)*20+300, ID = paste0("ID",1:50),
   Entscheidung = factor(sample(1:3,50, replace=T),labels=c("Chance","Tor","Unentschieden")))  


# Reorder ID grouping bars by Entscheidung
idx <- order(data$Entscheidung, data$ID)
data$ID <- factor(data$ID, levels=data$ID[idx])

ggplot(data, aes(x = ID, y = f0.mean, fill = Entscheidung)) + 
   geom_bar(stat = "identity") +
   scale_fill_manual(values = c("#F8766D", "#619CFF", "#00BA38"))
#为示例生成数据
种子集(2345)

数据查看这是否有助于ggplot(数据,aes(x=ID,y=f0.mean,fill=Entscheidung,order=Entscheidung))+geom_bar(stat=“identity”)
。感谢您的快速回答。我已经试过了,但不起作用。我听说order命令在当前版本的ggplot2下不再有效。您可以共享数据示例吗?