【问题标题】:Reordering in a group bar chart using ggplot2 package [duplicate]使用ggplot2包在组条形图中重新排序[重复]
【发布时间】:2020-01-03 08:05:03
【问题描述】:

我使用来自我创建的数据框的数据创建了一个组条形图,以获取相对于基线的百分比变化。在obj变量的每个组中,有 5%、10%、15% 和 20% 的数据。如何对条形进行排序,使其按该顺序排列,而不是按现在的 10%、15%、20%、5% 的顺序排列?

maxprice_Pchange<-(kharif_fv_c_allocation_val[c(6,11,16,21,
                                                7,12,17,22,
                                                8,13,18,23,
                                                9,14,19,24,
                                                10,15,20,25),1] - 181184567630)/181184567630*100
dev_constraint<-rep(c("5%","10%","15%","20%"),5)
obj<-c(rep("Max kharif price",4), rep("Min BWF",4),rep("Min TWF",4),rep("Max b-carotene",4),rep("Max Iron",4))
maxprice_Pchang_df<-data.frame(maxprice_Pchange,dev_constraint,obj)

ggplot(maxprice_Pchang_df, aes(fill=dev_constraint,x= obj, y= maxprice_Pchange)) + 
  geom_bar(position="dodge", stat="identity")+ scale_x_discrete(limits = c("Max kharif price", "Min BWF",
                                                                          "Min TWF", "Max b-carotene","Max Iron"))

Grouped bar chart

【问题讨论】:

    标签: r ggplot2 bar-chart


    【解决方案1】:

    您可以通过以下方式在ggplot 之外对其进行重新排序:

    library(ggplot2)
    maxprice_Pchang_df$dev_constraint <- factor(maxprice_Pchang_df$dev_constraint, levels = c("5%","10%","15%","20%"))
    ggplot(maxprice_Pchang_df, aes(fill=dev_constraint,x= obj, y= maxprice_Pchange)) + 
      geom_bar(position="dodge", stat="identity")+ 
      scale_x_discrete(limits = c("Max kharif price", "Min BWF", "Min TWF", "Max b-carotene","Max Iron"))
    

    【讨论】:

    猜你喜欢
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    相关资源
    最近更新 更多