R 如何将形状指定给第二个图例

R 如何将形状指定给第二个图例,r,ggplot2,R,Ggplot2,我试图将ggplot中的点形状更改为不同的形状,而不是统一的圆形,但保留不同的颜色,我认为它比圆形更好的视觉标识符,我尝试在geom_point中使用,正如@BenNorris(赞赏)所建议的: shape=factor(Var2)) 我得到这个图,我想在第二个图例中实现这些形状,我用红色圈出了它们: 这是我的代码: library(repr, warn.conflicts = FALSE) options(repr.plot.width=3.5, repr.plot.height=8.5)

我试图将ggplot中的点形状更改为不同的形状,而不是统一的圆形,但保留不同的颜色,我认为它比圆形更好的视觉标识符,我尝试在geom_point中使用,正如@BenNorris(赞赏)所建议的:

shape=factor(Var2))

我得到这个图,我想在第二个图例中实现这些形状,我用红色圈出了它们:

这是我的代码:

library(repr, warn.conflicts = FALSE)
options(repr.plot.width=3.5, repr.plot.height=8.5)


zmapp023 <- ggplot(plot_frame, aes(Var2, Var1, fill = value)) + 

  geom_tile(color = "white", position = position_dodge(), show.legend = TRUE) +

  geom_point(data = data.frame(Var2 = 1:4, Var1 = -1, value = 0), size = 5,
           aes(color = factor(Var2), shape = factor(Var2))) +
  geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 1), alpha = 0) +

  scale_color_manual(values = c("black", "forestgreen", "#DE2D29", "#3C57A8"),
                     labels = c(expression(
                         CD44^{lo}~"T Cells",
                         CD44^{hi}~CD69^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{hi}~"T Cells")),
                     guide = guide_legend(override.aes = list(fill = NA), 
                                          label.hjust = 0, position="bottom", size = 5)) +
scale_shape_manual(values = c(0,1,2,8)) +

  scale_y_discrete(position = "right") +
  labs(y = "", fill = "", color = " ", x = "")  +


  scale_fill_gradientn(colors = c("#3C57A8", "white", "#DE2D29"),
                       breaks = c(1.5, 0, -1.5),
                       labels = c("1.0", "0", "-1.0"),
                       limits = c(-1.5, 1.5),
                       space = "Lab",
                       guide = "colourbar",
                       aesthetics = "fill") +
  theme_minimal() + guides(colour=FALSE) +


theme (panel.grid = element_blank(), 
       axis.text.y.right = element_text(margin = margin(l = unit(-5, "cm"))),
       axis.text.y = element_text(face="italic", size=7, 
                                 color="black"),

       legend.justification = c(-0.9, 0),
       legend.direction = "vertical",
       legend.key.size = unit(0.6, "cm"),
       legend.key.width = unit(0.2,"cm"),
       legend.title.align = 0.5,
       axis.text.x = element_blank(),plot.margin = unit(c(1,1,5,1), "lines")) +
guides(
        fill = guide_colourbar(
            title = "Relative gene expression \n (z score)",
            title.position = "right",

            title.theme = element_text(angle = -90, size = 7.5),
            direction = "vertical",
            ticks = FALSE)) +

coord_cartesian(clip = 'off') 

legend1 <- ggplot(plot_frame, aes(Var2, Var1)) +
  geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 0), size = 5,
           aes(color = factor(Var2))) +
  geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 1), alpha = 0) +
scale_shape_manual(values = c(0,1,2,8)) +
  scale_color_manual(values = c("black", "forestgreen", "#DE2D29", "#3C57A8"),
                     labels = c(expression(
                         CD44^{lo}~"T Cells",
                         CD44^{hi}~CD69^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{hi}~"T Cells")),
                     guide = guide_legend(override.aes = list(size = 3, 
                    scale_shape_manual(values = c(0,1,2,8)),   
                    scale_color_manual(values = c("black", "forestgreen", "#DE2D29", "#3C57A8"),
                    labels = c(
                         CD44^{lo}~"T Cells",
                         CD44^{hi}~CD69^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{hi}~"T Cells"))), 
                                                              label.hjust = 0)) + 
theme (panel.grid = element_blank(), 
       axis.text.y.right = element_text(margin = margin(l = unit(-5, "cm"))),
       axis.text.y = element_text(face="italic", size=5, 
                                 color="black"),
       legend.justification = c(-0.5, 0),
       legend.direction = "vertical",
       legend.key = element_blank(),
       legend.title = element_blank(),
       axis.text.x = element_blank())


legend <- cowplot::get_legend(legend1)

zmapp023 + annotation_custom(legend$grobs[[1]], xmin = 6, ymax = -13)


我尝试了不同的线来分配它们,您可以通过将形状分配给不同的几何点来查看我的尝试。

编辑:有更好的方法

库(tidyverse)
图书馆(repr)

