ggplot中两个带有两个DataFrame的图例

时间:2016-01-22 20:14:10

标签: r ggplot2

我有以下示例,其中我需要为红点创建额外的图例区域,这些区域在gg2数据框中收集。 原始数据框gg有更多观察结果(10000+)。我想通过组合数据帧有一条出路,但我不确定如何。

set.seed(1234)
n <- 200
gg <- data.frame(wf=rnorm(n,0.5),wb=rnorm(n,0.5),z=runif(n,0,6))

x2 = c(0.63, 0.45, 0.6, 0.46, 0.84, 0.68, 0.68, 
          0.38, 0.64, 0.94, 0.62, 0.76, 0.55, 0.81, 0.53, 0.68, 0.61)

y2 <- c(0.37, 0.55, 0.37, 0.46, 0.18, 0.25, 0.31,
           0.61, 0.35, 0.06, 0.38, 0.24, 0.4, 0.18, 0.47, 0.32, 0.39)

labels = letters[seq(from = 1, to = 17)]

legends<- seq(from = 1, to = 17)

gg2 <- data.frame(x2,y2,labels,legends)
gg2$Color <- 3

# plot it
gg$`My Title` <- gg$z
ggplot(data=gg, aes(x=wf, y=wb, color=`My Title`)) + 
  #geom_point(aes(colour=z),shape="") +
  stat_density2d(aes(fill = ..level..),n = 100,contour = TRUE,geom = "polygon") +
  labs(x=expression(w[f]),y=expression(w[b])) +
  guides(fill=F) +
  geom_point(data=gg2,aes(x=x2,y=y2, color=as.factor(Color)))+
  scale_color_manual(name="RedPoint", values=c("red"))
  #geom_point(data=gg2,aes(x=x2,y=y2),inherit.aes=FALSE,color="red")

谢谢!

编辑:我需要点数之上的标签(这对我来说不是问题,我知道怎么做),还要添加传说。

传说可以是某种类型:

legend <- c=(l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15, l16, l17)

0 个答案:

没有答案