0

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.

user9396820
  • 135
  • 8
  • hi notice the rename after melt. I'm not sure I understand what you mean. You start with df_tot you melt and rename you get df the dataframe to plot. – user9396820 Nov 09 '20 at 22:32
  • Ignore previous comments. I did reproduce your plot but need more specificity than *values in the plots are all wrong*. I subsetted one of the facets and data is consistent with plot. Since most lines are at 0, you only see the last drawn. Check with: `subset(df, group_variable == "d" & group_sd == "nc_pi")` – Parfait Nov 09 '20 at 22:55
  • look at the nrows e plot for example for me it does not reflect the reality with GD meaing it is not the GD values I can show you maybe ? – user9396820 Nov 09 '20 at 23:57
  • Still not sure what you mean. Using your older posted data (unclear why you removed it), the `e` and `nrows` facet is consistent with `subset(df, group_variable == "e" & group_sd == "nrows")`. Are you confused by the scientific notation? This subset contains a wide difference of values across `ind` and `GD` line is well above 600,000 (i.e., 6e+05). – Parfait Nov 10 '20 at 15:56

0 Answers0