0
votes

I would like to recreate the following chart in R using ggplot. My data is as per a similar table where for each code (A, B, C etc.). I have a current value, a value 12M ago and the respective range (max, min) over the period.

My chart needs to show the current value in red, the value 12M ago in blue and then a line show the max and min range.

I can produce this painstaking in Excel using error bars, but I would like to reproduce it in R. Any ideas on how I can do this using ggplot? Thanks.

Data table

Chart

1

1 Answers

1
votes

Here's what I came up with, but just a note: please if you post your dataset, don't post an image, but instead post the result of dput(your.data.frame). The result of that is easily copy-pasted into the console in order to replicate your dataset, whereas I recreated your data frame manually. :/

A few points first regarding your data as is and the intended plot:

  • The red and blue hash marks used to indicate 12 months ago and today are not a geom I know of off the top of my head, so I'm using geom_point here to show them (easiest way). You can pick another geom of you wish to show them differently.

  • The ranges for high and low are already specified by those column names. I'll use those values for the required aesthetics in geom_errorbar.

You can use your data as is to plot and use two separate geom_point calls (one for "today" and one for "12M ago"), but that's going to make creating the legend more difficult than it needs to be, so the better option is to adjust the dataset to support having the legend created automatically. For that, we'll use the gather function from tidyr, being sure to just "gather together" the information in "today" and "12M ago" (my column name for that was different b/c you need to start with a letter in the data frame), but leave alone the columns for "high", "low", and the letters (called "category" in my dataframe).

Where df is the original data frame:

df1 <- df %>% gather(time, value, -category, -high, -low)

The new dataframe (df1) looks like this (18 observations total):

  category high low    time value
1        A   82  28 M12.ago    81
2        B   82  54 M12.ago    80
3        C   80  65 M12.ago    75
4        D   76  34 M12.ago    70
5        E   94  51 M12.ago    93
6        F   72  61 M12.ago    65

where "time" has "M12.ago" or "today".

For the plot, you apply category to x and value to y, and specify ymax and ymin with high and low, respectively for the geom_errorbar:

ggplot(df1, aes(x=category, y=value)) +
    geom_errorbar(aes(ymin=low, ymax=high), width=0.2) + ylim(0,100) +
    geom_point(aes(color=time), size=2) +
    scale_color_manual(values=list('M12.ago'='blue', 'today'='red')) +
    theme_bw() + labs(color="") + theme(legend.position='bottom')

Giving you this:

enter image description here