1

这个问题中使用 ggplot 的解决方案对我的数据非常有效。但是,我正在尝试添加一个图例,而我尝试过的一切都不起作用......

例如,在上述问题的ggplot示例中,如何添加图例以显示红色曲线与“海洋”相关,绿色曲线与“土壤”相关?是的,我想添加我将定义的文本,它与我的 data.frame 中的任何其他变量都不相关。

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

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

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

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")

剧情看起来这个

我只想要一个红色和蓝色的传说,上面写着“大师”和“转座子”

谢谢!

4

1 回答 1

5

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()

enter image description here

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"))

enter image description here

于 2013-09-26T21:08:19.433 回答