gpt4 book ai didi

替换 gtable : labels and gridlines 中 ggplot 的元素

转载 作者:行者123 更新时间:2023-12-01 11:35:15 25 4
gpt4 key购买 nike

我正在学习使用 gtable 操作 ggplot 对象。 [这是我问的一个相关问题:dismantling a ggplot with grid and gtable ]

目前的问题是:

  1. 如何从一个 gtable 中获取各种轴元素并将它们放入另一个 gtable 中以替换现有元素?,尤其是:替换垂直网格线(和相应的刻度线)。

下面是一些代码和数字。

# Data
df <- structure(list(Year = c(1950, 2013, 1950, 2013), Country = structure(c(1L,
1L, 2L, 2L), .Label = c("France", "United States"), class = "factor"),
Category = c("Hourly minimum wage", "Hourly minimum wage",
"Hourly minimum wage", "Hourly minimum wage"), value = c(2.14,
9.43, 3.84, 7.25), variable = c("France (2013 euros)",
"France (2013 euros)", "United States (2013 dollars)", "United States (2013 dollars)"
), Unit = c("2013 euros", "2013 euros", "2013 dollars", "2013 dollars"
)), .Names = c("Year", "Country", "Category", "value", "variable",
"Unit"), row.names = c(NA, 4L), class = "data.frame")

# Plot data with ggplot
library(ggplot2)
p1 <- ggplot(data = df, aes(x = Year, y = value, group = variable, colour = variable, shape = variable)) +
geom_line(size = 2, show_guide = FALSE) +
geom_point(size = 4, show_guide = FALSE) +
theme(panel.grid.major.x = element_line(size = 1, colour = "darkgreen"),
panel.grid.minor.x = element_line(size = 1, colour = "darkgreen", linetype = "dotted")) +
theme(text = element_text(size = 20, colour = "darkgreen")) +
theme(axis.text = element_text(size = 20, colour = "darkgreen"))

这是 p1:

enter image description here

p2 <- ggplot(data = df, aes(x = Year, y = value, group = variable, colour = variable, shape = variable)) + 
geom_line(size = 2, show_guide = FALSE) +
geom_point(size = 4, show_guide = FALSE) +
theme(panel.grid.major.x = element_line(size = 1, colour = "darkred"),
panel.grid.minor.x = element_line(size = 1, colour = "darkred", linetype = "dotted")) +
theme(text = element_text(size = 20, colour = "darkred")) +
theme(axis.text = element_text(size = 20, colour = "darkred"))

这是p2:

enter image description here

# replace the bottom axis of p1 with that of p2
library(gtable)
g1 <- ggplot_gtable(ggplot_build(p1))
g2 <- ggplot_gtable(ggplot_build(p2))

# function to remove selected elements from gtables, keeping widths
gtable_grob_remove <- function (g, what = "guide-box") {
require(gtable)
matches <- c(grepl(pattern = what, g$layout$name))
g$layout <- g$layout[!matches, , drop = FALSE]
g$grobs <- g$grobs[!matches]
return(g)
}

# replace "axis-b"
g <- g1
pos <- c(subset(g$layout, name == "axis-b", se = t:r))
g <- gtable_grob_remove(g, what = "axis-b")
g <- gtable_add_grob(g, g2$grobs[[which(g2$layout$name == "axis-b")]],
pos$t, pos$l, pos$b, pos$r, , name = "axis-b")

# replace "xlab"
pos <- c(subset(g$layout, name == "xlab", se = t:r))
g <- gtable_grob_remove(g, what = "xlab")
g <- gtable_add_grob(g, g2$grobs[[which(g2$layout$name == "xlab")]],
pos$t, pos$l, pos$b, pos$r, , name = "xlab")

grid.newpage()
grid.draw(g)

这是带有一些 p2 元素的 p1:

enter image description here

g1 是绿色的,g2 是红色的,我想要红色底轴标签和垂直网格线替换绿色的。

我已成功识别轴标签和轴标题,但我仍在寻找垂直网格线(以及轴刻度线和轴水平线!)。我想它们在 panel 中,但如何访问它们并更改它们?

在之前的评论中,baptiste 建议使用 names(ggplotGrob(p2)[["grobs"]][[4]][["children"]]) 作为识别不同元素的方法.这将返回:

names(ggplotGrob(p2)[["grobs"]][[4]][["children"]])
[1] "grill.gTree.843" "GRID.polyline.828"
[3] "geom_point.points.830" "panel.border.zeroGrob.831"

但是,我不知道如何从这里获取它。此外,附加的数字,例如.843 随每个 session 而变化,所以我想找到一种不对这些数字进行硬编码的方法。感谢您的建议!

最佳答案

根据另一个答案,考虑这个,

# locate the children
grill1 <- grepl("grill",
names(g1[["grobs"]][[4]][["children"]]))
grill2 <- grepl("grill",
names(g2[["grobs"]][[4]][["children"]]))

# swap the kids, the parents won't notice
g1[["grobs"]][[4]][["children"]][grill1] <-
g2[["grobs"]][[4]][["children"]][grill2]


grid.newpage()
grid.draw(g1)

enter image description here

关于替换 gtable : labels and gridlines 中 ggplot 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27754638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com