在同一个图中绘制两张图 [英] Plot two graphs in the same plot

查看:126
本文介绍了在同一个图中绘制两张图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ggplot 的解决方案 这个问题对我的数据非常有效。然而,我试图添加一个图例,并且我尝试过的所有东西都不起作用...



例如,在上面问题的ggplot示例中,我怎么能添加图例以显示红色曲线与海洋相关,绿色曲线与土壤相关?是的,我想添加我将要定义的文本,并且它与我的data.frame中的任何其他变量无关。

下面的例子是我自己的一些数据...

 比率概率统计
1.0e-04 1e-04 891.15
1.0e-05 1e-04 690
...

等(大约400行)。我有两个类似于上面的数据框。
所以我的代码是

pre $ g $ p $ g $ g $ p $($ g $ g $ p $(Master1MY,aes(Probability))
g< ; - ge + point(aes(y = Transposon1MY $ Stats),color =blue); - g + geom_point(aes(y = Master1MY $ Stats),color =red ,size = 1)
g + labs(title =10,000bp and 1MY,x =Probability,y =Stats)

该图看起来像



我只想要一个红色和蓝色的传说,说出主人和转座子

谢谢!
<在 ggplot 中,通常最方便的方式是将数据保持为'长'格式。在这里,我使用 reshape2 包中的函数 melt 将数据从宽格式转换为长格式。根据您指定不同的 aes thetics(大小,形状,颜色等)的方式,将出现相应的图例。

  library(ggplot2)
library(reshape2)

#指的是广泛的格式。
x < - seq(-2,2,0.05)
ocean < - pnorm(x)
soil < - pnorm(x,1,1)
df< ; - data.frame(x,ocean,soil)

#将数据融化为长格式
df2 < - melt(data = df,id.vars =x)

#使用美学参数'color'
ggplot(data = df2,aes(x = x,y = value,color = variable))+ geom_line()


编辑,设置图例的名称和标签



<$手动设置不同颜色的色标和标签名称
ggplot(data = df2,aes(x = x,y = value,color = variable))+
geom_line()+
scale_colour_discrete(name =样本类型,labels = c(Sea water,Soil))

Edit2,遵循新的示例数据
转换您的数据,假设到很长的时间ORMAT。再一次,如果你的数据保持长格式,我相信你可以让你的 ggplot 生活更轻松。我将每一步都与我在第一个答案中使用的简单示例数据相关联。请注意,有许多替代方法可以重新排列数据。基于您在更新中提供的数据中的小部分(不可重现)部分,这是一种方法。

 #x < -  seq(-2,2,0.05)
#Master1MY $概率
概率< - 1:100

#ocean < - pnorm(x)
#Master1MY $ Stats
Master1MY < - rnorm(100,mean = 600,sd = 20)

#soil <-pnorm(x,1,1)
#Transposon1MY $ Stats
Transposon1MY < - rnorm(100,mean = 100,sd = 10 )

#df < - data.frame(x,ocean,soil)
df < - data.frame(Probability,Master1MY,Transposon1MY)

#df2 < - melt(df,id.var =x)
df2 < - melt(df,id.var =Probability)

#default
ggplot(data = df2,aes(x = Probability,y = value,col = variable))+
geom_point()

#更改图例名称和标签, scale_colour_discrete'

#使用'scale_colour_manual'设置手动颜色比例。
$ b ggplot(data = df2,aes(x = Probability,y = value,col = variable))+
geom_point()+
scale_colour_manual(values = c(red ,blue),name =样本类型,labels = c(Master,Transposon))


The solution with ggplot in this question worked really well for my data. However, I am trying to add a legend and everything that I tried does not work...

For example, in the ggplot example in the above question, how I can add a legend to show that the red curve is related to "Ocean" and the green curve is related to "Soil"? Yes, I want to add text that I will define and it is not related to any other variable in my data.frame.

The example below is some of my own data...

Rate     Probability      Stats
1.0e-04    1e-04          891.15 
1.0e-05    1e-04          690
...

etc (it's about 400 rows). And I have two data frames similar to the above one. So My code is

g <- ggplot(Master1MY, aes(Probability))
g <- g + geom_point(aes(y=Master1MY$Stats), colour="red", size=1)
g <- g + geom_point(aes(y=Transposon1MY$Stats), colour="blue", size=1)
g + labs(title= "10,000bp and 1MY", x = "Probability", y = "Stats")

The plot looks like

I just want a red and blue legend saying "Master" and "Transposon"

Thanks!

解决方案

In ggplot it is generally most convenient to keep the data in a 'long' format. Here I use the function melt from the reshape2 package to convert your data from wide to long format. Depending how you specify different aesthetics (size, shape, colour et c), corresponding legends will appear.

library(ggplot2)
library(reshape2)

# data from the example you were referring to, in a 'wide' format.
x  <- seq(-2, 2, 0.05)
ocean <- pnorm(x)
soil <- pnorm(x, 1, 1)
df <- data.frame(x, ocean, soil)

# melt the data to a long format
df2 <- melt(data = df, id.vars = "x")

# plot, using the aesthetics argument 'colour'
ggplot(data = df2, aes(x = x, y = value, colour = variable)) + geom_line()

Edit, set name and labels of legend

# Manually set name of the colour scale and labels for the different colours
ggplot(data = df2, aes(x = x, y = value, colour = variable)) +
 geom_line() +
 scale_colour_discrete(name = "Type of sample", labels = c("Sea water", "Soil"))

Edit2, following new sample data Convert your data, assuming its organization from your update, to a long format. Again, I believe you make your ggplot life easier if you keep your data in a long format. I relate every step with the simple example data which I used in my first answer. Please note that there are many alternative ways to rearrange your data. This is one way, based on the small (non-reproducible) parts of your data you provided in the update.

# x  <- seq(-2, 2, 0.05)
# Master1MY$Probability
Probability <- 1:100

# ocean <- pnorm(x)
# Master1MY$Stats
Master1MY <- rnorm(100, mean = 600, sd = 20)

# soil <- pnorm(x,1,1)
# Transposon1MY$Stats
Transposon1MY <- rnorm(100, mean = 100, sd = 10)

# df <- data.frame(x, ocean, soil)
df <- data.frame(Probability, Master1MY, Transposon1MY)

# df2 <- melt(df, id.var = "x")
df2 <- melt(df, id.var = "Probability")

# default
ggplot(data = df2, aes(x = Probability, y = value, col = variable)) +
  geom_point()

# change legend name and labels, see previous edit using 'scale_colour_discrete'

# set manual colours scale using 'scale_colour_manual'.

ggplot(data = df2, aes(x = Probability, y = value, col = variable)) +
  geom_point() +
  scale_colour_manual(values = c("red","blue"), name = "Type of sample", labels = c("Master", "Transposon"))

这篇关于在同一个图中绘制两张图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