将网格线添加到带有背景图像的 ggplot
Adding grid lines to a ggplot with a background image
我已经使用 ggplot2 绘制了一个图,并使用图形作为背景。现在我想添加网格以使其更清晰,但我被卡住了。我的 ggplot2 函数:
library(ggplot2)
library(ggpubr)
ggplot(df), aes(x = Year, y = Number)) +
background_image(jpg) +
geom_line(color = "black", size = 2) +
labs(title = "Title", y = "Name1", x = "Name2")
如有任何帮助,我将不胜感激。
使用 ggpubr::background_image
中的示例,这可以通过设置 panel.background = element_blank()
和 panel.ontop = TRUE
来实现。试试这个:
library(ggpubr)
#> Loading required package: ggplot2
library(ggplot2)
img.file <- system.file(file.path("images", "background-image.png"),
package = "ggpubr")
img <- png::readPNG(img.file)
# Plot with background image
ggplot(iris, aes(Species, Sepal.Length))+
background_image(img)+
geom_boxplot(aes(fill = Species), color = "white")+
theme(panel.background = element_blank(),
panel.ontop = TRUE)
由 reprex package (v0.3.0)
于 2020-05-25 创建
编辑: 另一种方法是使用 geom_vline
和 geom_hline
:
手动绘制网格线
library(ggpubr)
#> Loading required package: ggplot2
library(ggplot2)
img.file <- system.file(file.path("images", "background-image.png"),
package = "ggpubr")
img <- png::readPNG(img.file)
# Plot with background image
ggplot(iris, aes(Species, Sepal.Length))+
background_image(img) +
geom_vline(aes(xintercept = Species), color = "white") +
geom_hline(yintercept = c(5, 6, 7, 8), color = "white") +
geom_boxplot(aes(fill = Species), color = "white")
由 reprex package (v0.3.0)
于 2020-05-26 创建
我已经使用 ggplot2 绘制了一个图,并使用图形作为背景。现在我想添加网格以使其更清晰,但我被卡住了。我的 ggplot2 函数:
library(ggplot2)
library(ggpubr)
ggplot(df), aes(x = Year, y = Number)) +
background_image(jpg) +
geom_line(color = "black", size = 2) +
labs(title = "Title", y = "Name1", x = "Name2")
如有任何帮助,我将不胜感激。
使用 ggpubr::background_image
中的示例,这可以通过设置 panel.background = element_blank()
和 panel.ontop = TRUE
来实现。试试这个:
library(ggpubr)
#> Loading required package: ggplot2
library(ggplot2)
img.file <- system.file(file.path("images", "background-image.png"),
package = "ggpubr")
img <- png::readPNG(img.file)
# Plot with background image
ggplot(iris, aes(Species, Sepal.Length))+
background_image(img)+
geom_boxplot(aes(fill = Species), color = "white")+
theme(panel.background = element_blank(),
panel.ontop = TRUE)
由 reprex package (v0.3.0)
于 2020-05-25 创建编辑: 另一种方法是使用 geom_vline
和 geom_hline
:
library(ggpubr)
#> Loading required package: ggplot2
library(ggplot2)
img.file <- system.file(file.path("images", "background-image.png"),
package = "ggpubr")
img <- png::readPNG(img.file)
# Plot with background image
ggplot(iris, aes(Species, Sepal.Length))+
background_image(img) +
geom_vline(aes(xintercept = Species), color = "white") +
geom_hline(yintercept = c(5, 6, 7, 8), color = "white") +
geom_boxplot(aes(fill = Species), color = "white")
由 reprex package (v0.3.0)
于 2020-05-26 创建