如何使用ggplot2在y轴上绘制多个变量?

发布于 2024-10-26 02:25:33 字数 494 浏览 1 评论 0原文

我有一个包含这样的数据的文本文件:

   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 

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风流物 2024-11-02 02:25:33

您的问题的具体情况有点不清楚,但使用 ggplot 图形在一个图中绘制多个变量的一般方法是 melt() data.frame () 首先。我没有遵循点和线应该如何适合您的图表,但这里有一种使用 colour 参数来绘制列 A的方法CG 按 x 轴上的 class

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

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()
坦然微笑 2024-11-02 02:25:33

我有一个类似的问题,我想绘制。

答案是,你需要一个NEW列,可以将其设置为群组交互。在这里,我创建了一个名为 V1 的列,它指定每个字母也属于哪个字母,然后使用 aes(group=interaction(variable.factor, new.factor)。在本示例中,该列“V1”是任意

  class variable       value V1
1      u        a  0.77041380  a
2      v        a  0.09461429  a
3      t        a  0.22704242  a
4      w        a -0.21501380  a
5      w        a -0.48246983  a
6      v        a  1.69609897  a
7      w        a -0.38847860  a
8      t        a  2.45669883  a
9      t        a  0.24774451  a
10     u        a  0.04195110  a
11     u        c  0.57444553  c
12     v        c  0.73172047  c
13     t        c -1.59409421  c
14     w        c -0.12679464  c
15     w        c  0.19424856  c
16     v        c -1.28742724  c
17     w        c -1.12103626  c
18     t        c -0.57090558  c
19     t        c  0.53798077  c
20     u        c -0.47777022  c
21     u        g -0.91249913  g
22     v        g -1.49256508  g
23     t        g -1.77449710  g
24     w        g  0.71426647  g
25     w        g  0.79678361  g
26     v        g -1.28814106  g
27     w        g -1.04701972  g
28     t        g  0.07309817  g
29     t        g  2.03606615  g
30     u        g  1.76030312  g

修改上面的 ggplot 代码:

ggplot(df.m, aes(class, value, colour = variable, group = interaction(V1, variable))) +
  geom_point()+
  geom_line()

这会产生一条连接每个类别的每个字母的线(来自上面的答案)(抱歉,低代表,请点击链接)

按线连接的字母

如果要分隔线,请使用 position=position_dodge()< /code>

ggplot(df.m, aes(class, value, colour = variable, group = interaction(V1, variable))) +
  geom_point(position = position_dodge(width = 0.2))+
  geom_line(position = position_dodge(width = 0.2))

字母通过带有抖动的线路连接

这里的要点是你需要一个因素group=interaction() 与 x 轴分开并跨越 x 轴。

I had a similar issue I wanted to plot.

The answer is, you need a NEW column, which can be set as the group interaction. Here, I created a column called V1, which designates which letter, each letter belongs too, then use aes(group=interaction(variable.factor, new.factor). In this example case, the column 'V1' is arbitrary.

  class variable       value V1
1      u        a  0.77041380  a
2      v        a  0.09461429  a
3      t        a  0.22704242  a
4      w        a -0.21501380  a
5      w        a -0.48246983  a
6      v        a  1.69609897  a
7      w        a -0.38847860  a
8      t        a  2.45669883  a
9      t        a  0.24774451  a
10     u        a  0.04195110  a
11     u        c  0.57444553  c
12     v        c  0.73172047  c
13     t        c -1.59409421  c
14     w        c -0.12679464  c
15     w        c  0.19424856  c
16     v        c -1.28742724  c
17     w        c -1.12103626  c
18     t        c -0.57090558  c
19     t        c  0.53798077  c
20     u        c -0.47777022  c
21     u        g -0.91249913  g
22     v        g -1.49256508  g
23     t        g -1.77449710  g
24     w        g  0.71426647  g
25     w        g  0.79678361  g
26     v        g -1.28814106  g
27     w        g -1.04701972  g
28     t        g  0.07309817  g
29     t        g  2.03606615  g
30     u        g  1.76030312  g

slightly modiftiyng the ggplot code above:

ggplot(df.m, aes(class, value, colour = variable, group = interaction(V1, variable))) +
  geom_point()+
  geom_line()

This results in a line which connects each letter, across each class (from the above answer). (sorry low rep, please follow link)

letters connected by line

If you want to separate the lines, use position=position_dodge()

ggplot(df.m, aes(class, value, colour = variable, group = interaction(V1, variable))) +
  geom_point(position = position_dodge(width = 0.2))+
  geom_line(position = position_dodge(width = 0.2))

letters connected by line with jitter

The take away here is you need a factor for group=interaction() which is separate from, and spans across your x axis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文