简体   繁体   English

ggplot2:匹配绘图符号的图例符号

[英]ggplot2: legend symbols matching plot symbols

I made a ggplot where I make use of the viridis color package.我制作了一个 ggplot,在其中使用了 viridis 颜色包。 I adjusted the geom_point in the graph to different symbols.我将图中的geom_point调整为不同的符号。 The symbols are not displaying in my legend, although the colors are correctly programmed.尽管颜色已正确编程,但符号未显示在我的图例中。 How can I match my legend (including symbols and colors) with my ggplot?如何将我的图例(包括符号和颜色)与我的 ggplot 匹配?

Attempt:试图:

library(ggplot)
library(viridis)

ggplot(df, aes(`Lengte_(cm)`, verschil_lengte))+
  geom_point(aes(shape = Lengteklasse, colour = Lengteklasse), size = 3)+
  geom_hline(yintercept = 1.0, linetype="dashed", color = "red")+
  geom_hline(yintercept = 2.0, linetype="dashed", color = "red")+
  scale_shape_manual(values = c(16, 17, 15, 3, 8), guide = "none")+
  scale_color_viridis(discrete = T, option = "D")+
  scale_x_continuous(breaks = seq(7,12, by = 0.5))+
  scale_y_continuous(breaks = seq(0,3, by = 0.5))+
  labs(x = "Lengte (cm)", y = "Verschaling (mm)")+
  guides(col = guide_legend("Lengteklasse (cm)"))+
  theme_classic()

current outcome:目前的结果:

在此处输入图像描述

df = df =

