1

I have this plot made in ggplot2

enter image description here

With the following code:

colors4 <- c("Exp" = "#000000", "TL 1" = "#56B4E9", "TL 2"="#CC79A7", "TL 3"="#009E73", "TL 4"="#D55E00", "Model"="#999999")
ggplot(binodalbetla, aes(x=w.sal,y=w.tpc,colour=Category), show.legend=FALSE)+
  geom_line(data=subset(binodalbetla,Category == "TL 1"),linewidth=1.2)+
  geom_line(data=subset(binodalbetla,Category == "TL 2"),linewidth=1.2)+
  geom_line(data=subset(binodalbetla,Category == "TL 3"),linewidth=1.2)+
  geom_line(data=subset(binodalbetla,Category == "TL 4"),linewidth=1.2)+
  geom_line(data=subset(binodalbetla,Category == "Model"),linewidth=1.2)+
  geom_point(data=subset(binodalbetla,Category == "Exp"), aes(color="Exp"), size=2, shape=5)+
  labs(
    x = expression(w[Salt]),
    y = expression(w[TPC]),
    title = bquote(Bet~":"~2~LA~"/"~K[3]~PO[4])) +
  scale_color_discrete(name="") + 
  theme(panel.background = element_rect(fill = "white", colour = "grey50"), axis.text.x= element_text(face="bold", size=20), axis.text.y= element_text(face="bold", size=20), axis.text=element_text(size=34), axis.title.x = element_text(size=28), axis.title.y = element_text(size=28), title=element_text(size=34,face="bold"), legend.title= element_blank(), legend.text=element_text(size=30)) +
  ylim(0,1)+
scale_color_manual(values = colors4)

It is fine, but the problem is that in the legend a diamond symbol appears (because I chose shape=5 in geom_point). I would like that in the legend only the diamond simbol appears (without the black line), while for the other only the coloured lines are showed, is there any option?

Here is a minimal reproducible dataset:

binodalbetla
    Category      w.sal       w.tpc
1        Exp 0.36964296 0.089970010
2        Exp 0.35235401 0.111331692
3        Exp 0.32564172 0.147805666
4        Exp 0.30792464 0.170437196
5        Exp 0.27185358 0.219397133
6        Exp 0.25057568 0.247797606
7        Exp 0.23468525 0.274395726
8        Exp 0.19402343 0.385795245
9        Exp 0.16116004 0.440709359
10       Exp 0.13430737 0.494772301
11       Exp 0.10794299 0.548779986
12       Exp 0.08918584 0.594597503
13       Exp 0.07037506 0.645092388
14       Exp 0.04556309 0.702074353
15      TL 1 0.33234165 0.195207709
16      TL 1 0.45051932 0.025008216
17      TL 1 0.15453553 0.451284098
18      TL 2 0.29572918 0.298255159
19      TL 2 0.48062042 0.014280041
20      TL 2 0.06463047 0.653192383
21      TL 3 0.34704979 0.251874003
22      TL 3 0.50379819 0.008956064
23      TL 3 0.04385440 0.726794834
24      TL 4 0.29369441 0.348022565
25      TL 4 0.51158047 0.007416504
26      TL 4 0.03488902 0.752779981
27     Model 0.00300000 0.980858995
28     Model 0.00400000 0.964341288
29     Model 0.00500000 0.950018816
30     Model 0.00600000 0.937252593
31     Model 0.00700000 0.925663241
32     Model 0.00800000 0.915003704
33     Model 0.00900000 0.905102457
34     Model 0.01000000 0.895834667
35     Model 0.01100000 0.887106136
36     Model 0.01200000 0.878843704
37     Model 0.01300000 0.870989212
38     Model 0.01400000 0.863495518
39     Model 0.01500000 0.856323778
40     Model 0.01600000 0.849441
  • 1
    Can you please update your question to give the reproducible data set using `dput()` - it makes it much easier to run your code. – nrennie Mar 11 '23 at 20:32

3 Answers3

2

You can make vectors of shape and linetype values and pass the two vectors to your scale_colur_manual by adding override_aes() argument:

make vectors of shapes and linetypes:

shapes <- c(5, NA, NA, NA, NA, NA)
linetype <- c("blank","solid","solid","solid","solid","solid")

pass vectors to your scale_color_manual in your ggplot code:

.....
    scale_color_manual(values = colors4, guide = 
    guide_legend(override.aes = list(linetype= linetype,shape = shapes)))

enter image description here

S-SHAAF
  • 1,863
  • 2
  • 5
  • 14
1

Add guides(color = guide_legend(override.aes = list(linetype= 0)))

see Remove lines from color and fill legends

your code w/ additon:

