Is it possible to rasterize a ggplot in such way that the resulting raster object represents the ggplot?
I have a following ggplot p:
If I convert it to a raster object and plot it again it looks like this:
This is how I create and rasterize the plot p:
library(ratser)
library(ggplot)
p<-ggplot(wind_data, aes(x = lon, y = lat))
geom_segment(aes(xend = lon 0.1, yend = lat 0.1),
arrow = arrow(length = unit(0.1, "cm")), size = 0.25)
p<-p theme_void() coord_cartesian(xlim = c(3.604383, 14.60482), ylim = c(47.07157 ,54.73807 )) theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black") )
ggsave(plot=p, "my_ggplot.tiff", device = "tiff")
raster <- raster::raster("my_ggplot.tiff") # OR stack("my_ggplot.tiff") for colored images
lat_long <- ggplot_build(p)$layout$panel_params[[1]][c("x.range","y.range")]
extent(raster) <- c(lat_long$x.range,lat_long$y.range)
projection(raster) <- CRS(" proj=longlat datum=WGS84")
Here is wind_data data.frame:
wind_data<-structure(list(lon = c(8.87312091729266, 8.71871388830953, 10.5679127453358,
10.7216122406487, 10.4141464047873, 10.5679127453358, 10.1064188710028,
11.3357135330171, 11.4890576651767, 11.1822955254471), lat = c(54.6482360923283,
54.5584019137964, 54.2888913181823, 54.2888913181823, 54.1990517614177,
54.1990517614177, 54.0193686018903, 53.6599860593553, 53.6599860593553,
53.5701370357575), dd = c(241, 238, 247, 240, 240, 246, 247,
246, 245, 247)), class = "data.frame", row.names = c(NA, -10L
))
Here is the data of the plot p:
p<-structure(list(data = structure(list(lon = c(8.87312091729266,
8.71871388830953, 10.5679127453358, 10.7216122406487, 10.4141464047873,
10.5679127453358, 10.1064188710028, 11.3357135330171, 11.4890576651767,
11.1822955254471), lat = c(54.6482360923283, 54.5584019137964,
54.2888913181823, 54.2888913181823, 54.1990517614177, 54.1990517614177,
54.0193686018903, 53.6599860593553, 53.6599860593553, 53.5701370357575
), dd = c(241, 238, 247, 240, 240, 246, 247, 246, 245, 247)), class = "data.frame", row.names = c(NA,
-10L)), layers = list(<environment>), scales = <environment>,
mapping = structure(list(x = ~lon, y = ~lat), class = "uneval"),
theme = structure(list(line = structure(list(), class = c("element_blank",
"element")), rect = structure(list(), class = c("element_blank",
"element")), text = structure(list(family = "", face = "plain",
colour = "black", size = 11, hjust = 0.5, vjust = 0.5,
angle = 0, lineheight = 0.9, margin = structure(c(0,
0, 0, 0), unit = 8L, class = c("margin", "simpleUnit",
"unit", "unit_v2")), debug = FALSE, inherit.blank = TRUE), class = c("element_text",
"element")), title = NULL, aspect.ratio = NULL, axis.title = structure(list(), class = c("element_blank",
"element")), axis.title.x = NULL, axis.title.x.top = NULL,
axis.title.x.bottom = NULL, axis.title.y = NULL, axis.title.y.left = NULL,
axis.title.y.right = NULL, axis.text = structure(list(), class = c("element_blank",
"element")), axis.text.x = NULL, axis.text.x.top = NULL,
axis.text.x.bottom = NULL, axis.text.y = NULL, axis.text.y.left = NULL,
axis.text.y.right = NULL, axis.ticks = NULL, axis.ticks.x = NULL,
axis.ticks.x.top = NULL, axis.ticks.x.bottom = NULL,
axis.ticks.y = NULL, axis.ticks.y.left = NULL, axis.ticks.y.right = NULL,
axis.ticks.length = structure(0, unit = 8L, class = c("simpleUnit",
"unit", "unit_v2")), axis.ticks.length.x = NULL, axis.ticks.length.x.top = NULL,
axis.ticks.length.x.bottom = NULL, axis.ticks.length.y = NULL,
axis.ticks.length.y.left = NULL, axis.ticks.length.y.right = NULL,
axis.line = structure(list(colour = "black", size = NULL,
linetype = NULL, lineend = NULL, arrow = FALSE, inherit.blank = FALSE), class = c("element_line",
"element")), axis.line.x = NULL, axis.line.x.top = NULL,
axis.line.x.bottom = NULL, axis.line.y = NULL, axis.line.y.left = NULL,
axis.line.y.right = NULL, legend.background = NULL, legend.margin = NULL,
legend.spacing = NULL, legend.spacing.x = NULL, legend.spacing.y = NULL,
legend.key = NULL, legend.key.size = structure(1.2, unit = 3L, class = c("simpleUnit",
"unit", "unit_v2")), legend.key.height = NULL, legend.key.width = NULL,
legend.text = structure(list(family = NULL, face = NULL,
colour = NULL, size = structure(0.8, class = "rel"),
hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL,
margin = NULL, debug = NULL, inherit.blank = TRUE), class = c("element_text",
"element")), legend.text.align = NULL, legend.title = structure(list(
family = NULL, face = NULL, colour = NULL, size = NULL,
hjust = 0, vjust = NULL, angle = NULL, lineheight = NULL,
margin = NULL, debug = NULL, inherit.blank = TRUE), class = c("element_text",
"element")), legend.title.align = NULL, legend.position = "right",
legend.direction = NULL, legend.justification = NULL,
legend.box = NULL, legend.box.just = NULL, legend.box.margin = NULL,
legend.box.background = NULL, legend.box.spacing = NULL,
panel.background = NULL, panel.border = structure(list(), class = c("element_blank",
"element")), panel.spacing = structure(5.5, unit = 8L, class = c("simpleUnit",
"unit", "unit_v2")), panel.spacing.x = NULL, panel.spacing.y = NULL,
panel.grid = NULL, panel.grid.major = structure(list(), class = c("element_blank",
"element")), panel.grid.minor = structure(list(), class = c("element_blank",
"element")), panel.grid.major.x = NULL, panel.grid.major.y = NULL,
panel.grid.minor.x = NULL, panel.grid.minor.y = NULL,
panel.ontop = FALSE, plot.background = NULL, plot.title = structure(list(
family = NULL, face = NULL, colour = NULL, size = structure(1.2, class = "rel"),
hjust = 0, vjust = 1, angle = NULL, lineheight = NULL,
margin = structure(c(5.5, 0, 0, 0), unit = 8L, class = c("margin",
"simpleUnit", "unit", "unit_v2")), debug = NULL,
inherit.blank = TRUE), class = c("element_text",
"element")), plot.title.position = "panel", plot.subtitle = structure(list(
family = NULL, face = NULL, colour = NULL, size = NULL,
hjust = 0, vjust = 1, angle = NULL, lineheight = NULL,
margin = structure(c(5.5, 0, 0, 0), unit = 8L, class = c("margin",
"simpleUnit", "unit", "unit_v2")), debug = NULL,
inherit.blank = TRUE), class = c("element_text",
"element")), plot.caption = structure(list(family = NULL,
face = NULL, colour = NULL, size = structure(0.8, class = "rel"),
hjust = 1, vjust = 1, angle = NULL, lineheight = NULL,
margin = structure(c(5.5, 0, 0, 0), unit = 8L, class = c("margin",
"simpleUnit", "unit", "unit_v2")), debug = NULL,
inherit.blank = TRUE), class = c("element_text",
"element")), plot.caption.position = "panel", plot.tag = structure(list(
family = NULL, face = NULL, colour = NULL, size = structure(1.2, class = "rel"),
hjust = 0.5, vjust = 0.5, angle = NULL, lineheight = NULL,
margin = NULL, debug = NULL, inherit.blank = TRUE), class = c("element_text",
"element")), plot.tag.position = "topleft", plot.margin = structure(c(0,
0, 0, 0), unit = 3L, class = c("simpleUnit", "unit",
"unit_v2")), strip.background = NULL, strip.background.x = NULL,
strip.background.y = NULL, strip.placement = NULL, strip.text = structure(list(
family = NULL, face = NULL, colour = NULL, size = structure(0.8, class = "rel"),
hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL,
margin = NULL, debug = NULL, inherit.blank = TRUE), class = c("element_text",
"element")), strip.text.x = NULL, strip.text.y = NULL,
strip.switch.pad.grid = structure(2.75, unit = 8L, class = c("simpleUnit",
"unit", "unit_v2")), strip.switch.pad.wrap = structure(2.75, unit = 8L, class = c("simpleUnit",
"unit", "unit_v2"))), class = c("theme", "gg"), complete = TRUE, validate = TRUE),
coordinates = <environment>, facet = <environment>, plot_env = <environment>,
labels = list(x = "lon", y = "lat", xend = "lon 0.1", yend = "lat 0.1")), class = c("gg",
"ggplot"))
If i understand it correctly the resulting raster object should have only min value 0 and max value 1 and represent the arrows in lat long coordinates. But it does not look like this. What am I doing wrong?
CodePudding user response:
Still not sure about this conversion on a meta level, but technically you should be able to solve it this way:
library(terra)
#> terra 1.6.33
library(geodata)
# write your plot to disk
ggsave(plot = p, "my_ggplot.jpeg")
# import as SpatRaster object
r <- rast("my_ggplot.jpeg")
#> Warning: [rast] unknown extent
# your object has 3 layers with values ranging from 0 to 255
r
#> class : SpatRaster
#> dimensions : 1668, 3609, 3 (nrow, ncol, nlyr)
#> resolution : 1, 1 (x, y)
#> extent : 0, 3609, 0, 1668 (xmin, xmax, ymin, ymax)
#> coord. ref. :
#> source : my_ggplot.jpeg
#> colors RGB : 1, 2, 3
#> names : my_ggplot_1, my_ggplot_2, my_ggplot_3
summary(r)
#> Warning: [summary] used a sample
#> my_ggplot_1 my_ggplot_2 my_ggplot_3
#> Min. : 62 Min. : 62 Min. : 62
#> 1st Qu.:255 1st Qu.:255 1st Qu.:255
#> Median :255 Median :255 Median :255
#> Mean :254 Mean :254 Mean :254
#> 3rd Qu.:255 3rd Qu.:255 3rd Qu.:255
#> Max. :255 Max. :255 Max. :255
# provide proper georeferencing information by crs and extent definition
crs(r) <- " proj=longlat datum=WGS84 no_defs type=crs"
ext(r) <- c(3.604383, 14.60482, 47.07157, 54.73807) |> ext()
r
#> class : SpatRaster
#> dimensions : 1668, 3609, 3 (nrow, ncol, nlyr)
#> resolution : 0.003048057, 0.004596223 (x, y)
#> extent : 3.604383, 14.60482, 47.07157, 54.73807 (xmin, xmax, ymin, ymax)
#> coord. ref. : proj=longlat datum=WGS84 no_defs
#> source : my_ggplot.jpeg
#> colors RGB : 1, 2, 3
#> names : my_ggplot_1, my_ggplot_2, my_ggplot_3
# this is optional, just to verify the correct position
adm <- geodata::gadm("de", path = tempdir())
# inspect result
plot(r)
plot(adm, add = TRUE, border = "red")