0

I created a function where, among other things, I draw the following kind of plot.

enter image description here

The number of bars depends on the number of quantiles I select. To make it easier to read, I prefer to leave the quantile number on the x-axis. Nonetheless, I would like to add a second legend to show the intervals for each quantile. Something like

"1 : (17.5,28.5]" "2 : (28.5,31]" ... "NA : NA"

I can easily create the labels, I just do not know how to use them to create the legend.

labels <- c()
  for(c in 1:ntiles){
    labels <- cbind(labels, paste(dataset_1$Quantile_rank[c],": ", dataset_1$Quantile[c],sep=" "))
  }

Thanks

EDIT:

that would be maybe easier if I add some sample data and the code I wrote.

Data:

Quantile | Quantile_rank | EventRate | EventRate_LowCI90 | EventRate_HighCI90

NA | NA | 0.2857143 | 0.09986631 | 0.5905234

(32.1,39.6] | 1 | 0.5135135 | 0.38248270 | 0.6427128

(39.6,41.2] | 2 | 0.4736842 | 0.34704006 | 0.6038073

and the code is

ggplot(dataset_1, aes(x=as.factor(Quantile_rank), y=EventRate)) +
    geom_bar(aes(fill=EventRate), stat="identity") +
    scale_fill_gradient(low = "green", high = "red") +
    geom_text(aes(label=round(EventRate, digits=3)), vjust=1.6, color="darkblue", size=3.5) +
    geom_errorbar(aes(ymin=EventRate_LowCI90, ymax=EventRate_HighCI90), width=.2,
                  position=position_dodge(.9)) +
    labs(title = paste("Event rate for variable",var, sep=" "),
         subtitle = "Plot of event rates per quantile",
         caption = paste("Number of quantiles:", ntiles, sep=" "),
         x="Bin",
         y = "Event Rate") +
    theme(
      plot.title = element_text(color = "black", size = 12, face = "bold"),
      plot.subtitle = element_text(color = "blue"),
      plot.caption = element_text(color = "blue", face = "italic")
    )

I would like the quantile values to be shown as a second legend (as a column, using the labels above).

Didifr
  • 89
  • 11
  • Couldn't you use `scale_x_discrete(labels = paste0(bin_number, "\n", labels)`? – teunbrand Sep 13 '21 at 11:53
  • This is not what I want to do. Doing so, I would replace the x-axis label. Instead of seeing the quantile ranks, I would see the intervals. I want to see the quantile rank on the x-axis, for 2 reasons: first, if I use the intervals, then it makes it impossible to read, as they are too wide (labels are just mixed); second, using the quantile ranks is easier to interpret. I want to see the interval values as a second legend, next to the Event Rate: as many rows as quantiles, showing the value of the intervals – Didifr Sep 13 '21 at 12:28
  • Please provide us with example data according to https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example so we can reprodce your plot. – danlooo Sep 13 '21 at 12:38
  • Just added. The first three rows/quantiles – Didifr Sep 13 '21 at 12:48

1 Answers1

0

You can calculate the quantile in a new column and use this for your x-axis like this:

library(tidyverse)

iris %>%
  arrange(Sepal.Length) %>%
  mutate(quantile = row_number() / n()) %>%
  ggplot(aes(quantile, Sepal.Length, fill = Sepal.Length)) +
    geom_bar(stat = "identity") +
    scale_fill_binned()

Created on 2021-09-13 by the reprex package (v2.0.1)

danlooo
  • 10,067
  • 2
  • 8
  • 22
  • Not sure I get what you have in mind. I already have the quantile rank computed, and this is what I am using to draw my plot. I edit my question to show how the data looks like, and the code – Didifr Sep 13 '21 at 12:32