plotting multiple variables in ggplot

2019-06-12 22:48发布

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

标签: r plot ggplot2
1条回答
\"骚年 ilove
2楼-- · 2019-06-12 23:30

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)
查看更多
登录 后发表回答