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

R: GGPlot Multiple Boxplots

I would like to plot four boxplots in one chart, however i want to add the median and range myself.

My code

ggplot(data = df_subset[df, aes(y = target_fed_funds_rate_median, x = Date, group = Date)) +
  geom_boxplot(aes(ymin = target_fed_funds_rate_lr, lower = target_fed_funds_rate_cl, middle = target_fed_funds_rate_median,
    upper = target_fed_funds_rate_ch, ymax = target_fed_funds_rate_hr))

generates the following chart: What my code is generating

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

However, I am looking to create a chart similar to this one: How my chart should look like

What my Dataframe looks like: my dataframe df:

Any ideas how to fix this? Many thanks

Below a reproducable example:

time = c(1, 2, 3, 4, 5)
median = c(1, 2, 3, 4, 5)
low = c(0.5, 1.5, 2.5, 3.5, 4.5)
high = c(1.5, 2.5, 3.5, 4.5, 5.5)
center_high = c(1.25, 2.25, 3.25, 4.25, 5.25)
center_low = c(0.75, 1.75, 2.75, 3.75, 4.75)

df = data.frame(median, low, high, center_high, center_low)

ggplot(data = df, aes(y = median, x = time, group = time)) +
  geom_boxplot(aes(ymin = low, lower = center_low, middle = median,
    upper = center_high, ymax = high))

Any ideas how to fix this? Many thanks

>Solution :

As you want to "manually" create a boxplot by providing the boxplot stats you have to set stat="identity" in geom_boxplot:

library(ggplot2)

ggplot(data = df, aes(y = median, x = time, group = time)) +
  geom_boxplot(aes(
    ymin = low, lower = center_low, middle = median,
    upper = center_high, ymax = high
  ), stat = "identity")

enter image description here

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