Search code examples
rplotggplot2latticescatter-plot

Creating a scatter plot for multiple rows in R


I have a data frame that looks something like this:

        Samp1    Samp2    Samp3     Samp4    Samp5
Gene1    84.1     45.2     34.3      54.6     76.2
Gene2    94.2     12.4     68.0      75.3     24.8
Gene3    29.5     10.5     43.2      39.5     45.5
...

I am trying to create a scatter plot where the x-axis are the samples(Samp1-5), and the y-axis are the rows(Gene1-3 and so on), but I want the data of each row to be plotted as a different color on the same plot.

Any thoughts on how to do this in R? I am more than willing to use ggplot2, lattice, car or any other package in R.


Solution

  • Here is a solution with ggplot2:

    The data:

    dat <- read.table(text="Samp1    Samp2    Samp3     Samp4    Samp5
      Gene1    84.1     45.2     34.3      54.6     76.2
      Gene2    94.2     12.4     68.0      75.3     24.8
      Gene3    29.5     10.5     43.2      39.5     45.5", header = TRUE)
    

    The plot:

    library(ggplot2)  
    ggplot(stack(dat), aes(x = ind, y = values, colour = rownames(dat))) +
      geom_point()
    

    enter image description here