Search code examples
rggplot2gridextragtable

how to edit ggplot's legend words using a custom object(gtable)?


How can i edit ggplot's legend using a gTree objec? my data is

d1 <- data.frame(structure(list(g = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
                                          1L, 2L, 3L, 4L), levels = c("2000", "4000", "8000", "16000"), class = "factor"), 
                          variable = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
                                                 3L, 3L, 3L), levels = c("V1", "V2", "V3", "V4"), class = "factor"), 
                          Mean = c(0.29357, 0.26426, 0.23491, 0.2078, 0.28445202, 0.2556518, 
                                   0.22108974, 0.19075, 0.2747697, 0.24120966, 0.20709, 0.1781
                          ), SD = c(0.00834584706697326, 0.00805671224786501, 0.00709389648549643, 
                                    0.00775189246150216, 0.00982287212402248, 0.00828946965143372, 
                                    0.00721378264599603, 0.0083178257068657, 0.00919638608922226, 
                                    0.00836673216780334, 0.00625861854744711, 0.0080070122329093
                          ), ymin = c(0.291256649218125, 0.262026793301103, 0.232943672214507, 
                                      0.20565128503519, 0.281729258760067, 0.253354076240436, 0.21909018146622, 
                                      0.188444416342875, 0.272220591829289, 0.238890520163528, 
                                      0.205355199446622, 0.175880569442402), ymax = c(0.295883350781875, 
                                                                                      0.266493206698897, 0.236876327785493, 0.20994871496481, 0.287174781239933, 
                                                                                      0.257949523759564, 0.22308929853378, 0.193055583657125, 0.277318808170711, 
                                                                                      0.243528799836472, 0.208824800553378, 0.180319430557598))))
                                

and i can use following code to plot

library(ggplot2)
ts = 25
bs = 40
colorManualunit<- c("#377EB8", "#FF7F00", "#984EA3")
p <- ggplot(d1, aes(x=g, y=Mean, colour=variable, group=variable, shape=variable)) +
  geom_errorbar(aes(ymin=ymin, ymax=ymax), position=position_dodge(width=0.1), width=0.5) +
  geom_point(position=position_dodge(width=0.1), size=3) +
  geom_line(aes(group=variable), position=position_dodge(width=0.1), size=1.5, linetype= 2) +
  theme_bw()+
  theme(panel.grid=element_blank(),
        # legend.position='none', 
        axis.text.x = element_text(size = ts), 
        axis.text.y = element_text(size = ts),
        axis.title.x = element_text(size = bs), 
        axis.title.y = element_text(size = bs))+
  scale_x_discrete(expand=expansion(add=0.3))+
  scale_y_continuous(breaks=c(0.17,0.21,0.25,0.29),expand=expansion(add=0.01)) +
  scale_color_manual(values =  colorManualunit)+
  labs(x='n', y = "CDF Approx. Err.")
p

and get picture enter image description here

now i want to change the legend words use gtree object

library(grid)
library(gtable)
dx<-0.3
tuvp<-viewport(h=0.6,w=0.6)

gTree(children=gList(
  pointsGrob(x=c(0.2,0.5,0.8),y=c(0.8,0.2,0.8),default='npc',pch=20,gp=gpar(cex=dx)),
  linesGrob(x=c(0.2,0.5,0.8),y=c(0.8,0.2,0.8))
),vp=tuvp)->v1

gTree(children=gList(
  pointsGrob(x=c(0.2,0.5,0.8),y=c(0.2,0.8,0.2),default='npc',pch=20,gp=gpar(cex=dx)),
  linesGrob(x=c(0.2,0.5,0.8,0.2),y=c(0.2,0.8,0.2,0.2))
),vp=tuvp)->v2


gTree(children=gList(
  pointsGrob(x=c(0.5,0.5,0.2,0.8),y=c(0.2,0.6,0.8,0.8),default='npc',pch=20,gp=gpar(cex=dx)),
  linesGrob(x=c(0.5,0.5,0.2),y=c(0.2,0.6,0.8)),
  linesGrob(x=c(0.5,0.8),y=c(0.6,0.8))
),vp=tuvp)->v3

How can i get following picture's legend? enter image description here

I try use ggplotGrob but failed,i can not change the words.

ggplotGrob(p)->pg 

pg$grobs[[15]]$grob[[1]]$grob[[13]] <- v1
pg$grobs[[15]]$grob[[1]]$grob[[14]] <- v2
pg$grobs[[15]]$grob[[1]]$grob[[15]] <- v3
grid.draw(pg)

but i can change the xlab and ylab use ggplotGrob

ggplotGrob(p)->pg 
pg$grobs[[12]]<-v1
pg$grobs[[13]]<-v3
grid.draw(pg)

