首页 > 解决方案 > 重叠几何坐标适用于其中一个或其他

问题描述

我正在使用geom_scatterpie映射数据。但是,要么我的地图歪斜,要么我的饼图歪斜。使用 coord_map/coord_fixed 时,我不能单独使用这些功能。如何确保我的地图和饼图看起来“正常”?

这是我运行的代码:

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) 

提前致谢!

编辑:我的代码的目标是绘制不同地区的饼图,这样我就可以看到一些技术的生产地点、数量和哪些(因此是饼图)。如您所见,我的饼图是倾斜的。如果我使用 coord_fixed,我的底图就会倾斜。

地图

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))

我已经尝试了类似问题的方法(使用 ggplot 添加到投影地图时 R 饼图失真)第 1 步:

  tidyr::gather(type, value, -V8, -V9, -total,-V10, -City) %>%
  tidyr::nest(type, value)

使用数据集:

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))

(警告信息:'...' 的所有元素都必须命名。你想要data = c(type, value)吗?)

但是当我尝试第 2 步时(添加到我的数据集中)

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())))

我收到错误:错误:.x 必须是向量,而不是我尝试过使用第一个代码和第二个代码的函数,但似乎无法正确处理。提前致谢!

标签: roverlapcoordinate-transformationgeometry-surfacescatterpie

解决方案


推荐阅读