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.
You can create a scatter plot in R with multiple variables, known as pairwise scatter plot or scatterplot matrix, with the pairs function. In addition, in case your dataset contains a factor variable, you can specify the variable in the col argument as follows to plot the groups with different color.
If you have a grouping variable you can create a scatter plot by group passing the variable (as factor) to the col argument of the plot function, so each group will be displayed with a different color. Note that internally the function will store the factors as integers (1 = "black" , 2 = "red" , 3 = "green" , …).
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()
If you want to do this in lattice or ggplot2 then you will probably need to reshape your data to long format, see the reshape
function or the reshape2
package.
For base graphics the matplot
function will probably do what you want, you may need to supress the x-axis and use the axis
function to add your own if you don't want just the numbers 1 through 5 as the axis tick marks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With