structure(list(`Lengte_(cm)` = c(9, 10.7, 10.7, 7.7, 9.1, 11.2, 
9.7, 10.2, 8.6, 8.9, 11.2, 11.4, 10.5, 10.5, 11.1, 8.9, 11.5, 
10.4, 9.1, 9.2, 10.1, 7.8, 9.8, 8.2, 10.1, 10.5, 10.2, 7.9, 9.3, 
8, 8.7, 8.9, 8.8, 9.3, 8.5, 7.7, 11.2, 9.4, 9.7, 11.2, 11, 10.7, 
9), Lengteklasse = structure(c(4L, 5L, 5L, 2L, 4L, 6L, 4L, 5L, 
3L, 3L, 6L, 6L, 5L, 5L, 6L, 3L, 6L, 5L, 4L, 4L, 5L, 2L, 4L, 3L, 
5L, 5L, 5L, 2L, 4L, 3L, 3L, 3L, 3L, 4L, 3L, 2L, 6L, 4L, 4L, 6L, 
6L, 5L, 4L), .Label = c("6", "7", "8", "9", "10", "11", "12", 
"13"), class = "factor"), verschil_lengte = c(0, 1.4, 1.8, 1.8, 
1.4, 0.800000000000001, 0.600000000000001, 0.600000000000001, 
1.4, 1.9, 1.3, 1.5, 0.300000000000001, 0.5, 0.9, 2.2, 1, 1, 1.4, 
2.1, 1.3, 2.2, 0.899999999999999, 2.3, 1.1, 0.699999999999999, 
2.1, 0.4, 0.5, 0.9, 2.1, 1.6, 1.7, 0.799999999999999, 2, 2.1, 
0.5, 0.799999999999999, 1.3, 0.4, 0.300000000000001, 1.6, 0.199999999999999
)), row.names = c(NA, -43L), class = c("tbl_df", "tbl", "data.frame"
), na.action = structure(c(`1` = 1L, `2` = 2L, `3` = 3L, `4` = 4L, 
`5` = 5L, `6` = 6L, `7` = 7L, `8` = 8L, `9` = 9L, `10` = 10L, 
`11` = 11L, `12` = 12L, `13` = 13L, `14` = 14L, `15` = 15L, `16` = 16L, 
`17` = 17L, `18` = 18L, `19` = 19L, `20` = 20L, `21` = 21L, `22` = 22L, 
`23` = 23L, `24` = 24L, `25` = 25L, `26` = 26L, `27` = 27L, `28` = 28L, 
`29` = 29L, `30` = 30L, `31` = 31L, `32` = 32L, `33` = 33L, `34` = 34L, 
`35` = 35L, `36` = 36L, `37` = 37L, `38` = 38L, `39` = 39L, `40` = 40L, 
`41` = 41L, `42` = 42L, `43` = 43L, `44` = 44L, `45` = 45L, `46` = 46L, 
`47` = 47L, `48` = 48L, `49` = 49L, `50` = 50L, `51` = 51L, `52` = 52L, 
`53` = 53L, `54` = 54L, `55` = 55L, `56` = 56L, `57` = 57L, `58` = 58L, 
`59` = 59L, `60` = 60L, `61` = 61L, `62` = 62L, `63` = 63L, `64` = 64L, 
`65` = 65L, `66` = 66L, `67` = 67L, `68` = 68L, `69` = 69L, `70` = 70L, 
`71` = 71L, `72` = 72L, `73` = 73L, `74` = 74L, `75` = 75L, `76` = 76L, 
`77` = 77L, `78` = 78L, `79` = 79L, `80` = 80L, `81` = 81L, `82` = 82L, 
`83` = 83L, `84` = 84L, `85` = 85L, `86` = 86L, `87` = 87L, `88` = 88L, 
`89` = 89L, `90` = 90L, `91` = 91L, `92` = 92L, `93` = 93L, `94` = 94L, 
`95` = 95L, `96` = 96L, `97` = 97L, `98` = 98L, `99` = 99L, `100` = 100L, 
`101` = 101L, `102` = 102L, `103` = 103L, `104` = 104L, `105` = 105L, 
`106` = 106L, `107` = 107L, `108` = 108L, `109` = 109L, `110` = 110L, 
`111` = 111L, `112` = 112L, `113` = 113L, `114` = 114L, `115` = 115L, 
`116` = 116L, `117` = 117L, `118` = 118L, `119` = 119L, `120` = 120L, 
`121` = 121L, `122` = 122L, `123` = 123L, `124` = 124L, `125` = 125L, 
`126` = 126L, `127` = 127L, `128` = 128L, `129` = 129L, `130` = 130L, 
`131` = 131L, `132` = 132L, `133` = 133L, `134` = 134L, `135` = 135L, 
`136` = 136L, `137` = 137L, `138` = 138L, `139` = 139L, `140` = 140L, 
`141` = 141L, `142` = 142L, `143` = 143L, `144` = 144L, `145` = 145L, 
`146` = 146L, `147` = 147L, `148` = 148L, `149` = 149L, `150` = 150L, 
`151` = 151L, `152` = 152L, `153` = 153L, `154` = 154L, `155` = 155L, 
`156` = 156L, `157` = 157L, `158` = 158L, `159` = 159L, `160` = 160L, 
`161` = 161L, `162` = 162L, `163` = 163L, `164` = 164L, `165` = 165L, 
`166` = 166L, `167` = 167L, `168` = 168L, `169` = 169L, `170` = 170L, 
`171` = 171L, `172` = 172L, `173` = 173L, `174` = 174L, `175` = 175L, 
`176` = 176L, `177` = 177L, `178` = 178L, `179` = 179L, `180` = 180L, 
`181` = 181L, `182` = 182L, `183` = 183L, `184` = 184L, `185` = 185L, 
`186` = 186L, `187` = 187L, `188` = 188L, `189` = 189L, `190` = 190L, 
`191` = 191L, `192` = 192L, `193` = 193L, `194` = 194L, `195` = 195L, 
`196` = 196L, `197` = 197L, `198` = 198L, `199` = 199L, `200` = 200L, 
`201` = 201L, `202` = 202L, `203` = 203L, `204` = 204L, `205` = 205L, 
`206` = 206L, `207` = 207L, `208` = 208L, `209` = 209L, `210` = 210L, 
`211` = 211L, `212` = 212L, `213` = 213L, `214` = 214L, `215` = 215L, 
`216` = 216L, `217` = 217L, `218` = 218L, `219` = 219L, `220` = 220L, 
`221` = 221L, `222` = 222L, `223` = 223L, `224` = 224L, `225` = 225L, 
`226` = 226L, `227` = 227L, `228` = 228L, `229` = 229L, `230` = 230L, 
`231` = 231L, `232` = 232L, `233` = 233L, `234` = 234L, `235` = 235L, 
`236` = 236L, `237` = 237L, `238` = 238L, `239` = 239L, `240` = 240L, 
`241` = 241L, `242` = 242L, `243` = 243L, `244` = 244L, `245` = 245L, 
`246` = 246L, `247` = 247L, `248` = 248L, `249` = 249L, `250` = 250L, 
`251` = 251L, `252` = 252L, `253` = 253L, `254` = 254L, `255` = 255L, 
`256` = 256L, `257` = 257L, `258` = 258L, `259` = 259L, `260` = 260L, 
`261` = 261L, `262` = 262L, `263` = 263L, `264` = 264L, `265` = 265L, 
`266` = 266L, `267` = 267L, `268` = 268L, `269` = 269L, `270` = 270L, 
`271` = 271L, `272` = 272L, `273` = 273L, `274` = 274L, `275` = 275L, 
`277` = 277L, `278` = 278L, `279` = 279L, `280` = 280L, `281` = 281L, 
`282` = 282L, `284` = 284L, `285` = 285L, `286` = 286L, `288` = 288L, 
`289` = 289L, `290` = 290L, `291` = 291L, `292` = 292L, `293` = 293L, 
`294` = 294L, `295` = 295L, `296` = 296L, `297` = 297L, `298` = 298L, 
`300` = 300L, `301` = 301L, `302` = 302L, `303` = 303L, `304` = 304L, 
`305` = 305L, `306` = 306L, `308` = 308L, `309` = 309L, `310` = 310L, 
`311` = 311L, `312` = 312L, `313` = 313L, `314` = 314L, `315` = 315L, 
`316` = 316L, `317` = 317L, `318` = 318L, `319` = 319L, `321` = 321L, 
`322` = 322L, `323` = 323L, `324` = 324L, `325` = 325L, `326` = 326L, 
`327` = 327L, `328` = 328L, `329` = 329L, `330` = 330L, `331` = 331L, 
`333` = 333L, `334` = 334L, `335` = 335L, `336` = 336L, `337` = 337L, 
`338` = 338L, `339` = 339L, `340` = 340L, `341` = 341L, `342` = 342L, 
`343` = 343L, `344` = 344L, `345` = 345L, `346` = 346L, `347` = 347L, 
`348` = 348L, `349` = 349L, `351` = 351L, `352` = 352L, `354` = 354L, 
`356` = 356L, `357` = 357L, `358` = 358L, `359` = 359L, `360` = 360L, 
`361` = 361L, `362` = 362L, `363` = 363L, `364` = 364L, `366` = 366L, 
`368` = 368L, `369` = 369L, `370` = 370L, `371` = 371L, `372` = 372L, 
`373` = 373L, `374` = 374L, `375` = 375L, `376` = 376L, `377` = 377L, 
`378` = 378L, `379` = 379L, `380` = 380L, `382` = 382L, `383` = 383L, 
`384` = 384L, `387` = 387L, `388` = 388L, `390` = 390L, `391` = 391L, 
`392` = 392L, `393` = 393L, `394` = 394L, `395` = 395L, `396` = 396L, 
`397` = 397L, `399` = 399L, `400` = 400L, `401` = 401L, `402` = 402L, 
`404` = 404L, `405` = 405L, `406` = 406L, `407` = 407L, `408` = 408L, 
`409` = 409L, `410` = 410L, `411` = 411L, `412` = 412L, `413` = 413L, 
`414` = 414L, `415` = 415L, `416` = 416L, `417` = 417L, `419` = 419L, 
`420` = 420L, `423` = 423L, `424` = 424L, `425` = 425L, `426` = 426L, 
`427` = 427L, `429` = 429L, `430` = 430L, `431` = 431L, `432` = 432L, 
`433` = 433L, `434` = 434L, `435` = 435L, `436` = 436L, `437` = 437L, 
`438` = 438L, `439` = 439L, `440` = 440L, `441` = 441L, `442` = 442L, 
`443` = 443L, `444` = 444L, `446` = 446L, `447` = 447L, `448` = 448L, 
`450` = 450L, `451` = 451L, `452` = 452L, `453` = 453L, `454` = 454L, 
`455` = 455L, `456` = 456L, `457` = 457L, `459` = 459L, `460` = 460L, 
`462` = 462L, `463` = 463L, `464` = 464L, `465` = 465L, `466` = 466L, 
`467` = 467L, `468` = 468L, `469` = 469L, `470` = 470L, `471` = 471L, 
`472` = 472L, `473` = 473L, `474` = 474L, `475` = 475L, `476` = 476L, 
`478` = 478L, `479` = 479L, `480` = 480L, `481` = 481L, `482` = 482L, 
`483` = 483L, `484` = 484L, `485` = 485L, `486` = 486L, `487` = 487L, 
`488` = 488L, `489` = 489L, `490` = 490L, `491` = 491L, `493` = 493L, 
`495` = 495L, `496` = 496L, `497` = 497L, `498` = 498L, `499` = 499L, 
`500` = 500L, `501` = 501L, `502` = 502L, `503` = 503L, `504` = 504L, 
`505` = 505L, `506` = 506L, `507` = 507L, `508` = 508L, `509` = 509L, 
`510` = 510L, `511` = 511L, `512` = 512L, `513` = 513L, `514` = 514L, 
`515` = 515L, `516` = 516L, `517` = 517L, `518` = 518L, `519` = 519L, 
`520` = 520L, `521` = 521L, `522` = 522L, `523` = 523L, `524` = 524L, 
`525` = 525L, `526` = 526L, `527` = 527L, `528` = 528L, `529` = 529L, 
`530` = 530L, `531` = 531L, `532` = 532L, `533` = 533L, `535` = 535L, 
`536` = 536L, `537` = 537L, `538` = 538L, `539` = 539L, `540` = 540L, 
`542` = 542L, `543` = 543L, `544` = 544L, `545` = 545L, `546` = 546L, 
`547` = 547L, `548` = 548L, `549` = 549L, `550` = 550L, `551` = 551L, 
`553` = 553L, `554` = 554L, `555` = 555L, `556` = 556L, `557` = 557L, 
`558` = 558L, `559` = 559L, `560` = 560L, `561` = 561L, `562` = 562L, 
`563` = 563L, `564` = 564L, `565` = 565L, `566` = 566L, `567` = 567L, 
`568` = 568L, `569` = 569L, `570` = 570L, `571` = 571L, `572` = 572L, 
`573` = 573L, `574` = 574L, `575` = 575L, `576` = 576L, `577` = 577L, 
`578` = 578L, `579` = 579L, `580` = 580L, `581` = 581L, `582` = 582L, 
`583` = 583L, `584` = 584L, `585` = 585L, `586` = 586L, `587` = 587L, 
`588` = 588L, `589` = 589L, `590` = 590L, `591` = 591L, `593` = 593L, 
`595` = 595L, `596` = 596L, `597` = 597L, `598` = 598L, `599` = 599L, 
`601` = 601L, `602` = 602L, `603` = 603L, `604` = 604L, `605` = 605L, 
`606` = 606L, `608` = 608L, `609` = 609L, `610` = 610L, `611` = 611L, 
`612` = 612L, `614` = 614L, `615` = 615L, `616` = 616L, `617` = 617L, 
`618` = 618L, `619` = 619L, `620` = 620L, `621` = 621L, `622` = 622L, 
`623` = 623L, `624` = 624L, `625` = 625L, `626` = 626L, `627` = 627L, 
`628` = 628L, `629` = 629L, `631` = 631L, `632` = 632L, `633` = 633L, 
`634` = 634L, `635` = 635L, `636` = 636L, `637` = 637L, `638` = 638L, 
`639` = 639L, `640` = 640L, `641` = 641L, `642` = 642L, `643` = 643L, 
`645` = 645L, `646` = 646L, `647` = 647L, `648` = 648L, `649` = 649L, 
`650` = 650L, `651` = 651L, `652` = 652L, `653` = 653L, `654` = 654L, 
`655` = 655L, `657` = 657L, `658` = 658L, `659` = 659L, `661` = 661L, 
`662` = 662L, `663` = 663L, `664` = 664L, `666` = 666L, `667` = 667L, 
`668` = 668L, `669` = 669L, `670` = 670L, `671` = 671L, `672` = 672L, 
`673` = 673L, `675` = 675L, `677` = 677L, `678` = 678L, `679` = 679L, 
`680` = 680L, `681` = 681L, `682` = 682L, `683` = 683L, `684` = 684L, 
`685` = 685L, `686` = 686L, `687` = 687L, `688` = 688L, `689` = 689L, 
`690` = 690L, `691` = 691L, `692` = 692L, `693` = 693L, `696` = 696L, 
`697` = 697L, `698` = 698L), class = "omit"))

