in Education by
This question already has answers here: Order Bars in ggplot2 bar graph (14 answers) Closed 3 years ago. I borrowed (and slightly simplified) code for a ggplot2 plotting function from: https://bookdown.org/MathiasHarrer/Doing_Meta_Analysis_in_R/plotting-the-summary.html Given the following data, the function plot_rob() produces the graph shown. I would like to display the bars in the following order (from top to bottom): "Random sequence generation", "Allocation concealment", "Blinding of participants", "Blinding of personnel", "Blinding of outcome assessor", "Incomplete outcome data", "Intention to treat analysis", "Group similarity at baseline", "Co-interventions", "Compliance", "Timing of outcome assessments" data <- structure(list(name = c("Random sequence generation", "Allocation concealment", "Blinding of participants", "Blinding of personnel", "Blinding of outcome assessor", "Incomplete outcome data", "Intention to treat analysis", "Group similarity at baseline", "Co-interventions", "Compliance", "Timing of outcome assessments", "Random sequence generation", "Allocation concealment", "Blinding of participants", "Blinding of personnel", "Blinding of outcome assessor", "Incomplete outcome data", "Intention to treat analysis", "Group similarity at baseline", "Co-interventions", "Compliance", "Timing of outcome assessments", "Random sequence generation", "Allocation concealment", "Blinding of participants", "Blinding of personnel", "Blinding of outcome assessor", "Incomplete outcome data", "Intention to treat analysis", "Group similarity at baseline", "Co-interventions", "Compliance", "Timing of outcome assessments" ), RoB = c("U", "H", "H", "H", "H", "H", "H", "L", "L", "L", "L", "U", "L", "H", "H", "H", "L", "L", "H", "L", "L", "L", "L", "L", "H", "H", "H", "H", "H", "L", "L", "L", "L")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -33L)) The function plot_rob() below: plot_rob <- function(rob.long) { rob.long$RoB<-as.factor(rob.long$RoB) rob.long$RoB<-factor(rob.long$RoB,levels(rob.long$RoB)[c(1,3,2)]) rob.plot <-ggplot(data=rob.long)+ geom_bar(mapping=aes(x=name,fill=RoB), width=0.7, position = "fill", color="black")+ coord_flip(ylim = c(0,1))+ guides(fill = guide_legend(reverse = TRUE))+ scale_fill_manual("Risk of Bias", labels = c(" High risk of bias ", " Unclear risk of bias ", " Low risk of bias "), values = c("U" = "#E2DF07", "H" = "#BF0000", "L" = "#02C100"))+ scale_y_continuous(labels = scales::percent)+ theme(axis.title.x=element_blank(), axis.title.y=element_blank(), axis.ticks.y=element_blank(), axis.text.y = element_text(size=18, color = "black"), axis.line.x = element_line(colour = "black", size = 0.5, linetype = "solid"), legend.position = "bottom", panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), legend.background = element_rect(linetype="solid", colour ="black"), legend.title = element_blank(), legend.key.size = unit(0.75,"cm"), legend.text=element_text(size=12)) return(rob.plot) } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
As @DS_UNI already mentioned you have to convert the name column to a factor and then specify the order. In this case you want the (reverse) order in which they appear so you can use the following line: library(tidyverse) data %>% mutate(name = fct_inorder(name) %>% fct_rev()) %>% plot_rob()

Related questions

0 votes
    This question already has answers here: Order Bars in ggplot2 bar graph (14 answers) Closed 3 years ago. I ... -interventions", "Compliance", "Timing of outcome assessments" data...
asked May 19, 2022 in Education by JackTerrance
0 votes
    What is ggplot2 an implementation of? (a) the Grammar of Graphics developed by Leland Wilkinson (b) 3D ... of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    What is a geom in the ggplot2 system? (a) a plotting object like point, line, or other shape (b) a ... Data Analysis of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    _________ uniforms and customizes plots of packages ggplot2, graphics and lattice. (a) uniCox (b) uniPlot (c) ... of R Programming Select the correct answer from above options...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    The sum of squares of the difference between the observations and the line in the horizontal direction in the ... of R Programming Select the correct answer from above options...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    I have 2 types (say a and b) of dataframes in my R environment, each with 20 years. I am trying ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have 2 types (say a and b) of dataframes in my R environment, each with 20 years. I am trying ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Columns can be arranged in descending order too by using the special ____ operator. (a) asc() (b) desc( ... Operations of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    In order to use the R-related functionality in Dundas BI, you must have access to an existing _________ (a ... Started of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Which of the following is a plot to investigate the order in which observations were recorded? (a) ggplot (b ... of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Which of the following sort a dataframe by the order of the elements in B? (a) x[rev(order(x$B)), ... Regression of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    Which of the following will reverse the order of values in x? (a) rev(x) (b) max(x, na.rm=TRUE) ... Linear Regression of R Programming Select the correct answer from above options...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    In order to calculate confidence intervals and hypothesis tests, it is assumed that the errors are independent and ... R Programming Select the correct answer from above options...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    I am running the code and it works ggplot(data_df, aes(x= RR, y= PPW, col = year)) + ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Is it possible to change the default values of formal parameters in an R function at runtime? Let's assume, we have the function f...
asked Apr 26, 2022 in Education by JackTerrance
...