First, my apologies because I've been trying to solve this issue by sectioning it in small parts (see 1, 2, 3). However, I am totally stuck when merging all of them. In order to replicate it and ease reproducibility, you can download the original data here.
Once you load them in R, this is the structure:
> str(data_example2)
'data.frame': 252 obs. of 4 variables:
$ Groups : Factor w/ 6 levels "Group1","Group2",..: 1 1 1 1 1 1 1 2 2 2 ...
$ Y_values: Factor w/ 126 levels "C if I1I2P3P4M1M2",..: 63 95 1 115 123 112 114 48 17 67 ...
$ Units : Factor w/ 2 levels "Uni1","Uni2": 1 1 1 1 1 1 1 1 1 1 ...
$ X_value : num 1 0.35 0.93 0.73 0.95 0.32 0.88 0.13 0.93 0.84 ...
So, what do I want? I would like to build a line chart in ggplot2 using these conditions:
- X axis represents variable
X_value
. - Y axis represents variable
Y_value
. As you can see in variableUnits
, we have two groups (Uni1
andUni2
), each composed by 126 observations. More importantly, these 126 observations in each Unit is composed by 126 factors. It is very important to keep the order of theseY_values
, so that in the line graph the top left Y value should beI1 if I2CP3P4M1M2
and the bottom left should beI1I2CP3 if P4M1M2
- I would like to trace two lines, on for
Uni1
, and one forUni2
. - I would like to shade the background using the factors represented in the variable
Groups
, maybe usinggeom_rect()
, but without representing the outlines of the rectangles and keeping different colors for each of the 6 groups.
Final chart should be something like this but shading per Groups
.