ggplot图例标签和颜色

时间:2018-03-25 14:52:57

标签: r ggplot2 colors legend

我正在努力与ggplot一起制作这个人物的传奇故事。现在,我只做一张地图,当它工作时,我会在同一个地块中制作四张地图。
我想要一个这样的图例:底部,标题在中心和比例,标签和颜色之上,并省略NA值。

Desired output

这是我的代码:

阅读shapefile并安装新变量

map_ev@data$id = rownames(map_ev@data)  
map_ev.points = fortify(map_ev, region="id")  
map_ev.df = join(map_ev.points, map_ev@data, by="id")  
map_ev.df$median_norm = map_ev.df$median / map_ev.df$VOM

带主题的矢量选择ggplot

theme_opts <- list(theme(panel.grid.minor = element_blank(),  
                         panel.grid.major = element_blank(),  
                         panel.background = element_blank(),  
                         panel.border = element_blank(),  
                         axis.line = element_blank(),  
                         axis.text.x = element_blank(),  
                         axis.text.y = element_blank(),  
                         axis.ticks = element_blank(),  
                         axis.title.x = element_blank(),  
                         axis.title.y = element_blank(),  
                         plot.title = element_text(size=12, hjust=0.5),   
                         legend.position = "bottom",   
                         legend.title=element_blank()))  

安装变量中断

map_ev.df$median_norm <- cut(map_ev.df$median_norm, breaks=c(-200, -100, -50, -20, -5, 0, 5, 20, 50, 100, +200))

检查休息

levels(map_ev.df$median_norm)

要使用的颜色

color_map <- palette(c("#5b2e07", "#904d07", "#b98436", "#dfc27e", "#f6e8c3", 
                       "#c9e9e4", "#84cdc4", "#3c958f", "#01675a", "#073a31"))

ggplot代码

ggplot(map_ev.df) +   
  aes(long,lat,group=group,fill=median_norm, color=median_norm) +  
  geom_polygon() + geom_path(color="black") +   
  labs(title="Equivalent variation") + coord_equal() +   
  theme_opts

现在我得到这个数字:partial results

谢谢大家,感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我刚刚注意到您需要手动填充颜色。您可以使用:

myscale <- c("(-1,10]"="#BBDF27", "(10,20]"="#43BF71", "(20,30]"="#21908C", "(30,50]"="#35608D", "(50,101]"="#482576", "(101,300]"="#ffeeed")

结合:

scale_fill_manual(values=myscale,na.value="#e0e0e0",name="",labels=c("<10","10 - 20","20 - 30","30 - 50", ">50","Estimate", "NA")) 

其中scale_fill manual替换另一个答案中的scale_fill_brewer

只需用您自己的颜色和标签值替换即可。绘制没有任何标签的地图时,将显示myscale中需要输入的值。祝你好运!

答案 1 :(得分:0)

之所以没有标题,是因为您从未指定标题,如果您没有指定标题,则通过在legend.title=element_blank()中写入theme重新删除它。相反,我重写了它以指定标题应该居中。使用guides(colour = guide_legend(title.position = "top"),fill = guide_legend(title.position = "top",nrow=1,byrow=TRUE)),将位置设置为“顶部”,并确保它仅是图例项的一行。 (如果您确实需要,我在这里留下了colour=

您似乎还想要黑色边框,这意味着我删除了所有colour=,因为它指定了边框的颜色。

我添加了scale_fill_brewer,有更多可用的调色板。您可以在其中指定图例标题和图例标签的外观。顺便说一下,每次您更改输入数据时,都需要手动调整这些值。

抱歉,我不知道如何删除NA的图例项。

这是我想出的总代码:

ggplot(map_ev.df) +   
  aes(long,lat,group=group,fill=median_norm) + 
  scale_fill_brewer(palette="BrBG",name= "Title of legend",labels=c("-200 - -100","-100 - 50","-5 - 0","0 - 5", "label for NA"))+
  geom_polygon() + geom_path(color="black") +   
  labs(title="Equivalent variation") + coord_equal() +   
  theme(panel.grid.minor = element_blank(),  
        panel.grid.major = element_blank(),  
        panel.background = element_blank(),  
        panel.border = element_blank(),  
        axis.line = element_blank(),  
        axis.text.x = element_blank(),  
        axis.text.y = element_blank(),  
        axis.ticks = element_blank(),  
        axis.title.x = element_blank(),  
        axis.title.y = element_blank(),  
        plot.title = element_text(size=12, hjust=0.5),   
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.title = element_text(hjust=0.5))+
  guides(colour = guide_legend(title.position = "top"),fill = guide_legend(title.position = "top",nrow=1,byrow=TRUE))

使用我自己的一些数据进行填充: map

相关问题