2

First of all, thanks to take a look to this ! :-)

I have a grouped bar plot which I would like to order in decreasing order of each participants RmoinsU value !

But somehow I fail! Do you have an idea ?

The Plot: Grouped bar plot

My Code and example data:

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)
stefan
  • 90,330
  • 6
  • 25
  • 51
ehlo69
  • 75
  • 2

2 Answers2

0

I am not sure if this is what you are looking for: You could relevel your factor participants with this code:

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

and change in ggplot:

x=reorder(participants, -value)

to

x=factor(participants)

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
0

The issue is that using reorder the bars reordered according to the mean of RmoinsU and RplusU. As I quite often encounter the case where I want to reorder by only one category I wrote myself a small function reorder_where to achieve this. Basically it allows to reorder by a value where an additional condition is fulfilled, e.g. in your case reorder by value where the condition type == "RmoinsU" is true:

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)

stefan
  • 90,330
  • 6
  • 25
  • 51