【问题标题】:How to reorder a grouped bar plot by one category?如何按一个类别重新排序分组条形图?
【发布时间】:2021-09-01 14:58:54
【问题描述】:

首先,谢谢你看这个! :-)

我有一个分组条形图,我想按每个参与者的降序排列RmoinsU值!

但不知何故我失败了!你有什么想法吗?

剧情: Grouped bar plot

我的代码和示例数据:

library(tidyverse)
library(ggplot2)

CassieDfBuild.fn <- function(RmoinsU,RplusU) {
  RmoinsU.df <- data.frame(participants<-c(1:length(RmoinsU)),value<-RmoinsU,type<-"RmoinsU")
  colnames(RmoinsU.df) <- c("participants","value","type")
  
  RplusU.df <- data.frame(participants<-c(1:length(RplusU)),value<-RplusU,type<-"RplusU")
  colnames(RplusU.df) <- c("participants","value","type")
  
  graph.df <- bind_rows(RmoinsU.df,RplusU.df)
  
  graph.df$participants <- factor(graph.df$participants)
 
  return(graph.df)
}

#Barchart Adherence rate Local
RmoinsU.Local<-c(1, 0.868421052631579,  0.90625,    0.741935483870968,  0.625,  0.25,   0.823529411764706,  1,  0.75,   0.761904761904762,  0.838709677419355,  1,  0.451612903225806,  0.941176470588235,  0.9,    0.523809523809524,  0.564102564102564,  0.681818181818182,  0.421052631578947,  0.62,   0.636363636363636,  0.5,    0.944444444444444,  0.727272727272727,  0,  0.847826086956522,  0.3,    0,  0.35,   0.565217391304348,  0.479166666666667,  0,  0.866666666666667,  0.964285714285714,  0.333333333333333,  0.714285714285714,  0.764705882352941,  0.769230769230769,  0.9375, 1,  0.916666666666667,  0.625,  0.576923076923077,  0.78125,    0.8,    0.666666666666667,  0.666666666666667,  0.684210526315789,  0,  0.5,    0.846153846153846,  0.75,   0.166666666666667,  0.8,    0.695652173913043,  0.785714285714286,  0.545454545454545,  0.488372093023256,  0.705882352941177,  0.611111111111111,  0.6875, 0.346153846153846,  0)
RplusU.Local<-c(0.88,   0.789473684210526,  0.571428571428571,  0.829268292682927,  0.82258064516129,   0.891304347826087,  0.595744680851064,  0.626865671641791,  0.666666666666667,  1,  0.948717948717949,  0.813559322033898,  0.7,    0.970588235294118,  0.896551724137931,  0.414634146341463,  1,  0.946428571428571,  0.68,   0.914285714285714,  0.681818181818182,  1,  0.830188679245283,  0.696969696969697,  1,  0.928571428571429,  0.722222222222222,  1,  0.901960784313726,  0.117647058823529,  1,  0.986301369863014,  0.933333333333333,  0.477272727272727,  0.905660377358491,  1,  0.909090909090909,  0.82258064516129,   0.869565217391304,  1,  1,  0.972972972972973,  1,  0.941176470588235,  1,  0,  0.574074074074074,  0.868421052631579,  0.666666666666667,  1,  0.782608695652174,  0.876923076923077,  0.644444444444444,  0.836065573770492,  0.863636363636364,  0.85,   0.5,    0.95,   1,  1,  0.705882352941177,  0.772727272727273,  1)

test.list <- length(RmoinsU.Local)

graph.adherence.Local.df <- CassieDfBuild.fn(RmoinsU.Local,RplusU.Local)

# Et voici le plot
p <- ggplot(graph.adherence.Local.df, aes(fill=type, x=reorder(participants, -value), y=value)) +
  geom_bar(stat="identity", color="black", position=position_dodge())+
  theme_minimal() +
  scale_fill_grey() +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  geom_hline(yintercept = 0) +
  geom_hline(yintercept = 1) +
  ggtitle("Graph de Cassie 1") +
  labs(x = "Participants") +
  labs(y = "Adherence") +
  theme(legend.title=element_blank()) +
  theme(plot.title = element_text(face="bold")) +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position = c(0.075, 0.2)) +
  theme(
    # manipulation de la légende
    legend.background = element_rect(fill = "white"),
    legend.key = element_rect(color = "black"),
  )
print(p)

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我不确定这是否是您要查找的内容: 您可以使用以下代码重新调整您的因素participants

    helper <- levels(graph.adherence.Local.df$participants)
    helper1 <- as.character(sort(as.numeric(helper), decreasing = TRUE))
    levels(graph.adherence.Local.df$participants) <- helper1
    

    并更改ggplot

    x=reorder(participants, -value)

    x=factor(participants)

    【讨论】:

      【解决方案2】:

      问题在于,使用reorder,条形图根据RmoinsURplusU 的平均值重新排序。由于我经常遇到我只想按一个类别重新排序的情况,我给自己写了一个小函数reorder_where 来实现这一点。基本上,它允许按满足附加条件的值重新排序,例如在您的情况下,在条件 type == "RmoinsU" 为真的情况下按值重新排序:

      library(tidyverse)
      library(ggplot2)
      
      reorder_where <- function (x, by, where, fun = mean, ...) {
        xx <- x[where]
        byby <- by[where]
        byby <- tapply(byby, xx, FUN = fun, ...)[x]
        reorder(x, byby)
      }
      
      # Et voici le plot
      p <- ggplot(graph.adherence.Local.df, aes(fill=type, x=reorder_where(participants, -value, type == "RmoinsU"), y=value)) +
        geom_bar(stat="identity", color="black", position=position_dodge())+
        theme_minimal() +
        scale_fill_grey() +
        theme(panel.border = element_blank(), panel.grid.major = element_blank(),
              panel.grid.minor = element_blank()) +
        geom_hline(yintercept = 0) +
        geom_hline(yintercept = 1) +
        ggtitle("Graph de Cassie 1") +
        labs(x = "Participants") +
        labs(y = "Adherence") +
        theme(legend.title=element_blank()) +
        theme(plot.title = element_text(face="bold")) +
        theme(plot.title = element_text(hjust = 0.5)) +
        theme(legend.position = c(0.075, 0.2)) +
        theme(
          # manipulation de la légende
          legend.background = element_rect(fill = "white"),
          legend.key = element_rect(color = "black"),
        )
      print(p)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多