0

我一直在尝试在地图上绘制 3D 条形图。我可以使用包绘制 3D 条形图,但是rayshader这个包从. 我想在地图上绘制堆叠或瀑布条。像这样在 excel 中使用 PowerMap或像这张图片fillcolorggplot

这是我的数据:

data <- structure(list(group = structure(c(4L, 5L, 12L, 9L, 7L, 16L, 
25L, 32L, 23L, 10L, 13L, 33L, 8L, 11L, 26L, 29L, 21L, 2L, 1L, 
3L, 28L, 18L, 20L, 19L, 15L, 14L, 30L, 27L, 31L, 17L, 22L, 6L, 
24L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", 
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", 
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", 
"32", "33"), class = "factor"), max = c(15.97, 40.4, 40.4, 40.4, 
37.2, 37.2, 33.9, 33.3, 30.8, 30.8, 26.3, 24.8, 24.8, 24.8, 15.97, 
15.97, 13.6, 23.03, 24.8, 15.97, 15.97, 15.97, 23.03, 33.9, 37.2, 
11.608, 33.9, 33.9, 30.8, 28.1, 9.7, 7.246, 13.82), min = c(11.608, 
37.2, 37.2, 37.2, 33.9, 33.9, 33.3, 30.8, 20.43, 20.43, 24.8, 
20.43, 20.43, 20.43, 13.6, 13.6, 10.3, 11.608, 20.43, 7.246, 
7.246, 13.82, 15.97, 28.4, 33.9, 5.333, 28.4, 33.3, 20.43, 23.03, 
8.7, 5.333, 11.62), lng = c(72.366669, -104.199997, -107.099998, 
-104.199997, -102.449997, -104.199997, -103.800003, -119.616669, 
-102.5, -119.633331, -103.800003, -103.866669, -103.866669, -107, 
-103.800003, -103.800003, -100.800003, 16.5, -104.199997, 0.716667, 
0.7, 0.32, 103.606667, 1.65, 107.25, 33.358612, 13.316667, -103.800003, 
-119.581108, 7.856111, -3.78, 39.233334, 35.849998), lat = c(32.683334, 
29.9, 43.200001, 29.9, 43.599998, 29.9, 42.400002, 44.599998, 
43.299999, 44.700001, 42.400002, 42.416668, 42.416668, 35.700001, 
42.400002, 42.400002, 42.799999, -28.25, 42, 43.450001, 43.483334, 
47.450001, 36.23111, 44.333332, 23.666668, 47.892776, 50.200001, 
42.400002, 44.785, 47.343334, 40.18, 12.233333, 0.9)), row.names = c(NA, -33L), class = c("tbl_df", "tbl", "data.frame"), .Label = 1:33)

这是我目前能够做的一个例子:

listOfPackages <- c("ggplot2","tidyverse","rnaturalearth","rnaturalearthdata","png","grid","rayshader","sf","rgeos")
lapply(listOfPackages, require, character.only = TRUE)

#Base Map
    
    region <- ne_countries(scale = "medium", returnclass = "sf",continent = "North america")
    basemap <- ggplot(data = region) + geom_sf(aes(colour="#FFFFFF"))+coord_sf(xlim = c(-50, -170), ylim = c(15, 90), expand = FALSE)+  
        theme(legend.position = "none",axis.line=element_blank(),axis.text.x=element_blank(), axis.title.x=element_blank(),axis.text.y=element_blank(), axis.title.y=element_blank(),axis.ticks=element_blank(),panel.background = element_blank())
    
    ggsave("map_img.png",plot = basemap, width = 100, height = 100, units = "mm")
    background <- readPNG("map_img.png")        
    
    xlim=c(-170,-50)
    ylim=c(15,90)
    map1 <- ggplot(data = region) + annotation_custom(rasterGrob(background, width=unit(1,"npc"), height=unit(1,"npc")), -Inf, Inf, -Inf, Inf)+xlim(xlim[1],xlim[2]) + ylim(ylim[1],ylim[2])+
        geom_point(data = data, aes(x = lng, y = lat, colour=max),shape=5, size=0.2) +
        geom_point(data = data, aes(x = lng, y = lat, colour=min),shape=3, size=1)+
        scale_colour_gradient(name = "millions", 
                              limits=range(data$max), 
                              low="#ffff73", high="#fca773")+
        ggtitle("records")+
        theme(plot.title = element_text(hjust = 0.5))
    
    #3D plot
    map_3d <- plot_gg(map1,multicore = TRUE, width = 7 ,height=7, raytrace = FALSE, windowsize = c(1200, 960), fov = 70, zoom = 0.25, theta = 330, phi = 40)

另外(如果可能的话)我想为不同的组使用不同的颜色

4

0 回答 0