0

I am plotting integer values in (Z) as raster file..In the Z values there is number 6 is not-existing , however i wish to assign a fill color for it and show it in the legend.. How can i do it? Below is the data, where X and Y correspond to longitude and latitude..

      X     Y   Z
84  75.75 12.50 7
85  76.00 12.50 5
86  76.25 12.50 5
87  76.50 12.50 2
88  76.75 12.50 7
89  77.00 12.50 4
100 75.75 12.25 2
101 76.00 12.25 4
102 76.25 12.25 2
103 76.50 12.25 3
104 76.75 12.25 2
105 77.00 12.25 4
116 75.75 12.00 1
117 76.00 12.00 1
118 76.25 12.00 2
119 76.50 12.00 3
120 76.75 12.00 2
121 77.00 12.00 5
132 75.75 11.75 1
133 76.00 11.75 0
134 76.25 11.75 1
135 76.50 11.75 0
136 76.75 11.75 1
137 77.00 11.75 0
148 75.75 11.50 1
149 76.00 11.50 0
150 76.25 11.50 1
151 76.50 11.50 2
152 76.75 11.50 2
153 77.00 11.50 4

I had tried with the below code..

  [![p1 <-ggplot(XYZ.1)+ geom_raster(aes(X,Y,fill=Z)) +
    coord_equal()+theme_bw()+xlab("")+ylab("") +
    scale_fill_manual(values = c("grey50", "#f7f7f7", "#80b1d3","#7fc97f","#ffff99","#beaed4","#f0027f"),name= "Integer", guide = guide_legend(reverse = TRUE)) +
    scale_x_continuous(breaks=seq(75.625,77.125, 0.25), labels=c(paste(seq(75.625,77.125, 0.25),"°E", sep="")),expand = c(0, 0))+
    scale_y_continuous(breaks=seq(11.375,12.625,0.25), labels=c(paste(seq(11.375,12.625,0.25),"°N", sep="")),expand = c(0, 0)) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1),panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
    theme(plot.title = element_text(hjust = 0.5,size = 10, face = "plain"),legend.title=element_text(size=10),legend.text=element_text(size=9))][1]][1] 

1: enter image description here

user1142937
  • 314
  • 5
  • 19

1 Answers1

1

Define Z as a factor with levels from 0 to 7 and use drop=FALSE inside scale_fill_manual:

XYZ.1$Z <- factor(XYZ.1$Z, levels=0:7)

p1 <- ggplot(XYZ.1)+ geom_raster(aes(X,Y,fill=Z)) +
   coord_equal() + theme_bw() + xlab("") + ylab("") +
   scale_fill_manual(values = c("grey50","#f7f7f7","#80b1d3","#7fc97f","#ffff99",
                                "#beaed4","red","#f0027f"), 
        name= "Integer", guide=guide_legend(reverse=TRUE), drop=FALSE) +
   geom_polygon(data=shp.Kabini.basin.bndry, aes(long,lat,group=group),
                colour="black", fill=NA)+
   scale_x_continuous(breaks=seq(75.625,77.125, 0.25), 
        labels=c(paste(seq(75.625,77.125, 0.25),"°E", sep="")),expand = c(0, 0))+
   scale_y_continuous(breaks=seq(11.375,12.625,0.25), 
        labels=c(paste(seq(11.375,12.625,0.25),"°N", sep="")),expand = c(0, 0)) +
   theme(axis.text.x = element_text(angle = 90, hjust = 1),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
   theme(plot.title = element_text(hjust = 0.5,size = 10, face = "plain"),
        legend.title=element_text(size=10), legend.text=element_text(size=9))
print(p1)

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
  • @ Marco Sandri.. Thanks a lot.. I was using `as.factor(XYZ.1$Z)`.. Did'nt think of `factor` with 7 levels...Can you also tell me if i wish to create another case of giving 1 color for a range lets say [0,2] "red".. I am aware of using `cut`..However, again if the case arises where there is no value..But we want to show color in colorbar.. – user1142937 May 01 '18 at 13:13
  • 1
    The output of the `cut` function is a factor with all the specified intervals as levels, even if some levels/intervals are empty. Try for example `x <- runif(100); x <- x[x<0.4 | x>0.6]; y <- cut(x, breaks=seq(0,1,0.1)); table(y)` – Marco Sandri May 01 '18 at 13:26
  • @ Marco Sandri..Thanks a lot again..I got it.. – user1142937 May 01 '18 at 13:28