ggplot(binodalbetla, aes(x=w.sal,y=w.tpc,colour=Category), show.legend=FALSE)+
  geom_line(data=subset(binodalbetla,Category == "TL 1"),lwd=1.2)+ # change "linewidth" to "lwd"
  geom_line(data=subset(binodalbetla,Category == "TL 2"),lwd=1.2)+
  geom_line(data=subset(binodalbetla,Category == "TL 3"),lwd=1.2)+
  geom_line(data=subset(binodalbetla,Category == "TL 4"),lwd=1.2)+
  geom_line(data=subset(binodalbetla,Category == "Model"),lwd=1.2)+
  geom_point(data=subset(binodalbetla,Category == "Exp"), aes(color="Exp"), size=2, shape=5)+
  labs(
    x = expression(w[Salt]),
    y = expression(w[TPC]),
    title = bquote(Bet~":"~2~LA~"/"~K[3]~PO[4])) +
  scale_color_discrete(name="") +
  theme(panel.background = element_rect(fill = "white", colour = "grey50"), 
        axis.text.x= element_text(face="bold", size=20), 
        axis.text.y= element_text(face="bold", size=20), 
        axis.text=element_text(size=34), 
        axis.title.x = element_text(size=28), 
        axis.title.y = element_text(size=28), 
        title=element_text(size=34,face="bold"), 
        legend.title= element_blank(), 
        legend.text=element_text(size=30)) +
  ylim(0,1)+
  guides(color = guide_legend(override.aes = list(linetype= c(0,1,1,1,1,1)))) # Note the use of linetypes for each unique legend item here so as to not delete all lines just the black one
dandrews
  • 967
  • 5
  • 18
  • 1
    Note that I just hacked your data by transferring it to text and then reading into R and it didn't look exactly like your example, if you use `dput()` as suggested I could see if this is exactly what you are looking for – dandrews Mar 11 '23 at 20:54
  • Sorry, I am pretty new to R and I don´t know how to use dput(). – David Moldes Mar 12 '23 at 03:07
  • 1
    Just pass your data to the `dput(data)` function then copy and paste the result to your post as code. If your data is very large you can subset the data like this: `dput(data[1:4,]` – dandrews Mar 12 '23 at 03:09
  • 1
    Also, I just checked using the data from @Rui Barradas below, and there is no line under the black diamonds unlike the plot you have posted in your example. It is worth noting this omission from each of the answers here. Also instead of passing `linewidth` to each geom, use `lwd` otherwise you get a warning message and no effect on your actual line width. I am editing my answer to show this. – dandrews Mar 12 '23 at 03:18
1

Here is a way.

  • It is much simpler to map Category to the aesthetics required and to call geom_line just once. Then set the colours with scale_color_manual to remove the legend line corresponding to category "Exp";
  • subset the data only to plot the points but you must also map shape to Category and set the legend shapes with the appropriate scale_shape_manual values.
library(ggplot2)

colours <- c(Exp = "transparent", Model = "#999999", `TL 1` = "#56b4e9", 
             `TL 2` = "#cc79a7", `TL 3` = "#009e73", `TL 4` = "#d55e00")
shapes <- c(Exp = 5L, Model = NA, `TL 1` = NA, `TL 2` = NA, `TL 3` = NA, `TL 4` = NA)

ggplot(binodalbetla, aes(w.sal, w.tpc, colour = Category, shape = Category)) +
  geom_line(linewidth = 1.2) +
  geom_point(
    data = subset(binodalbetla, Category == "Exp"),
    size = 2, stroke = 1.2, colour = "black"
  ) +
  scale_color_manual(name = "Category", values = colours) +
  scale_shape_manual(name = "Category", values = shapes) +
  labs(x = expression(W[Salt]), y = expression(w[TPC])) +
  theme_bw() +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank()
  ) 

Created on 2023-03-11 with reprex v2.0.2


Data

binodalbetla <- read.table(text = "
Category      w.sal       w.tpc
1        Exp 0.36964296 0.089970010
2        Exp 0.35235401 0.111331692
3        Exp 0.32564172 0.147805666
4        Exp 0.30792464 0.170437196
5        Exp 0.27185358 0.219397133
6        Exp 0.25057568 0.247797606
7        Exp 0.23468525 0.274395726
8        Exp 0.19402343 0.385795245
9        Exp 0.16116004 0.440709359
10       Exp 0.13430737 0.494772301
11       Exp 0.10794299 0.548779986
12       Exp 0.08918584 0.594597503
13       Exp 0.07037506 0.645092388
14       Exp 0.04556309 0.702074353
15      'TL 1' 0.33234165 0.195207709
16      'TL 1' 0.45051932 0.025008216
17      'TL 1' 0.15453553 0.451284098
18      'TL 2' 0.29572918 0.298255159
19      'TL 2' 0.48062042 0.014280041
20      'TL 2' 0.06463047 0.653192383
21      'TL 3' 0.34704979 0.251874003
22      'TL 3' 0.50379819 0.008956064
23      'TL 3' 0.04385440 0.726794834
24      'TL 4' 0.29369441 0.348022565
25      'TL 4' 0.51158047 0.007416504
26      'TL 4' 0.03488902 0.752779981
27     Model 0.00300000 0.980858995
28     Model 0.00400000 0.964341288
29     Model 0.00500000 0.950018816
30     Model 0.00600000 0.937252593
31     Model 0.00700000 0.925663241
32     Model 0.00800000 0.915003704
33     Model 0.00900000 0.905102457
34     Model 0.01000000 0.895834667
35     Model 0.01100000 0.887106136
36     Model 0.01200000 0.878843704
37     Model 0.01300000 0.870989212
38     Model 0.01400000 0.863495518
39     Model 0.01500000 0.856323778
40     Model 0.01600000 0.849441
", header = TRUE)

Created on 2023-03-11 with reprex v2.0.2

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