I have a code below for generating a simple polar bar plot in ggplot for quadrant-based values (wind radii from a central point of latitude & longitude), which looks like this:
I want to extract these polar plots to a SpatialPolygons object, so I can plot them as polygons on a map similar to this:
Is there any method to extract ggplot objects like this to a SpatialPolygons, shapefile, or some kind of dataframe for plotting on a map with ggplot/ggmap? Even a suggestion to explore further would be useful. Thanks in advance.
My dataframe:
winds <- data.frame(WindField = c(34, 50, 64, 34, 50, 64, 34, 50, 64, 34, 50, 64),
Quadrant = c("NE", "NE", "NE", "SE", "SE", "SE",
"SW", "SW", "SW", "NW", "NW", "NW"),
Radius = c(222, 93, 37, 139, 46, 37, 74, 19, 9, 222, 93, 37))
quads <- c("NE", "SE", "SW", "NW")
My ggplot code:
ggplot() +
geom_col(data = winds,
aes(x = factor(Quadrant, levels = quads),
y = Radius,
fill = factor(WindField),
group = factor(Quadrant, levels = quads)),
stat = "identity", position = "identity", width = 1, color = 'black') +
scale_fill_manual(values = c("yellow", "orange", "red")) +
guides(fill = guide_legend(title = "Wind [kt]")) +
coord_polar() +
theme_bw() +
theme(plot.title = element_text(size = 16),
plot.subtitle = element_text(size = 12),
axis.title = element_text(size = 14),
axis.text.y = element_text(size = 12, face = 'bold'),
axis.text.x = element_text(size = 14, face = 'bold'),
legend.text = element_text(size = 13),
legend.title = element_text(size = 13),
panel.border = element_blank(),
legend.position = "bottom") +
labs(y = "Radius [km]", x='Quadrant')
I don't think shapefiles play very well with ggplot, but you can convert the plots (which are ggplot objects) into grob objects & add them to the map using
annotation_custom()
.Here's an example, assuming you wish to plot multiple bar plots using a single data frame source file that contains all the necessary information.
Step 0: Generate data
Verify what the plots would look like, on a normal plot:
Step 1: Create separate polar bar plot for each location, convert to grob object, & specify their positions
Step 2: Create map
Step 3: Combine map with the list of bar plot grobs