0

I have the following example, where I need to create an extra legend area for the red points, which are collected in gg2 dataframe. The original dataframe gg has way more observations (10000+). I suppose there is a way out by combining the dataframes, but I am not sure how.

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

Thanks!

EDIT: I need labels on top of the points (which is not a problem for me right now, I know how to do it),but also add legends.

The legend can be something of the sort:

legend <- c=(l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15, l16, l17)
user191919
  • 724
  • 1
  • 9
  • 27
  • 1
    quick question, why you have geom_point(aes(colour=z),shape="") + – MLavoie Jan 22 '16 at 20:26
  • Probably has to do with the different legends I had when using `stat_density2d`; it displayed the estimated density as well as the actual values. – user191919 Jan 22 '16 at 20:27
  • maybe you could create a new column in gg2 with a single factor gg2$Color <- 3 and replace your last geom_point() with geom_point(data=gg2,aes(x=x2,y=y2, color=as.factor(Color))) + scale_color_manual(name="RedPoint", values=c("red")); it works when I remove your first geom_point() expression – MLavoie Jan 22 '16 at 20:30
  • I need a legend for each red point, is that possible? – user191919 Jan 22 '16 at 20:39
  • but they all red, how do you want to label them? x2 and y2 in gg2 are both numeric – MLavoie Jan 22 '16 at 20:40
  • Sorry I didnt explain the problem very accurately. I would like to add a legend to them since I will have an abbreviated label for each point, added with a `geom_text`. I will alter the example. – user191919 Jan 22 '16 at 20:44
  • What should the additional legend look like? As written, this problem is very poorly described (though interesting) – alexwhitworth Jan 22 '16 at 21:21
  • Possible duplicate: http://stackoverflow.com/questions/10366005/showing-separate-legend-for-a-geom-text-layer – alexwhitworth Jan 22 '16 at 21:25
  • try this geom_point(data=gg2,aes(x=x2,y=y2, color=as.factor(legends)))+ scale_color_manual(name="RedPoint", values=c("red", "red", "red", "red", "red", "red", "blue", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red"), labels=c("pt1", "pt2", "pt3", "pt4", "pt5", "pt6", "pt7", "pt8", "pt9", "pt10", "pt11", "pt12", "pt13", "pt14", "pt15", "pt16", "pt17")) + theme(legend.position = c(.5, .5)) + theme(legend.background = element_rect(fill = "yellow")) + theme(legend.key = element_rect(fill = "green"))...the extra is only to show u what u can do – MLavoie Jan 22 '16 at 21:27
  • MLavoie suggestion did work, although I would like to break the legend in two columns or make it presentable in some fashion. I recognize I did not ask that in the original questio, so if needs be I will accept the above as an swer and open a new question. Anyway, thanks for the help! – user191919 Jan 24 '16 at 17:34

0 Answers0