使用ggplot绘制y轴上的多个变量。

时间:2022-12-16 12:48:09

I'm a beginner in R and I have a problem and need your help please.

我是R的初学者,我有一个问题,需要你的帮助。

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.

当然它有很多行。我想把这些数据以这样一种方式,所有的类都标注在x轴,每个人都有一个,C和G值绘制geom_point,和这些点连接使用一个与一个特定的颜色取决于,C或G .为此,我设法用情节和标准功能,但现在我想使用ggplot库。

Thanks in advance.

提前谢谢。

1 个解决方案

#1


8  

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:

你的问题的具体细节有点不清楚,但是用ggplot图形来绘制多个变量的一般方法是先将data.frame()融化。我没有注意到这些点和线应该如何适合你的图,但这里有一个方法,用颜色参数在x轴上画出A, C和G的列:

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()

#1


8  

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:

你的问题的具体细节有点不清楚,但是用ggplot图形来绘制多个变量的一般方法是先将data.frame()融化。我没有注意到这些点和线应该如何适合你的图,但这里有一个方法,用颜色参数在x轴上画出A, C和G的列:

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()