1

I have the following:

set.seed(100)
df <- data.frame(
  lng    = runif(n=20, min=5,  max=10),
  lat    = runif(n=20, min=40, max=50),
  year   = rep(c("2001","2002","2003","2004"), each=5),
  season = sample(c("spring", "autumn"), 10, replace = T),
  info   = sample(c("yes","no"), 10, replace = T)
  )

Which can be plotted by:

ggplot() +
  geom_point(data=df,
             aes(x     = lng,
                 y     = lat,
                 color = year,
                 shape = season),
             size=3)

To produce:

plot with current code

Great. But I want a red outline on the shapes were info == "yes".

The desired output would be:

desired output

Not made using actual data, just for demonstrative purpose. Made in powerpoint.

Admittedly it is similar to this question here, but not quite.

I am happy to split the df using a filter if easier then two + geom_points()

Many thanks

Jim

Jim
  • 558
  • 4
  • 13
  • Not sure about the border, However, you can use size = info OR You may want to use facet_wrap( ~ info) for clearer representation of data. – nikn8 Apr 13 '20 at 12:25
  • Hi, yes I did toy with the idea of size, but it seems like a compromise to me. thanks for you suggestion – Jim Apr 13 '20 at 13:09

1 Answers1

3

Below is a quick solution (not the best), which is to use another scale, and below I use size as the scale, then use guides() to manually specify the shape to appear in the legend. you need to plot the bigger red shapes first and then plot over so that it looks like an outline:

ggplot() +
geom_point(data=subset(df,info=="yes"),
aes(x=lng,y=lat,shape = season,size=info),col="red") + 
scale_size_manual(values=3.6)+
geom_point(data=df,
             aes(x     = lng,
                 y     = lat,
                 color = year,
                 shape = season),
             size=3)+
guides(size = guide_legend(override.aes = list(shape = 1)))

enter image description here

You can change the legend for the shape by playing around with options in the guide()

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • thanks for you answer. Double plotting is not a bad idea at all, or plotting black point in the middle (as per previous answer - no deleted). Thank you, I wondered if there was a way to do it otherwise... – Jim Apr 13 '20 at 13:05
  • you're welcome ... the tricky thing is the legend. One way is that for the geom_point() call you specify the shapes (basically unfilled versions) directly, but takes a bit more coding – StupidWolf Apr 13 '20 at 13:10
  • someone in SO recommended this package https://cran.r-project.org/web/packages/gghighlight/vignettes/gghighlight.html .. you can take a quick look, maybe it's useful for what you need – StupidWolf Apr 13 '20 at 13:11