0

I am trying to plot three lines from my dataframe. I read several posts regarding how to plot multiple lines into the same graph with ggplot2. Stackoverflow suggested this one "Plotting two variables as lines using ggplot2 on the same graph" and I tried it.

Here, is the database I am trying to plot:

test_df <- data.frame (samples = c("SRR7504771_sort", "SRR7504772_sort", "SRR7504773_sort", "SRR7504774_sort",
                                   "SRR7504775_sort", "SRR7504776_sort", "SRR7504777_sort", "SRR7504778_sort",
                                   "SRR7504779_sort", "SRR7504780_sort", "SRR7504781_sort", "SRR7504782_sort",
                                   "SRR7504783_sort", "SRR7504784_sort", "SRR7504785_sort", "SRR7504786_sort",
                                   "SRR7504787_sort", "SRR7504788_sort"),
                       MCBT_H37Rv_40_60 = c(57782763, 67674340, 54899455, 44669274, 56950512, 48855759, 52386587,
                                            49545209, 49227176, 45888933, 51202586, 63434537, 48509128, 57890225,
                                            50717205, 72839546, 60569151, 49096259),
                       MCBT_H37Rv_100_120 = c(57782639, 67674202, 54899363, 44669166, 56950405, 48855664, 52386480, 
                                              49545100, 49227101, 45888842, 51202499, 63434455, 48509002, 57890110, 
                                              50717155, 72839309, 60569035, 49096185),
                       MCBT_H37Rv_300_320 = c(57781333, 67672869, 54898438, 44668040, 56949390, 48854736, 52385075, 
                                              49544091, 49226094, 45887731, 51201465, 63433682, 48507580, 57888811, 
                                              50716314, 72837254, 60568018, 49095277)
)

And here is the code I used:

library("reshape2")

ggplot(data=test_data_long,
       aes(x=samples, y=value, colour=variable, group=variable)) +
       geom_line() + 
       theme(axis.text.x=element_text(angle=40, hjust=1))

I pass to ggplot the right stacked data as requested, but it does not work and my code still plot only 1 line.

enter image description here

The reason why I set "group" parameter in each aes was that I got an error if I run the command without setting groups.

> ggplot(data=test_data_long,
+        aes(x=samples, y=value, colour=variable)) +
+        geom_line() + 
+        theme(axis.text.x=element_text(angle=40, hjust=1))
geom_path: Each group consists of only one observation. Do you need to adjust
the group aesthetic?

Thank you in advance.

Giamma Fer
  • 65
  • 1
  • 8
  • You did this correctly. However the data is so similar that the lines are on top of each other. You can see this by using `geom_jitter()` to wiggle the dots. – M.Viking Sep 08 '22 at 15:19
  • or add jitter directly to the lines `geom_line(position=position_jitter(w=.5, h=.5))+` – M.Viking Sep 08 '22 at 15:23
  • 1
    Yes, you are right!!!! I did not expect similar data and did not inspect them enough to catch that they overlapped in the lineplot. Thanks for your help M.Viking. – Giamma Fer Sep 17 '22 at 20:33

0 Answers0