ggplot2 图的面板网格被创建在图的背景上。我的问题是:它是否有可能被修改以被带过来?
我可以部分看到用 geom_hline()
代替网格的解决方案或 geom_vline()
层。但是,对于更复杂的图或在绘制 map 时,这可能会很棘手,因此我的问题仅涉及修改 theme()
的元素。 .
library(tidyverse)
df <- data.frame(x = c(1,2),
y = c(1,2))
df %>% ggplot(aes(x, y)) +
geom_area() + theme(
panel.grid = element_line(color = "red")
)
用
geom_hline()
代替网格的欺骗解决方案或 geom_vline()
grd_x <- seq(1, 2, length.out = 9)
grd_y <- seq(0, 2, length.out = 9)
df %>% ggplot(aes(x, y)) +
geom_area() +
geom_hline(yintercept = grd_y, col = "red") +
geom_vline(xintercept = grd_x, col = "red")
最佳答案
如评论 1 中所述,您可以使用 theme(panel.ontop = TRUE)
.但是,在尝试此操作时,我再也看不到图表了。因此,您在更改panel.ontop
时需要确保面板的背景图像为空白。至 TRUE
:
library(tidyverse)
df <- data.frame(x = c(1,2),
y = c(1,2))
df %>% ggplot(aes(x, y)) +
geom_area() +
theme(panel.grid = element_line(color = "red"),
panel.ontop = TRUE, panel.background = element_rect(color = NA, fill = NA)
)
关于r - 将面板网格置于前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57018570/