-3

I have generated a plot using ggplot2, however I have difficulties with creating a custom legend. My plot looks like this: enter image description here

The parts in black are what was created by ggplot while the parts in red are what I need to be added in. The rectangle beside the point is actually a label, but I need the label outline (a rectangle) to be part of the legend. I've referred to related problems but I haven't seen a similar problem so far as I would interpret the issue to be more "drawing" in the legend than "mapping".

Could anyone help me pls? Since I have a number of plots I would prefer to solve the issue with R than manually edit the plots with an image editor. Thanks!

Edit: As requested, a reproducible sample. Sorry for not adding in advance!

library(ggrepel)
dput(df)
structure(list(ID = 1L, Date = structure(16259, class = "Date"), 
    Primary = 0.009, Secondary = structure(1L, .Label = "Label ABC", class = "factor")), row.names = c(NA, 
-1L), class = "data.frame", .Names = c("ID", "Date", "Primary", 
"Secondary"))

ggplot(df, aes(Date, Primary)) + geom_point(aes(shape=ifelse(Primary>0, "Detected", "Not Detected"))) + geom_label_repel(aes(label=Secondary)) + scale_shape_manual(values=c("Not Detected"=1,"Detected"=19),name="Primary") + theme_bw()
phusion
  • 97
  • 1
  • 9
  • Could you post the code you used to generate the plot? – guscht Dec 21 '17 at 10:36
  • Have you seen this [post](https://stackoverflow.com/questions/23635662/editing-legend-text-labels-in-ggplot)? If "yes" and the problem remains unsolved, than I strongly recommend you to edit the question, by providing a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This way you will help others to actually help you better. – mnm Dec 21 '17 at 12:23
  • Sorry, added, thanks. – phusion Dec 21 '17 at 12:46

1 Answers1

2

Your data:

df <- data.frame(stringsAsFactors=FALSE,
           ID = c(1L, 2L),
           Date = c("2014-07-08", "2017-03-12"),
           Primary = c(0.009, -0.05),
           Secondary = c("Label ABC", "Label BCD")
)

Here's native ggplot solution. Basically there's a hidden rectangle behind each point and it is the one showing in the legend. You need to put it onto a different aestetic, such as color, while points are differentiated by shape, in order for it not to be combined with points into one legend item.

library(ggrepel)


df %>% 
  mutate(isDetected=ifelse(Primary>0, "Detected", "Not Detected")) %>% 
  ggplot(aes(Date, Primary)) + 
    geom_rect(aes(xmin=Date, xmax=Date, ymin=Primary, ymax=Primary, color=isDetected), 
              fill="white")+
    geom_point(aes(shape=isDetected), size=3) + 
    geom_label_repel(aes(label=Secondary, color=isDetected), show.legend = FALSE) + 
    scale_shape_manual(values=c("Not Detected"=1,"Detected"=19))+
    labs(shape="Primary",
        color="Secondary")+
    theme_bw()

enter image description here

dmi3kno
  • 2,943
  • 17
  • 31