R:用纯色和线条填充多边形

时间:2021-05-25 21:59:54

标签: r ggplot2

我目前正在使用 ggplot 创建状态地图,但在弄清楚如何有条件地将线条和颜色添加到地图内的多边形时遇到了一些麻烦。我的数据集中有一个变量“行”。当lines==1 时,我希望关联的多边形填充有颜色(已设置)以及线条。以下是我的代码:

map_theme_main <- theme(panel.background = element_rect(fill="white"),
                        panel.border = element_rect(color="white", fill=NA, size=rel(1)),
                        axis.text.x = element_blank(),
                        axis.title.x = element_blank(),
                        axis.ticks.x = element_blank(),
                        axis.text.y = element_blank(),
                        axis.title.y = element_blank(),
                        plot.title = element_text(size = 16, hjust = .5),
                        plot.caption = element_text(size = 8, hjust = 0),
                        #legend.position = "bottom",
                        #legend.justification = c(0,0),
                        legend.text = element_text(size = 10),
                        legend.position = "right",
                        legend.title = element_text(size = 14),
                        axis.ticks.y = element_blank(),
                        panel.grid = element_blank(),
                        legend.key.size = unit(.6, 'cm'),
                        panel.grid.major = element_line(color = 'white'))


main<-ggplot(data = zcta_ss_DF, aes(x=long, y=lat,group=group, fill =bin)) + geom_polygon() + geom_path(size = 0.1) +
  scale_fill_manual('', values = colors, drop = F) +
  coord_cartesian(xlim = c(-125, -117), ylim = c(45, 49)) + 
  ggtitle("Total Sample Size By ZCTA") +
  map_theme_main + guides(fill = guide_legend(ncol=1, title.theme = element_text(size=8)))

main <- main + theme(legend.key = element_rect(color="black"))

我将如何修改上面的代码以在变量“lines”==1 的行上添加?

1 个答案:

答案 0 :(得分:0)

您所说的“行”并不完全清楚,而且由于没有可重复的示例,我从 {maps} 中“借用”了。但是,如果您根据自己的情况调整代码,它应该可以帮助您前进。出于演示原因,我跳过了您应用的主题。

library(dplyr)
library(ggplot2)
library(maps)

my_map <- map_data("state")    # get a state-level world map

#----------------- basic plot -------------------------
ggplot() + 
    geom_polygon( data=my_map, aes(x=long, y=lat, group=group)
                , color="black"     # color defines the "line" of the polygon
                , fill="lightblue"  # fill gives the fill color of the polygon
                ) +
    coord_cartesian(xlim = c(-125, -117), ylim = c(45, 49)) +
    ggtitle("Total Sample Size By ZCTA") 

basic ggplot map of Washington

注意 colorfillgeom_polygon() 调用中的控制。

如果您想有条件地更改填充颜色,我们可以将此“微分器”作为一列添加到我们的地图数据框中。您可以将其作为分类变量添加到数据框中。我使用了一个简单的 TRUE/FALSE(又名二元分类变量)。在 scale_fill_manual() 中控制类别的颜色顺序。重要的是 fill 现在是一个变量,您可以为其应用“美学映射”。因此,它需要进入 aes()!
可以说,我的颜色选择可以改进。 :)

my_map <- my_map %>% 
   mutate(my_color = ifelse(region == "washington", TRUE, FALSE)  # adding a simple categorical variable
   ) 

ggplot() + 
  geom_polygon(data = my_map 
               , aes( x=long, y=lat, group=group
#--------------------- include mapping for fill --------------------------------------
                     ,fill = my_color)                      # fill is now an aesthetic
#------------------------------------------------------------------------------------- 
               , color="black" 
                ) +
  scale_fill_manual(values = c("yellow","lightblue")) +     # more control over fill
  coord_cartesian(xlim = c(-125, -117), ylim = c(45, 49)) +
  ggtitle("Total Sample Size By ZCTA")

enter image description here

将它放在后袋中,您现在可以处理多边形线(又名边界)。 color 参数定义线条颜色。如果你想“映射”它,你可以用类似的方式有条件地做到这一点。我对 line-color 和 line-width 应用相同的原则,即 size.
由于缺乏创造力,我使用了上面创建的分类变量 my_color。显然,您可以在此处提供不同的值或其他变量。

ggplot() + 
  geom_polygon(data = my_map 
               , aes(x=long, y=lat, group=group, fill = my_color
#------------------ now color and size get included in the mapping -------------
               , color= my_color
               , size = my_color) 
#--------------------------------------------- and supplied with a categorical variable
                ) +
  scale_fill_manual(values = c("yellow","lightblue")) +
  coord_cartesian(xlim = c(-125, -117), ylim = c(45, 49)) +
  ggtitle("Total Sample Size By ZCTA")

这会为您提供以下信息:

enter image description here

弄清楚颜色和尺寸定义的顺序是如何工作的。

希望这就是你的想法。
显然,您现在可以通过向情节添加附加层来添加“附加线”……但为此,您必须澄清您的问题或您想要实现的目标。

基于评论的修正:如果您想添加“纹理”类型的填充,现在有一个很酷的包 {ggpattern} 可以消除相当多的麻烦。查看软件包网页 https://statisticsglobe.com/ggpattern-r-package。 基于我们的示例,我将 pattern_fill 推入 aes() 映射以将其与变量挂钩(重新使用 my_color 分类变量,但选择您需要的)并定义“静态”填充 - aes() 之外的颜色。

ggplot() + 
    geom_polygon_pattern(data = my_map 
                 , aes(x=long, y=lat, group=group, fill = my_color
                       #------------------ now color and size get included in the mapping -------------
                       , color= my_color
                       , size = my_color 
                 #-------------------------- and supplied with a categorical variable
                       , pattern = my_color) # adding a mapping for the pattern
                 ,pattern_fill = "black"     # provide a static pattern fill color
                 #-------------------------- end of ggpattern/fill add-ons
    ) +
    scale_fill_manual(values = c("yellow","lightblue")) +
    coord_cartesian(xlim = c(-125, -117), ylim = c(45, 49)) +
    ggtitle("Total Sample Size By ZCTA")

这导致: figure with pattern fill

如上所述,通过分配分类变量来控制填充图案、颜色和大小,您可以更有创意。请容忍我,我选择这样做一次......而且非常简单。尝试适应您的用例所需的效果(类别)数量。