Home > Net >  Sort the y-axis bars using geom_linerange
Sort the y-axis bars using geom_linerange

Time:02-18

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.

CodePudding user response:

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)

  • Related