3

I have a data frame, called df, with 4 columns and 43010 lignes. The columns are X, Y, season, median. "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).

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:

Fig1

My problem is the legend. Is it possible to have one number between two classes ? For example, "-30" between the two last red classes. 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

      + 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 :

Fig3

I just need to add one more label for "70" at the top of the scale, any idea?

Chika
  • 1,455
  • 2
  • 16
  • 24
  • 3
    Possible duplicate of [Generating a color legend with shifted labels using ggplot2](https://stackoverflow.com/questions/26802272/generating-a-color-legend-with-shifted-labels-using-ggplot2) – AF7 Jan 24 '18 at 11:05

1 Answers1

1

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)

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

enter image description here

user20650
  • 24,654
  • 5
  • 56
  • 91
  • Thank you for your answer. I edited my post to take into account your comment. It almost works, I have just a problem because I have 10 different possible labels but I need to add one more label to specify the top bound. – Chika Jan 30 '15 at 08:52
  • Hi Chika, i am not quite sure which values you want, as your edited figure does not seem to only show non-consecutive values . Try with a simple [`reproducible example`](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) : What values out of the legend that is produced by the commands below do you want to keep? `df <- data.frame(X1=1:10, X2=1:10, value=1:10) ; ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = factor(value)))`. Thanks – user20650 Jan 31 '15 at 14:02
  • It wasn't pertinent to use a variable scale so I changed it and the trick you gave me with label.vjust works well. Thanks! – Chika Feb 03 '15 at 09:39