两个极地ggplot的传说(一个定制) [英] Two legends for polar ggplot (with one customized)

查看:149
本文介绍了两个极地ggplot的传说(一个定制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据:

  data<  -  structure(list(Indicator = structure(c(1L,1L, 1L,2L,2L,2L,
3L,3L,3L,6L,6L,6L,6L,4L,4L,5L,5L,1L,1L,1L,2L,2L,
2L, 3L,3L,3L,6L,6L,6L,6L,4L,4L,5L,5L,1L,1L,1L,2L,
2L,2L,3L,3L,3L,6L,6L, 6L,4L,4L,5L,5L,1L,1L,1L,
2L,2L,2L,3L,3L,3L,6L,6L,6L,6L,4L,4L,5L,5L, 1L,
1L,2L,2L,2L,3L,3L,3L,6L,6L,6L,6L,4L,4L,5L,5L)。标签= c(因果性,
Climatechangeriskperceptions,Currentadaptationoptions,Fishingasalivelihoodactivity,
Governance,Roleofshadowstateactors),class =factor),
Village = structure(c(1L,1L,1L,1L, 1L,1L,1L,1L,1L,
1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,2L,
2L, 2L,2L,2L,2L,2L,2L,2L,2L,2L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L, 3L,3L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L, 5L,
5L,5L,5L,5L,5L,5L,5L,5L,5L,5L,5L,5L,5L,5L,5L,
5L),标签= c( ,lahapau,Pelipowai,Ponam,
Tulu),class =factor),变量=结构(c(13L,
3L,10L,11L,12L, 16L,5L,8L,1L,2L,15L,17L,6L,14L,
9L,4L,7L,13L,3L,10L,11L,12L,16L,5L,8L,1L,2L, $ b 15L,17L,6L,14L,9L,4L,7L,13L,3L,10L,11L,12L,16L,
5L,8L,1L,2L,15L,17L,6L,14L, 4L,7L,13L,3L,10L,
11L,12L,16L,5L,8L,1L,2L,15L,17L,6L,14L,9L,4L,
7L,13L, 10L,11L,12L,16L,5L,8L,1L,2L,15L,17L,
6L,14L,9L,4L,7L)。标签= c(alternativelivelihood,
anyactorsinvolvedinsustainability bbbb讨论选定的领导者,努力获得指导,
渔民社区影响,基础设施效能,
多重因素,占领儿童,可逆性,
Riskasamajor考虑因素,风险承担,状态阉割,
捕鱼时间,Whatwasdone,Whoisatrisk,whowasinvolved?
),class =factor),legend.var = structure (1L,2L,3L,
4L,5L,6L,7L,8L,9L,10L,11L,12L,13L,14L,15L,16L,
17L,1L,2L,3L, 4L,5L,6L,7L,8L,9L,10L,11L,12L,13L,
14L,15L,16L,17L,1L,2L,3L,4L,5L,6L,7L,8L, 10L,
11L,12L,13L,14L,15L,16L,17L,1L,2L,3L,4L,5L,6L,
7L,8L,9L,10L,11L,12L,13L, 14L,15L,16L,17L,1L,2L,
3L,4L,5L,6L,7L,8L,9L,10L,11L,12L,13L,14L,15L,
16L,17L) ,.Label = c(a,b,c,d,e,f,g,
h,i,j, k,l,m,n,o,p,q),class =factor),
score = c(1,0.97,1, 0.76,0.794,1,0.71,0.9,0.5,1,
1,1,1,0.49,0.72,1,0.7,1,1,0,0.67,0.5,1,0.2,
1,1,0.7,0.4,0.5,0.3,0.67,0.5,0.7,0.8,1.0,0.46,
0.56, 0.3,0.5,0.3,0.3,0.4,0.6,1,1,
0.7,0.8,1,0.86,0.69,0.51,0.429,1,0.44,0.3,0.5,
0.6, 0.7,0.8,0.4,0.79,0.8,1,1,0.82,0.85,0.25,
0.226,1,0.18,0.1,0.7,0.3,0.6,0.3,0.48,0.16,0.4,
0.8)),.Names = c(Indicator,Village,Variables,legend.var,
score),class =data.frame,row.names = c(NA,-85L))

