ggplot2:为添加到地图的点设置自定义颜色、形状和大小

Gig*_*aur 1 r ggplot2

我想根据名为“数据集”的变量为添加到地图的点设置自定义形状、大小和颜色。如果我将所有点的形状设置为相同类型,我就可以设置点的颜色,但我希望有一张包含更多信息的地图。当我运行这段代码时,所有点都是黑色的圆圈。我缺少什么?

感谢大家的帮助和时间!

这是一个可重现的示例:

    # Read in libraries
    library(ggplot2)
    library(maps)
    library(maptools)
    library(ggmap)

    # Create mapping objects
    world <- map_data("world2")
    world$long <- world$long
    state_dat <- map_data("state")
    canada <- world[world$region==c("Canada"),]
    map_dat <- rbind(state_dat, canada)

    # Create custom shapes, sizes, colors
    pt_colors=c("red", "blue", "grey", "green")
    shapes = c(120, 22, 24, 21)
    shape_size = c(1.1, 0.8, 1, 1)

    # Create lat/long dataframe
    xy <- data.frame(Dataset=c("GBIF","Flower","GBIF","Leaf","DNA","GBIF","GBIF","Leaf","GBIF","GBIF","DNA","GBIF","DNA","GBIF","GBIF","Leaf","GBIF","GBIF","GBIF","DNA"),
                      lat=c(38.89450,34.45300,39.86556,30.38818,28.74590,33.78527,41.23439,30.37935,41.38250,40.60648,30.87580,40.56425,28.75000,41.52666,35.46451,30.73621,38.50221,33.70335,38.98000,29.61100),
                      long=c(-77.06292,-84.22643,-79.50248,-84.64519,-81.47860,-84.37109,-81.46374,-86.17667,-72.10861,-74.53538,-84.41520,-74.86654,-81.47750,-73.15833,-78.89952,-86.73095,-78.40308,-86.70289,-77.03917,-81.78740)
    )

    # Create base map
    p0 <- ggplot() +
      geom_polygon(data=map_dat,aes(x=long,y=lat,group=group, fill=region),fill="white",color="black", show.legend=FALSE)+
      coord_map("gilbert",xlim=c(-60,-97),ylim=c(15,47.5)) +#mollweide is pretty good
      labs(x=expression("Longitude"*~degree*W), y=expression("Latitude"*~degree*N)) +
      theme(panel.border = element_rect(colour = "black", fill=NA, size=1),
        plot.margin=unit(c(0.25,0.25,0.25,0.25),'inches'),
        legend.position='none') +
      theme(rect = element_blank())

    # Add points to the map
    p1 <- p0 +
        geom_point(data=xy,aes(x=long,y=lat,fill=Dataset)) + 
        scale_color_manual(values=pt_colors) + 
        scale_shape_manual(values=shapes) + 
        scale_size_manual(values=shape_size)
Run Code Online (Sandbox Code Playgroud)

阴谋

dsh*_*kol 6

您的颜色、形状和尺寸需要符合您的geom_point审美观。Geom_point不是fill为了美观而使用,而是为了使用colour

只需修复它就会生成您想要的内容。

p1 <- p0 +
  geom_point(data=xy,aes(x=long,y=lat,colour = Dataset, shape = Dataset, size = Dataset)) + 
  scale_color_manual(values=pt_colors) + 
  scale_shape_manual(values=shapes) + 
  scale_size_manual(values=shape_size)
Run Code Online (Sandbox Code Playgroud)

  • 澄清:大多数 ggplot 点形状(形状 0 - 20)仅使用颜色,而不使用填充。点形状 21 - 25 实际上具有颜色和填充美感 (2认同)