如何使用gTree对象编辑gplot的图例? 我的数据

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

我可以用下面的代码

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

现在我想改变图例单词使用gtree对象

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

我试着用了ggplotGrob个,但是失败了,我不能改变单词.

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)

但我可以改变xlab和ylab使用ggplotGrob

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

enter image description here

你可以使用任何其他方法来完成,而不仅仅是ggplotGrob

推荐答案

只是一个小的调整,例如.

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)

创建于2024—04—02,reprex v2.1.0

R相关问答推荐

在ubuntu 22.04上更新到R4.4后包安装出现编译错误

DT::可数据的正规表达OR运算符问题

是否有任何解决方案可以优化VSCode中RScript的图形绘制?

按块将载体转换为矩阵-reshape

MCMC和零事件二元逻辑回归

R中的子集文件—读取文件名索引为4位数字序列,例如0001到4000,而不是1到4000)

在R中替换函数中的特定符号

如何计算多个日期是否在一个日期范围内

从所有项的 struct 相同的两级列表中,将该第二级中的所有同名项绑定在一起

DEN扩展包中的RECT树形图出现异常行为

Geom_arcbar()中出错:找不到函数";geom_arcbar";

在R中,如何从一系列具有索引名的变量快速创建数据帧?

如何阻止围堵地理密度图?

对R中的列表列执行ROW Mean操作

R将函数参数传递给ggploy

R:使用ApexCharge更改标签在饼图中的位置

通过匹配另一个表(查找表)中的列值来填充数据表,并在另一个变量上进行内插

如何在R中使用因子行求和?

在使用ggplot2的情况下,如何在使用coord_trans函数的同时,根据未转换的坐标比来定位geom_瓷砖?

如何在一个GGPLATE中绘制多个灰度平滑?