zmapp023非常感谢@Tung!这很有帮助。我有一个相关的问题:
structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 
34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 
47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 
30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 
43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 
56L, 57L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 
52L, 53L, 54L, 55L, 56L, 57L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 
48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L), .Label = c("Sdc4", 
"Ramp1", "Ptpn5", "Prdm1", "Pmaip1", "Myo1e", "Lamc1", "Itgb8", 
"Itgav", "Foxp3", "Dst", "Csf1", "Bmpr2", "Xcl1", "Vdr", "Tnfsf11", 
"Spry1", "Rbpj", "Ptprs", "Nrp1", "Il5", "Il4", "Il13", "Igfbp7", 
"Ifng", "Furin", "Ern1", "Vim", "Tiam1", "Podnl1", "Penk", "Maf", 
"Lmna", "Il2ra", "Il1rl1", "Il10", "Id2", "Ehd1", "Csda", "Cd44", 
"Ccr2", "Capg", "Calca", "Bcl2l1", "Areg", "Adam8", "S100a6", 
"S100a4", "Rora", "Mki67", "Lgals1", "Il2rb", "Hist1h1b", "Anxa2", 
"Itgb1", "Esm1", "S100a10"), class = "factor"), Var2 = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L), value = c(-0.716873478395867, -0.919984230086674, 
-0.661176149944064, -0.828193431806995, -0.990890861695874, -0.985855800232094, 
-0.823026349552051, -0.608384425062877, -0.809200195330501, -0.621395373041386, 
-0.998621039674651, -0.795949946272161, -1.10479976781604, -0.499510689498092, 
-0.680578082389661, -0.850155027403529, -1.02359795075789, -0.930715054575906, 
-0.900246349411412, -1.12696248565925, -0.531673076619624, -0.634387083227473, 
-0.567814470617106, -0.704971739502355, -0.77107142771167, -1.0577165116177, 
-1.12689193292486, -1.07046845292562, -1.21970205863578, -1.06946734340346, 
-0.697966304450698, -1.31865479289137, -1.1644549788312, -0.724572318411187, 
-0.887650620239559, -0.878042971979847, -0.898835722780145, -0.966547257168572, 
-1.44089646981222, -1.17641689690512, -0.981496097411318, -0.949054521097622, 
-0.86984148513583, -1.03997550896008, -0.776245072529661, -0.926869652178528, 
-1.22014851883998, -1.11565223232092, -1.45758806540816, -1.30893449397075, 
-1.38003602497654, -1.224949334573, -1.38357974363453, -1.35339938422864, 
-0.968416032914201, -0.65165652813915, -1.4919639415255, -0.685400251755411, 
-0.802640661038347, -0.575837610054767, -0.629465888711167, -0.317915043043598, 
-0.559729515180935, -0.422625579063628, -0.503276458166707, -0.565387197269664, 
-0.414441446968764, -0.652570443296679, -0.699718481126342, -0.587483551485666, 
-0.479784066762678, -0.474739880805875, -0.163485068386743, -0.0319151401130408, 
-0.687844415173434, -0.793272191522238, -0.192431267926848, -0.478440062610096, 
-0.406493550612629, -0.516470014019787, -0.516293205774877, -0.0703527612407989, 
-0.321528054118583, -0.417459180983822, -0.410868262057476, -0.333210832661548, 
-0.61740991961346, -0.605534106803515, -0.228283398359129, -0.490846073129063, 
-0.588893105208149, -0.691611619542831, -0.768211373139653, -0.655714376336414, 
-0.691777405043065, 0.27283725214197, -0.46762324186072, -0.663659775386651, 
-0.595353641454884, -0.813248687573964, -0.672352278644248, -0.649282500217443, 
-0.722482130363645, -0.325408266036161, -0.49770614745867, 0.177675109919899, 
0.206312208622598, -0.0834329896761757, -0.400018473948618, 0.627959745919098, 
0.639468920429142, 0.881985828293655, 1.4894632640724, 0.564376769280816, 
-0.0175886218831413, 0.748001124892129, -0.237693188616826, 0.0748099685432856, 
-0.0738067160244709, 0.262707416774023, -0.204274564295389, -0.381902355833725, 
-0.047820862385917, -0.458203252153174, 0.498723523113106, 0.13206419664028, 
0.811723606016846, 1.49979274596739, 1.48431659019814, 1.43869340350301, 
1.36267422579482, 1.2280707677281, 1.09802917518633, 1.30099479431717, 
1.49960551175132, 1.4927056563283, 1.49686976804331, 1.47397253453575, 
1.43227082074503, 1.3325444408283, 1.1983987520954, 0.198402134411653, 
1.08912853827601, 1.00509721706754, -0.153425140424883, 0.852612722881043, 
0.698995996122549, -0.138755915285969, 0.296318265086462, 0.446421081491043, 
0.251052659372192, 0.50342451499526, 0.873163189823561, 0.6704650702465, 
0.474043909647662, 0.24672752839951, 1.12930018666821, 0.869584307302813, 
0.0200115056815992, 0.46498720236421, 0.447990671026386, 0.485629347319394, 
0.528646313252754, -0.0127091863060346, 0.637743076608102, 0.910524864813588, 
-0.0726857435537057, 0.859230754317892, 0.843382686575065, -0.364665784858882, 
0.351291400617603, 1.41986247931586, 0.974623726851428, 1.47470660621924, 
1.38284919037663, 1.38261262076394, 1.28287812909918, 1.4499266152937, 
1.493563316646, 1.42240835251479, 1.49404009657185, 1.15246823659118, 
1.36360436969755, 0.880559849096722, -0.520497871814678, -0.328998889961762, 
-0.425053223809833, -0.307160868594853, 0.390488791334733, 0.595489070523626, 
0.0183991764982231, -0.489492083871666, -0.451824932137866, -0.412585283406412, 
-0.252707589258521, -0.5908467120367, 0.0467001829831055, 0.345952361813286, 
1.28293458057144, 0.463784556661154, 0.681779946448773, 1.45692561131772, 
0.69432543075946, 0.956305055837712, 1.45222131165832, 1.28294398902894, 
1.19983334411133, 1.30349742116338, 1.15489996934721, 0.294895869208661, 
0.973575068519344, 1.17111204440007, 1.297680661103, 0.553790085627175, 
0.842743233449211, 1.40551605194952, 1.184364466613, 1.09756610180677, 
1.12772904683868, 0.751266682827816, 1.11533140667713, 0.825725901866236, 
0.714442943708028, 0.828305803894246, -0.145300290518389, -0.756952453676937, 
-0.473140866248911, 0.576295750132076)), class = "data.frame", row.names = c(NA, 
-228L))