enter image description here

you can use any other way to complete not just ggplotGrob


Solution

  • Just a small tweak to the assignments, e.g.

    pg[["grobs"]][[15]][[1]][[1]][[1]][[13]] <- v1

    library(tidyverse)
    library(grid)
    library(gtable)
    
    d1 <- data.frame(structure(list(
      g = structure(c(
        1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
        1L, 2L, 3L, 4L
      ), levels = c("2000", "4000", "8000", "16000"), class = "factor"),
      variable = structure(c(
        1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
        3L, 3L, 3L
      ), levels = c("V1", "V2", "V3", "V4"), class = "factor"),
      Mean = c(
        0.29357, 0.26426, 0.23491, 0.2078, 0.28445202, 0.2556518,
        0.22108974, 0.19075, 0.2747697, 0.24120966, 0.20709, 0.1781
      ), SD = c(
        0.00834584706697326, 0.00805671224786501, 0.00709389648549643,
        0.00775189246150216, 0.00982287212402248, 0.00828946965143372,
        0.00721378264599603, 0.0083178257068657, 0.00919638608922226,
        0.00836673216780334, 0.00625861854744711, 0.0080070122329093
      ), ymin = c(
        0.291256649218125, 0.262026793301103, 0.232943672214507,
        0.20565128503519, 0.281729258760067, 0.253354076240436, 0.21909018146622,
        0.188444416342875, 0.272220591829289, 0.238890520163528,
        0.205355199446622, 0.175880569442402
      ), ymax = c(
        0.295883350781875,
        0.266493206698897, 0.236876327785493, 0.20994871496481, 0.287174781239933,
        0.257949523759564, 0.22308929853378, 0.193055583657125, 0.277318808170711,
        0.243528799836472, 0.208824800553378, 0.180319430557598
      )
    )))
    
    ts = 25
    bs = 40
    colorManualunit<- c("#377EB8", "#FF7F00", "#984EA3")
    p <- ggplot(d1, aes(x=g, y=Mean, colour=variable, group=variable, shape=variable)) +
      geom_errorbar(aes(ymin=ymin, ymax=ymax), position=position_dodge(width=0.1), width=0.5) +
      geom_point(position=position_dodge(width=0.1), size=3) +
      geom_line(aes(group=variable), position=position_dodge(width=0.1), size=1.5, linetype= 2) +
      theme_bw()+
      theme(panel.grid=element_blank(),
            # legend.position='none', 
            axis.text.x = element_text(size = ts), 
            axis.text.y = element_text(size = ts),
            axis.title.x = element_text(size = bs), 
            axis.title.y = element_text(size = bs))+
      scale_x_discrete(expand=expansion(add=0.3))+
      scale_y_continuous(breaks=c(0.17,0.21,0.25,0.29),expand=expansion(add=0.01)) +
      scale_color_manual(values =  colorManualunit)+
      labs(x='n', y = "CDF Approx. Err.")
    #> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
    #> ℹ Please use `linewidth` instead.
    #> This warning is displayed once every 8 hours.
    #> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
    #> generated.
    p
    
    dx<-0.3
    tuvp<-viewport(h=0.6,w=0.6)
    
    gTree(children=gList(
      pointsGrob(x=c(0.2,0.5,0.8),y=c(0.8,0.2,0.8),default='npc',pch=20,gp=gpar(cex=dx)),
      linesGrob(x=c(0.2,0.5,0.8),y=c(0.8,0.2,0.8))
    ),vp=tuvp)->v1
    
    gTree(children=gList(
      pointsGrob(x=c(0.2,0.5,0.8),y=c(0.2,0.8,0.2),default='npc',pch=20,gp=gpar(cex=dx)),
      linesGrob(x=c(0.2,0.5,0.8,0.2),y=c(0.2,0.8,0.2,0.2))
    ),vp=tuvp)->v2
    
    
    gTree(children=gList(
      pointsGrob(x=c(0.5,0.5,0.2,0.8),y=c(0.2,0.6,0.8,0.8),default='npc',pch=20,gp=gpar(cex=dx)),
      linesGrob(x=c(0.5,0.5,0.2),y=c(0.2,0.6,0.8)),
      linesGrob(x=c(0.5,0.8),y=c(0.6,0.8))
    ),vp=tuvp)->v3
    
    
    ggplotGrob(p) -> pg 
    pg[["grobs"]][[15]][[1]][[1]][[1]][[13]] <- v1
    pg[["grobs"]][[15]][[1]][[1]][[1]][[14]] <- v2
    pg[["grobs"]][[15]][[1]][[1]][[1]][[15]] <- v3
    grid.draw(pg)
    

    Created on 2024-04-02 with reprex v2.1.0