R语言 更改ggplot2中的图例符号

hwazgwia  于 2023-03-15  发布在  其他
关注(0)|答案(3)|浏览(290)

我在ggplot2中绘制了此图

使用以下代码:

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)

这很好,但问题是在图例中出现了一个菱形符号(因为我在geom_point中选择了shape=5)。我希望在图例中只出现菱形符号(没有黑线),而对于其他图例,只显示彩色线,有什么选择吗?
以下是最小可重现数据集:

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
7uzetpgm

7uzetpgm1#

您可以创建shapelinetype值的向量,并通过添加override_aes()参数将这两个向量传递给scale_colur_manual
生成形状和线型的矢量:

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

将向量传递到ggplot代码中的scale_color_manual

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

qmelpv7a

qmelpv7a2#

这里有一个办法。

  • CategoryMap到所需的美学上,并且只调用geom_line一次,然后用scale_color_manual设置颜色,以移除类别"Exp"对应的图例行,这要简单得多;
  • 子集化数据仅用于绘制点,但您还必须将shapeMap到Category,并使用适当的scale_shape_manual值设置图例形状。
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()
  )

创建于2023年3月11日,使用reprex v2.0.2

数据

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)

创建于2023年3月11日,使用reprex v2.0.2

ajsxfq5m

ajsxfq5m3#

添加参考线(颜色= guide_legend(替代.aes =列表(线型= 0)))
参见Remove lines from color and fill legends
您的代码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

相关问题