简体   繁体   English

如何在ggplot中移动离散比例的图例?

[英]How to shift legend for discrete scales in ggplot?

I have a data frame, called df, with 4 columns and 43010 lignes. 我有一个名为df的数据框,有4列和43010个lignes。 The columns are X, Y, season, median. 列是X,Y,季节,中位数。 "X" and "Y" are lambert II coordinates of locations in France, "season" can have 5 values (Annual, Spring, Summer, Autumn and Winter), "median" is a number between -40 and 70 (a percentage in fact). “X”和“Y”是法国位置的朗伯II坐标,“季节”可以有5个值(年度,春季,夏季,秋季和冬季),“中位数”是介于-40和70之间的数字(百分比)事实)。

I want to plot a map of France for each season filled with the value "median". 我想为每个季节绘制一张法国地图,其中填充了“中位数”值。 As I want discrete classes, I transformed the values of the "median" column like this : 因为我想要离散类,我改变了“中间”列的值,如下所示:

vecP <- c(-40, -30, -20, -10, -5, 0, 5, 10, 20, 30, 70)
labP <- c("-40 ; -30", "-30 ; -20", "-20 ; -10", "-10 ; -5", "-5 ; 0", "0 ; 5", "5 ; 10", "10 ; 20", "20 ; 30", "30 ; 70")
df$sumBias <- cut(df$median, breaks=vecP, labels =labP)

So I have a fifth column with classes. 所以我有第五列的课程。 Now, to plot : 现在,绘图:

colorsP <- brewer.pal(length(labP), "RdBu")

pdf(file=paste(graph_wd,"Bias.pdf",sep=""), width=4, height=9, paper = "special")
p <- ggplot(data = df, aes(x=X, y=Y))
p <- (p    
      + theme_bw()
      + facet_wrap(~ season, ncol=1)
      + geom_tile(aes(fill= sumBias)) 
      + scale_fill_manual(name = "Bias (%)", values = setNames(colorsP, labP),breaks=rev(labP),labels=rev(labP),na.value="black") 
      + geom_path(data = polfrance, colour = 'black', 
              aes(x = long, y = lat, group = group)))

p <- (p  + scale_y_continuous(limits=c(1580000,2730000),
                          breaks=seq(1600000,2700000, 200000), 
                          labels= seq(1600,2700,200), expand=c(0,0))
      + scale_x_continuous(limits=c(0,1250000), 
                       breaks= seq(0,1250000, 400000), 
                       labels= seq(0,1250, 400), expand=c(0,0))
      + theme(panel.grid.major = element_blank(), axis.text=element_blank(), axis.ticks=element_blank(), axis.title=element_blank())
      + coord_fixed(ratio=1))
print(p)
dev.off()

It gives me this figure: 它给了我这个数字:

图。1

My problem is the legend. 我的问题是传说。 Is it possible to have one number between two classes ? 两个班级之间可以有一个号码吗? For example, "-30" between the two last red classes. 例如,最后两个红色类之间的“-30”。 Or something like that (the legend does not match, this is just to show what I want) : 或类似的东西(传说不匹配,这只是为了表明我想要的):

Fig2

Everything I tried always put one label next to one color class. 我尝试的所有东西总是在一个颜色类旁边放一个标签。

Thanks! 谢谢!

EDIT 编辑

With user20650 answer, I only modified, in the plot 有了user20650的答案,我只在剧情中进行了修改

      + scale_fill_manual(name = "Bias (%)", 
                      values = setNames(colorsP, labP), 
                      breaks=rev(labP),
                      labels=c("30", "20", "10", "5", "0", "-5", "-10", "-20", "-30", "-40"),
                      guide=guide_legend(label.vjust=-0.2),
                      na.value="black")

And it gives me this : 它给了我这个:

图三

I just need to add one more label for "70" at the top of the scale, any idea? 我只需要在比例的顶部添加一个“70”的标签,任何想法?

A bit of a crude fix and requires some manual adjustment but its a start?? 有点原始修复,需要一些手动调整,但它的开始? Idea is to keep every second label and use vjust to move the labels up slightly (this needs a bit of adjustment) 想法是保留每一个标签并使用vjust稍微向上移动标签(这需要一点调整)

library(ggplot2)
library(reshape2) 

# Reproducible data
df <- melt(outer(1:3, 1:3), varnames = c("X1", "X2"))

# plot
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = factor(value)))

# labels - keep every second label and weave in empty strings    
l1 <- levels(factor(df$value))[c(F,T)]
lbs <- c(rbind(rep("",length(l1)), l1))

p1 + scale_fill_discrete(labels=lbs, guide=guide_legend(label.vjust=1.1))

Which gives 这使

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM