Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort the y-axis bars using geom_linerange

I have the following data. I am trying to make a grant chart. In the example, I only put 4 id’s but in the original, I have around 30 unique ids.

structure(list(start_date = structure(c(14365, 14914, 14214, 
15400, 10957, 10957, 10957, 10957), class = "Date"), treated = c(1, 
1, 1, 1, 0, 0, 0, 0), end_date = structure(c(17896, 17896, 17896, 
17896, 14364, 14913, 14213, 15399), class = "Date"), id = c("A", 
"B", "D", "G", "A", "B", "D", "G")), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -8L))

I am using the following code to plot it.

ggplot(df_input) +
  geom_linerange(aes(y = id, 
                     xmin = start_date,
                     xmax = end_date,
                     colour = as.factor(treated)),
                 size = I(5))

I am not able to sort y-bars alphabetically, I tried sorting it before plotting but no success. From top to bottom I want A, then B and so on. Also, how can I reduce the gap to 0 between where the bar start which is 2000 and the y-labels A, B, D and G.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can reverse the order of the y values by using factors – I’ve done this in the call to ggplot but it couuld be done in the data frame.

Manage the padding around the data with the expand argument of the axis scale.

library(ggplot2)
library(forcats)


df_input <- structure(list(start_date = structure(c(14365, 14914, 14214, 
                                        15400, 10957, 10957, 10957, 10957), class = "Date"), treated = c(1, 
                                                                                                         1, 1, 1, 0, 0, 0, 0), end_date = structure(c(17896, 17896, 17896, 
                                                                                                                                                      17896, 14364, 14913, 14213, 15399), class = "Date"), id = c("A", 
                                                                                                                                                                                                                  "B", "D", "G", "A", "B", "D", "G")), class = c("tbl_df", "tbl", 
                                                                                                                                                                                                                                                                 "data.frame"), row.names = c(NA, -8L))


ggplot(df_input) +
  geom_linerange(aes(y = fct_rev(id),
                     xmin = start_date,
                     xmax = end_date,
                     colour = as.factor(treated)),
                 size = I(5))+
  scale_x_date(expand = expansion(mult = c(0, 0.05)))

Created on 2022-02-18 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading