Reputation: 163
I have my made a plot using ggplot2 and use a graphic as a background. Now I want to add grids to make it more clear and I've got stuck. My ggplot2 function:
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")
I will be grateful for any help.
Upvotes: 0
Views: 952
Reputation: 124183
Using the example from ggpubr::background_image
this can be achieved by setting panel.background = element_blank()
and panel.ontop = TRUE
. Try this:
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)
Created on 2020-05-25 by the reprex package (v0.3.0)
EDIT: An alterantive approach would be to draw the grid lines manually using geom_vline
and 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")
Created on 2020-05-26 by the reprex package (v0.3.0)
Upvotes: 2