如何使用ggplot2在y轴上绘制多个变量?
我有一个包含这样的数据的文本文件:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题的具体情况有点不清楚,但使用 ggplot 图形在一个图中绘制多个变量的一般方法是
melt()
data.frame () 首先。我没有遵循点和线应该如何适合您的图表,但这里有一种使用colour
参数来绘制列A
、的方法C
和G
按 x 轴上的class
:The specifics of your question are a bit unclear, but the general approach to plotting multiple variables in one plot with
ggplot
graphics is tomelt()
thedata.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 thecolour
parameter to plot the columnsA
,C
, andG
byclass
on the x-axis:我有一个类似的问题,我想绘制。
答案是,你需要一个NEW列,可以将其设置为群组交互。在这里,我创建了一个名为 V1 的列,它指定每个字母也属于哪个字母,然后使用 aes(group=interaction(variable.factor, new.factor)。在本示例中,该列“V1”是任意
修改上面的 ggplot 代码:
这会产生一条连接每个类别的每个字母的线(来自上面的答案)(抱歉,低代表,请点击链接)
按线连接的字母
如果要分隔线,请使用
position=position_dodge()< /code>
字母通过带有抖动的线路连接
这里的要点是你需要一个因素
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.slightly modiftiyng the ggplot code above:
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()
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.