Basically my question resembles the following one, but the answer is not working for me:
ggplot: 2 facets with multiple lines
I have a DataFrame
called df_tot like so:
Day row_names BX CR GD GR SB VB
1 J1 nrows_e 12506.000 14.000 746494.000 53.000 286.000 9.000
2 J1 nrows_n 49.000 5.000 17468.000 16.000 38.000 7.000
3 J1 nrows_d 12457.000 9.000 729026.000 37.000 248.000 2.000
4 J1 scar_e 0.385 0.002 23.199 0.011 0.068 0.002
5 J1 scar_n 0.399 0.002 21.215 0.011 0.054 0.003
6 J1 scar_d -0.014 0.001 1.984 0.000 0.014 0.000
I melted it and added a variable group_variable and group_sd so now I got:
df <- melt(df_tot, id.vars = c("Day", "row_names"), variable.name = 'value') %>%
rename(ind = 3) %>%
mutate(value = as.numeric(value))
df$group_variable <- substr(as.character(df$row_names), nchar(as.character(df$row_names)), nchar(as.character(df$row_names)))
df$group_sd <- as.factor(substr(as.character(df$row_names), 1, nchar(as.character(df$row_names))-2))
df$Day <- factor(df$Day, levels = c("J1", "J2", "J3", "J4", "J5", "J6", "J7", "J8", "J9", "J10", "J11", "J12", "J13", "J14"))
head(df)
Day row_names ind value group_variable group_sd
1 J1 nrows_e BX 12506.000 e nrows
2 J1 nrows_n BX 49.000 n nrows
3 J1 nrows_d BX 12457.000 d nrows
4 J1 scar_e BX 0.385 e scar
5 J1 scar_n BX 0.399 n scar
6 J1 scar_d BX -0.014 d scar
What I want is, like was asked in the question above, to create a facet_grid
for each group_variable
and group_sd
but on each of these facets (subplots) I would like to have the lines of all the ind columns in varying colors.
Until now I tried:
ggplot(df, aes(x = Day, y = value, group = ind)) +
geom_line(aes(colour = ind)) +
facet_grid(group_sd ~ group_variable, scale = "free_y")
The values in the plots are all wrong.
Please tell me if you need me the code to construct the data I will create it for you.