根据geom_map或ggplot2中的应急(2x2)表创建独特的图例? [英] Create a unique legend based on a contingency (2x2) table in geom_map or ggplot2?
本文介绍了根据geom_map或ggplot2中的应急(2x2)表创建独特的图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
R中的可重复代码:
require(地图)
set.seed(123)
#为每个州随机分配2个变量
mappingData< - data.frame(state = tolower(rownames(USArrests)),
iceCream =(sample(c(喜欢冰淇淋,不喜欢冰淇淋),50,replace = T)),
hotDogs =(sample(c(喜欢热狗,不喜欢热狗),50,replace = T)))
#创建一个'legend'键对于指示变量
mappingDataDF< -data.frame(
expand.grid(iceCream = c(喜欢冰淇淋,不喜欢冰淇淋),
hotDogs = c (喜欢热狗,不喜欢热狗)),
indicator = c(0,1,2,3))
mapp ingData <-mappingData%>%inner_join(mappingDataDF)
mappingDatam< - reshape2 :: melt(mappingData,id = 1)
states_map< - map_data(状态)
ggplot(mappingData,aes(map_id = state))+
geom_map(aes(fill = indicator),map = states_map)+
expand_limits(x = states_map $ long,y = states_map $ lat)
解决方案
一些数据设置简化了示例。
库(地图)
库(dplyr)
库(ggplot2)
set.seed(123)
#为每个状态随机分配2个变量
mappingData< - data.frame(state = tolower(rownames(USArrests) ),
iceCream =(sample(c(No,Yes),50,replace = T)),
hotDogs =(sample(c(No,Yes 50,replace = T)))%>%
mutate(indicator = interaction(iceCream,hotDogs,sep =:))
mappingData
状态iceCream hotDogs指标
1 alabama否否否:否
2阿拉斯加是否是:否
3亚利桑那州否是否:是
4阿肯色州是否是:否
...
states_map< - map_data(state)
$ p $从数据中生成一个独立的图例
legend_ic.hd< - b
$ bggplot(mappingData,aes(iceCream,hotDogs,fill = indicator))+
geom_tile(show.legend = F)+
scale_x_discrete(Ice cream?,expand = c(0,0)) +
scale_y_discrete(Hot dogs?,expand = c(0,0))+
theme_minimal()+
theme(axis.text.y = element_text(angle = 90,hjust = 0.5))+
coord_equal()
legend_ic.hd
然后将它用作原始地图中的自定义注释
ggplot(mappingData,aes(map_id = state))+
geom_map(aes(fill = indicator),map = states_map,show.legend = F)+
expand_limits(x = states_map $ long,y = states_map $ lat)+
coord_quickmap()+
annotation_custom(grob = ggplotGrob(legend_ic.hd),
xmin = -79,xmax = Inf,
ymin = -Inf,ymax = 33)
您必须手动调整注释的位置,或者:
使用
gridExtra
(或cowplot
):
plot_ic.hd< - ggplot(mappingData,aes(map_id = state))+
geom_map(aes(fill = indicator),map = states_m ap,show.legend = F)+
expand_limits(x = states_map $ long,y = states_map $ lat)+
coord_quickmap()
gridExtra :: grid.arrange( grobs = list(plot_ic.hd,legend_ic.hd),
ncol = 2,widths = c(1,0.33))
How can I do this based on this contingency table? I'm not entirely sure how to create a custom legend in R based on the indicator table I made (crimes).
Reproducible code in R:
require(maps) set.seed(123) # randomly assign 2 variables to each state mappingData <- data.frame(state = tolower(rownames(USArrests)), iceCream = (sample(c("Likes Ice Cream","Doesn't Like Ice Cream"),50, replace=T)), hotDogs = (sample(c("Likes Hot Dogs","Doesn't Like Hot Dogs"),50, replace=T))) # create a 'legend' key for an indicator variable mappingDataDF<-data.frame( expand.grid(iceCream=c("Likes Ice Cream","Doesn't Like Ice Cream"), hotDogs=c("Likes Hot Dogs","Doesn't Like Hot Dogs")), indicator=c("0","1","2","3")) mappingData<-mappingData %>% inner_join(mappingDataDF) mappingDatam <- reshape2::melt(mappingData, id = 1) states_map <- map_data("state") ggplot(mappingData, aes(map_id = state)) + geom_map(aes(fill = indicator), map = states_map) + expand_limits(x = states_map$long, y = states_map$lat)
解决方案I altered some of your data setup to simplify the example.
library(maps) library(dplyr) library(ggplot2) set.seed(123) # randomly assign 2 variables to each state mappingData <- data.frame(state = tolower(rownames(USArrests)), iceCream = (sample(c("No", "Yes"), 50, replace=T)), hotDogs = (sample(c("No", "Yes"), 50, replace=T))) %>% mutate(indicator = interaction(iceCream, hotDogs, sep = ":")) mappingData
state iceCream hotDogs indicator 1 alabama No No No:No 2 alaska Yes No Yes:No 3 arizona No Yes No:Yes 4 arkansas Yes No Yes:No ...
states_map <- map_data("state")
Generate an independent legend from the data
legend_ic.hd <- ggplot(mappingData, aes(iceCream, hotDogs, fill = indicator)) + geom_tile(show.legend = F) + scale_x_discrete("Ice cream?", expand = c(0,0)) + scale_y_discrete("Hot dogs?", expand = c(0,0)) + theme_minimal() + theme(axis.text.y = element_text(angle = 90, hjust = 0.5)) + coord_equal() legend_ic.hd
Then use it as a custom annotation in the original map
ggplot(mappingData, aes(map_id = state)) + geom_map(aes(fill = indicator), map = states_map, show.legend = F) + expand_limits(x = states_map$long, y = states_map$lat) + coord_quickmap() + annotation_custom(grob = ggplotGrob(legend_ic.hd), xmin = -79, xmax = Inf, ymin = -Inf, ymax = 33)
You have to adjust the location of the annotation manually, or:
Use
gridExtra
(orcowplot
):plot_ic.hd <- ggplot(mappingData, aes(map_id = state)) + geom_map(aes(fill = indicator), map = states_map, show.legend = F) + expand_limits(x = states_map$long, y = states_map$lat) + coord_quickmap() gridExtra::grid.arrange(grobs = list(plot_ic.hd, legend_ic.hd), ncol = 2, widths = c(1,0.33))
这篇关于根据geom_map或ggplot2中的应急(2x2)表创建独特的图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文