7

次のように、ggplot2 で複数のデータフレームからデータをプロットしています。

# subset of iris data
vdf = iris[which(iris$Species == "virginica"),]
# plot from iris and from vdf
ggplot(iris) + 
   geom_line(aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) + 
   geom_line(aes(x=Sepal.Width, y=Sepal.Length), colour="gray", size=2,
             data=vdf)

の凡例には、 からではなく からのcolourエントリのみが含まれます。ggplot2 agg を の凡例にするにはどうすればよいですか? この場合、 の凡例の下の灰色の線になりますか? ありがとう。irisvdfdata=vdfiris

4

2 に答える 2

7

aes凡例に表示するには、色を として設定する必要があります。

# subset of iris data
vdf = iris[which(iris$Species == "virginica"),]
# plot from iris and from vdf
library(ggplot2)
ggplot(iris) + geom_line(aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
  geom_line(aes(x=Sepal.Width, y=Sepal.Length, colour="gray"), 
            size=2, data=vdf)

ここに画像の説明を入力

編集同じ aes に対して複数の凡例を持つことはできないと思います。ここで回避策:

library(ggplot2)
ggplot(iris) + 
  geom_line(aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
  geom_line(aes(x=Sepal.Width, y=Sepal.Length,size=2), colour="gray",  data=vdf) +
   guides(size = guide_legend(title='vdf color'))

ここに画像の説明を入力

于 2013-07-14T18:18:31.160 に答える