我已经做出如下极坐标图:

  library(ggplot2)
ggplot(data,aes(x = legend.var,y = score,fill = Indicator),color = ')+
geom_bar(width = 1,alpha = 0.5,stat =identity)+
scale_y_continuous()+
coord_polar()+
主题(轴。 ticks = element_blank())+
facet_wrap(〜Village,nrow = 2,ncol = 3)+
guides(color = guide_legend(title.hjust = 0.5))



尽可能请参阅数据栏中 legend.var 按列指标分组。我想插入一个表或第二个图例,将每个指标与 legend.var 变量关联起来。柱。理想情况下,如果将其作为第二个图例插入,则 legend.var 列中包含的唯一项目将具有与相应指标相同的填充颜色。填充图例基于列指标。插入的表格/附加图例将包含具有唯一字母表和列变量的列 legend.var ,其含义为各自的字母。这些可以具有与相应的指标相同的填充颜色。我希望这是明确的。

解决方案

这是一个使用 gtable

  library(ggplot2)
p < - ggplot(data,aes(x = legend.var,y =),color ='black')+
geom_bar(width = 1,alpha = 0.5,stat =identity)+
scale_y_continuous()+
coord_polar )+
主题(axis.ticks = element_blank())+
facet_wrap(〜Village,nrow = 2,ncol = 3)+
guides(color = guide_legend(title.hjust = 0.5) ))+
theme(legend.position = c(0.85,0.25))

#create table $ b $ library(gridExtra)
tab< - tableGrob(unique (data [,c(legend.var,Variables)]),
show.rownames = FALSE,gpar.coretext = gpar(fontsize = 10),
gpar.coltext = gpar( fontsize = 10,fontface ='bold'),
gpar.corefill = gpar(fill =grey90,col =white),
gpar.colfill = gpar(fill =grey80, col =white))
单元(c(0.7,0.3),c(npc)),单元(1,npc) )
a < - gtable_add_grob(a,ggplotGrob(p),1,1)
a < - gtable_add_grob(a,tab,1,2)

#plot
grid.draw(a)


Here is my data:

data <- structure(list(Indicator = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 1L, 2L, 2L, 
2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 1L, 2L, 
2L, 2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 1L, 
2L, 2L, 2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 
1L, 2L, 2L, 2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L), .Label = c("Causality", 
"Climatechangeriskperceptions", "Currentadaptationoptions", "Fishingasalivelihoodactivity", 
"Governance", "Roleofshadowstateactors"), class = "factor"), 
    Village = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L), .Label = c("Andra", "lahapau", "Pelipowai", "Ponam", 
    "Tulu"), class = "factor"), Variables = structure(c(13L, 
    3L, 10L, 11L, 12L, 16L, 5L, 8L, 1L, 2L, 15L, 17L, 6L, 14L, 
    9L, 4L, 7L, 13L, 3L, 10L, 11L, 12L, 16L, 5L, 8L, 1L, 2L, 
    15L, 17L, 6L, 14L, 9L, 4L, 7L, 13L, 3L, 10L, 11L, 12L, 16L, 
    5L, 8L, 1L, 2L, 15L, 17L, 6L, 14L, 9L, 4L, 7L, 13L, 3L, 10L, 
    11L, 12L, 16L, 5L, 8L, 1L, 2L, 15L, 17L, 6L, 14L, 9L, 4L, 
    7L, 13L, 3L, 10L, 11L, 12L, 16L, 5L, 8L, 1L, 2L, 15L, 17L, 
    6L, 14L, 9L, 4L, 7L), .Label = c("alternativelivelihood", 
    "anyactorsinvolvedinsustainability", "Attributionfactors", 
    "discusswithelectedleaders", "Effortsdirectedtoreducerisks", 
    "fishercommunityinfluence", "Infrastructureeffectiveness", 
    "multiplicityofactors", "Occupationforchildren", "Reversibility", 
    "Riskasamajorconsideration", "Riskbeingaddressed", "Statusoffisheries", 
    "Timefishing", "Whatwasdone", "Whoisatrisk", "whowasinvolved?"
    ), class = "factor"), legend.var = structure(c(1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
    17L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
    14L, 15L, 16L, 17L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
    11L, 12L, 13L, 14L, 15L, 16L, 17L, 1L, 2L, 3L, 4L, 5L, 6L, 
    7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
    16L, 17L), .Label = c("a", "b", "c", "d", "e", "f", "g", 
    "h", "i", "j", "k", "l", "m", "n", "o", "p", "q"), class = "factor"), 
    score = c(1, 0.97, 1, 0.76, 0.794, 1, 0.71, 0.9, 0.5, 1, 
    1, 1, 1, 0.49, 0.72, 1, 0.7, 1, 1, 0, 0.67, 0.5, 1, 0.2, 
    1, 1, 0.7, 0.4, 0.5, 0.3, 0.67, 0.5, 0.7, 0.8, 1, 0, 0.46, 
    0.56, 0.375, 1, 0.13, 0.3, 0.5, 0.3, 0.3, 0.4, 0.6, 1, 1, 
    0.7, 0.8, 1, 0.86, 0.69, 0.51, 0.429, 1, 0.44, 0.3, 0.5, 
    0.6, 0.6, 0.7, 0.8, 0.4, 0.79, 0.8, 1, 1, 0.82, 0.85, 0.25, 
    0.226, 1, 0.18, 0.1, 1, 0.7, 0.3, 0.6, 0.3, 0.48, 0.16, 0.4, 
    0.8)), .Names = c("Indicator", "Village", "Variables", "legend.var", 
"score"), class = "data.frame", row.names = c(NA, -85L))

