使用ggplot在y轴上绘制多个变量 [英] Plot multiple variables on y-axis using ggplot

查看:195
本文介绍了使用ggplot在y轴上绘制多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个包含如下数据的文本文件:

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

  ACG类门序
-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

但当然它有很多行。我想以这样一种方式绘制这些数据,即所有类都绘制在x轴上,每个类都将A,C和G值绘制为geom_point,并且这些点使用具有特定颜色取决于A,C或G.
我通过使用plot和par函数设法做到了这一点,但现在我想使用ggplot库来做到这一点。




解决方案

您的问题的细节有点不清楚,但绘制多个变量的一般方法在 ggplot 图形的一个小区中,图形是 melt() data.frame()第一。我没有遵循点和线应该如何适合你的图形,但这里有一个方法,它使用 color 参数来绘制列 A C G class b
$ b $ pre $ code $ library $ g
library $(reshape2)
(10),c = rnorm(10),g = rnorm(10),class = sample(字母[20:23],10,TRUE))$
df < - data.frame b $ b df.m < - 熔化(df)
ggplot(df.m,aes(class,value,color = variable))+
geom_point()


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

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.

Thanks in advance.

解决方案

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

这篇关于使用ggplot在y轴上绘制多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