[英]R: Filling a polygon with a solid color and lines
我目前正在使用 ggplot 創建 state map 並且在弄清楚如何有條件地將線條和顏色添加到 map 中的多邊形時遇到了一些麻煩。 我的數據集中有一個變量“行”。 當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 的行層上?
目前尚不清楚您所說的“線條”是什么,並且由於沒有可重復的示例,因此我從{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")
注意geom_polygon()
調用中的color
和fill
控件。
如果您想有條件地更改填充顏色,我們可以將此“微分器”作為一列添加到我們的 map dataframe 中。 您可以將其作為分類變量添加到 dataframe。 我使用簡單的 TRUE/FALSE(又名二進制分類變量)。 在scale_fill_manual()
中控制類別的顏色順序。 重要的是fill
現在是一個變量,您可以對其應用“美學映射”。 因此,它需要 go 進入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")
將它放在后袋中,您現在可以處理多邊形線(也稱為邊界)。 color
參數定義線條顏色。 如果你想“映射”它,你可以以類似的方式有條件地做到這一點。 我將相同的原則應用於線條顏色和線條寬度,即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")
這將為您提供以下信息:
弄清楚你的顏色和尺寸定義的順序是如何工作的。
希望這是你的想法。
顯然,您現在可以通過在 plot 中添加附加層來添加“附加行”......但為此,您必須澄清您的問題或您想要實現的目標。
基於評論的修正:如果你想添加“紋理”類型的填充,現在有一個很酷的 package {ggpattern}
可以消除一些麻煩。 查看 package 網頁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")
如上所述,您可以通過分配分類變量來控制填充圖案、顏色和大小來更有創意。 請耐心等待我選擇這樣做一次......而且非常簡單。 玩轉你需要適應你的用例的效果(類別)的數量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.