[英]How to plot multiple variables on y-axis using 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.
我想以这样一种方式绘制此数据,即所有类都绘制在 x 轴上,每个类都将 A、C 和 G 值绘制为 geom_point,并且这些点使用具有特定属性的线连接颜色取决于 A、C 或 G。我设法通过使用 plot 和 par 函数来做到这一点,但现在我想使用 ggplot 库来做到这一点。
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.您的问题的具体情况有点不清楚,但是使用
ggplot
图形在一个图中绘制多个变量的一般方法是首先melt()
data.frame()
。 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:我没有遵循点和线应该如何适合您的图形,但这是一种使用
colour
参数在 x 轴上按class
绘制A
、 C
和G
列A
:
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()
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.答案是,您需要一个NEW列,可以将其设置为群组交互。 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.在这里,我创建了一个名为 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
slightly modiftiyng the ggplot code above:稍微修改上面的ggplot代码:
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()
如果要分隔线,请使用
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.这里的要点是您需要一个
group=interaction()
的因子,它与 x 轴分开并跨越 x 轴。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.