I am mapping data by using geom_scatterpie. However, either my map is skewed, or my pie charts. When using coord_map/coord_fixed, I cannot use these functions separately. How can I make sure both my map and my pie charts look 'normal'?
This is the code I run:
ggplot()+ geom_polygon(data = NLD_fixed, aes(x = long, y = lat, group = group),
fill= 'gray',colour = "white")+
geom_scatterpie(data=DF,aes(x=V9, y=V8, r=total/150),
cols = c("A","B", "C"),
alpha=.3)
Thanks in advance!
Edit: the goal of my code is to map pie charts for different regions, so that I can see where some technologies are produced, in what quantity, and which (therefore the pie charts). As you can see, my pie charts are skewed. If I use coord_fixed, my base map is skewed.
df<- structure(list(V8 = c(50.85, 51.4478875, 51.45, 51.966667, 52),
V9 = c(5.683333, 5.4859493, 5.466667, 5.666667, 4.366667),
Bio = c(1, 0, 1, 1, 0), Comp = c(0, 0, 2, 0, 16),
EnConv = c(0, 2, 1, 0, 5)), row.names = c(NA, -5L),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
groups = structure(list(V8 = c(50.85, 51.4478875, 51.45, 51.966667, 52),
.rows = list(1L, 2L, 3L, 4L, 5L)), row.names = c(NA, -5L),
class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))
I have tried the method of a similar question (R pie charts distorted when adding to projected map using ggplot) With Step 1:
tidyr::gather(type, value, -V8, -V9, -total,-V10, -City) %>%
tidyr::nest(type, value)
with dataset:
structure(list(V8 = c(50.85, 51, 51.44789, 51.45, 51.85, 51.966667,
52, 52.091, 52.15, 52.216667, 52.25, 52.35, 52.766667, 53.2,
53.216667), V9 = c(5.683333, 5.766667, 5.866667, 5.466667, 5.75,
5.666667, 4.366667, 5.122, 4.5, 6.9, 4.716667, 4.916667, 4.666667,
5.783333, 6.55), City = c("Maastricht", "Urmond", "TU/E", "Eindhoven",
"Nijmegen", "Wageningen", "Delft", "Utrecht", "Leiden", "Enschede",
"VU Campus", "Amsterdam", "Petten", "Leeuwarden", "Groningen"
), total = c(8, 23, 79, 93, 16, 53, 69, 85, 26, 23, 33, 52, 21,
19, 27), V10 = c(50.81, 50.96, 51.40789, 51.41, 51.81, 51.926667,
51.96, 52.051, 52.11, 52.176667, 52.21, 52.31, 52.726667, 53.16,
53.176667), data = list(structure(list(type = c("A", "B", "C",
"D", "E", "F", "G", "H"), value = c(0, 1, 3, 2, 0, 0, 0, 2)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -8L)), structure(list(
type = c("A", "B", "C", "D", "E", "F", "G", "H"), value = c(5,
15, 1, 0, 0, 0, 0, 2)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -8L)), structure(list(type = c("A", "B",
"C", "D", "E", "F", "G", "H"), value = c(7, 17, 13, 28, 1, 11,
1, 1)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-8L)), structure(list(type = c("A", "B", "C", "D", "E", "F",
"G", "H"), value = c(12, 6, 10, 47, 0, 15, 0, 3)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -8L)), structure(list(
type = c("A", "B", "C", "D", "E", "F", "G", "H"), value = c(0,
3, 4, 9, 0, 0, 0, 0)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -8L)), structure(list(type = c("A", "B",
"C", "D", "E", "F", "G", "H"), value = c(2, 28, 10, 4, 0, 1,
0, 8)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-8L)), structure(list(type = c("A", "B", "C", "D", "E", "F",
"G", "H"), value = c(16, 13, 4, 23, 1, 7, 4, 1)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -8L)), structure(list(
type = c("A", "B", "C", "D", "E", "F", "G", "H"), value = c(3,
36, 7, 33, 1, 3, 0, 2)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -8L)), structure(list(type = c("A", "B",
"C", "D", "E", "F", "G", "H"), value = c(0, 10, 5, 11, 0, 0,
0, 0)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-8L)), structure(list(type = c("A", "B", "C", "D", "E", "F",
"G", "H"), value = c(5, 9, 2, 4, 2, 1, 0, 0)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -8L)), structure(list(
type = c("A", "B", "C", "D", "E", "F", "G", "H"), value = c(4,
3, 9, 15, 0, 1, 0, 1)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -8L)), structure(list(type = c("A", "B",
"C", "D", "E", "F", "G", "H"), value = c(7, 10, 11, 18, 1, 4,
1, 0)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-8L)), structure(list(type = c("A", "B", "C", "D", "E", "F",
"G", "H"), value = c(3, 11, 1, 0, 0, 6, 0, 0)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -8L)), structure(list(
type = c("A", "B", "C", "D", "E", "F", "G", "H"), value = c(1,
15, 1, 2, 0, 0, 0, 0)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -8L)), structure(list(type = c("A", "B",
"C", "D", "E", "F", "G", "H"), value = c(2, 11, 4, 8, 0, 0, 0,
2)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-8L)))), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -15L), groups = structure(list(V8 = c(50.85,
51, 51.44789, 51.45, 51.85, 51.966667, 52, 52.091, 52.15, 52.216667,
52.25, 52.35, 52.766667, 53.2, 53.216667), V9 = c(5.683333, 5.766667,
5.866667, 5.466667, 5.75, 5.666667, 4.366667, 5.122, 4.5, 6.9,
4.716667, 4.916667, 4.666667, 5.783333, 6.55), .rows = list(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L)), row.names = c(NA, -15L), class = c("tbl_df", "tbl",
"data.frame"), .drop = FALSE))
(Warning message:
All elements of '...' must be named.
Did you want data = c(type, value)
? )
but when I try step 2 (added to my dataset)
mutate(pie.grob = purrr::map(data,
function(d) ggplotGrob(ggplot(d,
aes(x = 1, y = value, fill = type)) +
geom_col(color = "black",
show.legend = FALSE) +
coord_polar(theta = "y") +
theme_void())))
I get the error: Error: .x must be a vector, not a function I've tried playing around with the first code, and with the second, but cannot seem to get it right. Thanks in advance!