在ggplot中绘制多个变量 [英] plotting multiple variables in ggplot
问题描述
我有一张看起来像这样的数据表 -
pos gtt1 gtt2 ftp1 ftp2
8 100 123 49 101
9 85 93 99 110
10 111 102 53 113
11 88 110 59 125
12 120 118 61 133
13 90 136 64 145
14 130 140 104 158
15 78 147 74 167
16 123 161 81 173
17 160 173 88 180
18 117 180 94 191
19 89 188 104 199
20 175 197 107 213
我想用 pos
(position)使用ggplot在x轴上。我试图用一种颜色和 ftp1 $ c>显示
gtt1
和 gtt2
$ c>和 ftp2
为另一种颜色,因为它们是样本的单独组(gtt和ftp)。我已经成功创建了图形,但所有四行都有不同的颜色。我想只保留传说中的gtt和ftp(不是全部四个)。奖金,我怎样才能让这些线条变得光滑。
以下是我到目前为止所做的:
library(reshape2 ); library(ggplot2)
data< - read.table(myfile.txt,header = TRUE,sep =\ t)
data.melt < - melt(data, id =pos)
ggplot(data.melt,aes(x = pos,y = value,color = variable))+ geom_line()
提前致谢
最简单的方法是重新塑形您的数据的方式稍有不同:
dd1 = melt(dd [,1:3],id = c(pos ))
dd1 $ type =gtt
dd2 = melt(dd [,c(1,45)],id = c(pos))
dd2 $ type =ftp
dd.melt = rbind(dd1,dd2)
现在我们有一个指定变量type的列:
R>头(dd.melt,2)
pos变量值类型
1 8 gtt1 100 gtt
2 9 gtt1 85 gtt
数据一旦采用这种格式,ggplot命令很简单:
ggplot(dd.melt,aes(x = pos,y = value))+
geom_line(aes(color = type,group = variable))+
scale_colour_manual(values = c(gtt =蓝色,ftp =红色))
您可以使用 stat_smooth
:
## span控制平滑
g + stat_smooth(se = FALSE ,span = 0.5)
I have a data table which looks like this-
pos gtt1 gtt2 ftp1 ftp2
8 100 123 49 101
9 85 93 99 110
10 111 102 53 113
11 88 110 59 125
12 120 118 61 133
13 90 136 64 145
14 130 140 104 158
15 78 147 74 167
16 123 161 81 173
17 160 173 88 180
18 117 180 94 191
19 89 188 104 199
20 175 197 107 213
I want to make a line graph with pos
(position) on the x-axis using ggplot. I am trying to show gtt1
and gtt2
lines in one colour and ftp1
and ftp2
in another colour, because they are separate groups (gtt and ftp) of samples. I have successfully created the graph, but all four lines are in different colours. I would like to keep only gtt and ftp in the legend (not all four). Bonus, how can I make these lines little smooth.
Here is what I did so far:
library(reshape2);library(ggplot2)
data <- read.table("myfile.txt",header=TRUE,sep="\t")
data.melt <- melt(data,id="pos")
ggplot(data.melt,aes(x=pos, y=value,colour=variable))+geom_line()
Thanks in advance
The easiest way is to re-shape your data in a slightly different way:
dd1 = melt(dd[,1:3], id=c("pos"))
dd1$type = "gtt"
dd2 = melt(dd[,c(1, 4:5)], id=c("pos"))
dd2$type = "ftp"
dd.melt = rbind(dd1, dd2)
Now we have a column specifying the variable "type":
R> head(dd.melt, 2)
pos variable value type
1 8 gtt1 100 gtt
2 9 gtt1 85 gtt
Once the data is in this format, the ggplot command is straightforward:
ggplot(dd.melt,aes(x=pos, y=value))+
geom_line(aes(colour=type, group=variable)) +
scale_colour_manual(values=c(gtt="blue", ftp="red"))
You can add smoothed lines using stat_smooth
:
##span controls the smoothing
g + stat_smooth(se=FALSE, span=0.5)
这篇关于在ggplot中绘制多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!