Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot multiple variables on y-axis using ggplot2?

Tags:

r

ggplot2

I have a text file containing data like this:

   A             C             G    class     phylum       order
-0.000187   -0.219166   1.693306 Chordata   Monotremata   Mammalia  
0.015664    -0.264506   1.482692 Chordata   Batidoidimorpha   Chondrichthyes    
-0.404323   0.219374    2.230190 Platyhelminthes   Cyclophyllidea   Cestoda 

but of course it has a lot of rows. I want to plot this data in such a way that all the classes are plotted on the x-axis, each one of them has the A, C and G value plotted as geom_point, and that these points are connected using a line with a specific color depending on A,C or G. I managed to do this by using the plot and par functions, but now I want to do it using the ggplot library.

like image 397
weblover Avatar asked Aug 25 '26 20:08

weblover


1 Answers

The specifics of your question are a bit unclear, but the general approach to plotting multiple variables in one plot with ggplot graphics is to melt() the data.frame() first. I didn't follow how the points and lines are supposed to fit into your graph, but here's an approach that uses the colour parameter to plot the columns A, C, and G by class on the x-axis:

library(ggplot2)
library(reshape2)

df <- data.frame(a = rnorm(10), c = rnorm(10), g = rnorm(10), class = sample(letters[20:23], 10, TRUE))
df.m <- melt(df)
ggplot(df.m, aes(class, value, colour = variable)) +
  geom_point()
like image 116
Chase Avatar answered Aug 28 '26 10:08

Chase