2

Sorry if this question has already been answered but I could not find the solution to what I am after. I have a plot that uses both geom_line and geom_point. The result of this is that in the legend, it adds both a line and a point when they should have one or the other. I want to keep the circles for the data tg1 and tg2 and remove the line and then do the opposite to the data full i.e. keep the line but remove the circle. I have seen that something like this works where you want to remove dots from all of the legend entries but nothing to only do specifics Removing ggplot2's geom_point icons from the legend. Can anyone help? Thanks.

plot

#code for plot


library(ggplot2)
library(tidypaleo)

ggplot(LGRSL, aes(x =mmsl , y = Age))+
  coord_flip()+
  theme_classic(12)+
  geom_point(data=tg1,aes(x=mmslc,y=Year,col="Fort Denison 1"),pch=1,size=2)+
  geom_point(data=tg2,aes(x=mmslc,y=Year,col="Fort Denison 2"),pch=1,size=2)+
  geom_lineh(data = full, aes(x=Lutregalammslc,y=Year,col="Full budget"))+
  scale_colour_manual(values=c("grey15","grey50","black"))
  

## data

## tg1

structure(list(Year = 1886:1891, SLR = c(6919L, 6935L, 6923L, 
6955L, 6956L, 6957L), mmsl = c(-0.158, -0.142, -0.154, -0.122, 
-0.121, -0.12), m = c(6.919, 6.935, 6.923, 6.955, 6.956, 6.957
), GIA.correction = c(-0.02814, -0.02793, -0.02772, -0.02751, 
-0.0273, -0.02709), SLRc = c(6.89086, 6.90707, 6.89528, 6.92749, 
6.9287, 6.92991), mmslc = c(-0.19667, -0.18046, -0.19225, -0.16004, 
-0.15883, -0.15762)), row.names = c(NA, 6L), class = "data.frame")

##tg2

structure(list(Year = 1915:1920, SLR = c(7011L, 6929L, 6987L, 
6945L, 6959L, 6951L), mmsl = c(-0.066, -0.148, -0.09, -0.132, 
-0.118, -0.126), m = c(7.011, 6.929, 6.987, 6.945, 6.959, 6.951
), GIA.correction = c(-0.02205, -0.02184, -0.02163, -0.02142, 
-0.02121, -0.021), SLRc = c(6.98895, 6.90716, 6.96537, 6.92358, 
6.93779, 6.93), mmslc = c(-0.09858, -0.18037, -0.12216, -0.16395, 
-0.14974, -0.15753)), row.names = c(NA, 6L), class = "data.frame")

##full

structure(list(Year = 1900:1905, Lutregala = c(-0.103609677, 
-0.118603251, -0.134550791, -0.105553735, -0.103983082, -0.121731984
), Wapengo = c(-0.095213147, -0.096005337, -0.115700625, -0.097696891, 
-0.084444784, -0.109161066), Tarra = c(-0.106672829, -0.109537943, 
-0.135256365, -0.101357772, -0.089716518, -0.104258351), Lutregalammsl = c(-0.292863465, 
-0.307857039, -0.323804579, -0.294807523, -0.29323687, -0.310985772
), Wapengommsl = c(-0.257028279, -0.257820469, -0.277515756, 
-0.259512023, -0.246259916, -0.270976198), Tarrammsl = c(-0.30925682, 
-0.312121933, -0.337840355, -0.303941762, -0.292300508, -0.306842342
), LgGIAc = c(-0.01921, -0.01904, -0.01887, -0.0187, -0.01853, 
-0.01836), WapGIAc = c(-0.02486, -0.02464, -0.02442, -0.0242, 
-0.02398, -0.02376), TarGIAc = c(-0.02373, -0.02352, -0.02331, 
-0.0231, -0.02289, -0.02268), Lutregalammslc = c(-0.312073465, 
-0.326897039, -0.342674579, -0.313507523, -0.31176687, -0.329345772
), Wapmmslc = c(-0.281888279, -0.282460469, -0.301935756, -0.283712023, 
-0.270239916, -0.294736198), Tarmmslc = c(-0.33298682, -0.335641933, 
-0.361150355, -0.327041762, -0.315190508, -0.329522342)), row.names = c(NA, 
6L), class = "data.frame")

##LGRSL

structure(list(depths = c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5), RSL = c(0.047746907, 
0.025564293, 0.021733558, 0.007855661, -0.004909879, 0.01747051
), RSLerror = c(0.058158556, 0.057902654, 0.057988654, 0.057957388, 
0.057905405, 0.057226072), Age = c(2017.456716, 2013.594255, 
2006.92838, 1999.675523, 1994.729181, 1990.518154), Ageerror = c(0.373138707, 
0.77640096, 1.430582242, 1.627131115, 3.222393394, 3.239674718
), mmsl = c(0.01993169, -0.002250924, -0.006081659, -0.019959556, 
-0.032725096, -0.010344707)), row.names = c(NA, 6L), class = "data.frame")

##LGRSLgp

structure(list(Age = 1892:1897, mean = c(-0.298147401, -0.304630597, 
-0.31023294, -0.315506983, -0.321225142, -0.327190675), error = c(0.051858047, 
0.04985084, 0.047760525, 0.045624121, 0.043505044, 0.041477551
), min = c(-0.246289354, -0.254779758, -0.262472416, -0.269882862, 
-0.277720098, -0.285713124), max = c(-0.350005447, -0.354481437, 
-0.357993465, -0.361131103, -0.364730186, -0.368668226), x = c(-0.02125, 
-0.02108, -0.02091, -0.02074, -0.02057, -0.0204), meangia = c(-0.276897401, 
-0.283550597, -0.28932294, -0.294766983, -0.300655142, -0.306790675
), rate = c(NA, -4.967327, -4.946326, -4.964493, -4.977451, -4.911859
), raterror = c(NA, 3.581013, 3.796417, 4.022157, 4.226762, 4.255126
), mmsl = c(-0.325962618, -0.332445814, -0.338048157, -0.3433222, 
-0.349040359, -0.355005892)), row.names = c(NA, 6L), class = "data.frame")

Sophie Williams
  • 338
  • 1
  • 3
  • 14
  • Hi - thanks for the reproducible example - but could you clarify which dataset you dputtted corresponds to which dataset in your code (i.e. which is LGRSL?). Thanks. – user438383 Jul 13 '21 at 15:11
  • Hi, sorry, I have added them in with headers to hopefully make this clearer – Sophie Williams Jul 13 '21 at 15:30

1 Answers1

5

Here is a way.
Override the guide legend with a list of vectors of values for each of the aesthetics involved, shape and linetype. Note the different ways to specify what is to be removed.

I have also simplified the code a bit.

library(ggplot2)
library(dplyr)

colrs <- c("Fort Denison 1" = "grey15",
           "Fort Denison 2" = "grey50",
           "Full budget" = "black")
legnd <- list(shape = c(1, 1, NA), 
              linetype = c("blank", "blank", "solid"))

bind_rows(
  tg1 %>% mutate(col = "Fort Denison 1"),
  tg2 %>% mutate(col = "Fort Denison 2")
) %>%
ggplot(aes(x = mmslc, y = Year, colour = col)) +
  geom_point(pch = 1, size = 2) +
  geom_lineh(data = full, aes(x = Lutregalammslc, col = "Full budget"))+
  scale_colour_manual(values = colrs, 
                      guide = guide_legend(override.aes = legnd)) +
  coord_flip() +
  theme_classic(base_size = 12)

enter image description here

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66