r - 将面板网格置于前面

标签 r ggplot2

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

The red coloured grid is to be brought to the front:

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

The expected output.

最佳答案

如评论 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)
  )

enter image description here

关于r - 将面板网格置于前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57018570/

相关文章:

r - R 中的 lda 预测 : Warning message: 'newdata' had 1600 rows but variables found have 200 rows

R. 字母组合

R ggplot2 : Setting additional specific axis tick marks

r - 禁用ggplots的cowplot默认值

r - 使用 ggplot 以编程方式绘制热门事件的子事件 : R

r - 如何在 R Markdown 中突出显示内联 R 代码?

r - 更改 ggplot2 中图例键中的符号

r - 二维图中一维的ggplot2密度

r - LaTeX 符号未正确转换为 Moodle

r - qplot ylab 中的下标和上标 [R]