Your guide="none" implies you don't want a legend for shape.您的guide="none"意味着您不想要形状图例。 That's why the shapes don't appear.这就是形状不出现的原因。 To combine two legends, give them the same name.组合两个图例,请给它们相同的名称。 Thus:因此:

library(ggplot2)  # Note typo correction
library(viridis)

ggplot(df, aes(`Lengte_(cm)`, verschil_lengte))+
  geom_point(aes(shape = Lengteklasse, colour = Lengteklasse), size = 3)+
  geom_hline(yintercept = 1.0, linetype="dashed", color = "red")+
  geom_hline(yintercept = 2.0, linetype="dashed", color = "red")+
  scale_shape_manual(values = c(16, 17, 15, 3, 8), name="Lengteklasse (cm)")+
  scale_color_viridis(discrete = T, option = "D", name="Lengteklasse (cm)")+
  scale_x_continuous(breaks = seq(7,12, by = 0.5))+
  scale_y_continuous(breaks = seq(0,3, by = 0.5))+
  labs(x = "Lengte (cm)", y = "Verschaling (mm)")+
  guides(col = guide_legend("Lengteklasse (cm)"))+
  theme_classic()

produces生产

在此处输入图像描述

[You can also do away with guides(col = guide_legend("Lengteklasse (cm)"))+ .] [你也可以取消guides(col = guide_legend("Lengteklasse (cm)"))+ .]