I have made polar plots as follows:

library(ggplot2)
ggplot(data, aes(x = legend.var, y = score, fill = Indicator), color='black') + 
  geom_bar(width = 1, alpha=0.5, stat="identity") + 
  scale_y_continuous() + 
  coord_polar()  + 
  theme( axis.ticks = element_blank()) + 
  facet_wrap(~Village, nrow=2, ncol=3) + 
  guides(colour = guide_legend(title.hjust = 0.5))

As you can see in the data, column legend.var is grouped by the column indicator. I would like to insert a table or a second legend which associates each of the indicator with the legend.var and variables column. Ideally if this is inserted as a second legend, the unique items comprised in legend.var column would have the same fill color as the corresponding indicator. The fill legend is based on the column indicator. The inserted table/extra legend would comprise columns legend.var with a unique alphabet and column variables with the meaning of the respective alphabet. These can then have the same fill color as the corresponding indicator. I hope this is clear.

解决方案

Here is a solution using a gtable:

library(ggplot2)
p <- ggplot(data, aes(x = legend.var, y = score, fill = Indicator), color='black') + 
  geom_bar(width = 1, alpha=0.5, stat="identity") + 
  scale_y_continuous() + 
  coord_polar()  + 
  theme( axis.ticks = element_blank()) + 
  facet_wrap(~Village, nrow=2, ncol=3) + 
  guides(colour = guide_legend(title.hjust = 0.5)) +
  theme(legend.position=c(0.85,0.25))

#create table
library(gridExtra)
tab <- tableGrob(unique(data[, c("legend.var", "Variables")]), 
                 show.rownames=FALSE, gpar.coretext=gpar(fontsize=10), 
                 gpar.coltext=gpar(fontsize=10, fontface='bold'),
                 gpar.corefill = gpar(fill = "grey90", col = "white"),
                 gpar.colfill = gpar(fill = "grey80", col = "white"))

#arrange grobs
library(gtable)
a <- gtable(unit(c(0.7, 0.3) ,c("npc")), unit(1, "npc"))
a <- gtable_add_grob(a, ggplotGrob(p),1,1)
a <- gtable_add_grob(a, tab,1,2)

#plot
grid.draw(a)

这篇关于两个极地ggplot的传说(一个定制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