Here is another approach.这是另一种方法。 That of @Limey was my first thought. @Limey 是我的第一个想法。 But already posted.不过已经发了。 Anyway.反正。 The clue is:线索是:

If you want to have same color and shape in one legend then you have to give them the same name in the aesthetics!如果您想在一个图例中具有相同的颜色和形状,那么您必须在美学上给它们起相同的名称!

then to give them the same name in one column legend, we have to identify the names argument in scale_color_viridis and scale_shape_manual然后要在一列图例中给它们相同的名称,我们必须在scale_color_viridisscale_shape_manual中标识名称参数

then you could remove guides as already stated by Limey!:那么您可以删除 Limey 已经说过的指南!:

library(ggplot)
library(viridis)

ggplot(df, aes(`Lengte_(cm)`, verschil_lengte, shape = Lengteklasse, colour = Lengteklasse))+
  geom_point(size = 3)+
  geom_hline(yintercept = 1.0, linetype="dashed", color = "red")+
  geom_hline(yintercept = 2.0, linetype="dashed", color = "red")+
  scale_color_viridis(name = "Lengteklasse (cm)", 
                      discrete = T, option = "D")+
  scale_shape_manual(name = "Lengteklasse (cm)",
                     values = c(16, 17, 15, 3, 8))+
  scale_x_continuous(breaks = seq(7,12, by = 0.5))+
  scale_y_continuous(breaks = seq(0,3, by = 0.5))+
  labs(x = "Lengte (cm)", y = "Verschaling (mm)")+
  theme_classic()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM