Question

This is supposed to be a super easy question, but I am still learning ggplot and can't make it work.

I would like to fix the minimum and maximum value of the colourbar in this plot, to compare the plot with others graphically, I mean to have the minimum value in the legend to be e.g. -2 and the maximum to be 4.

I tried limits but couldn't make it work.

require(ggmap)
colormap <- c("Violet","Blue","Green","Yellow","Red","White")

map.in <- get_map(location = c(24.9072, 60.1477, 24.9623, 60.1752), source = "osm")

sim.data <- ggmap(map.in) %+% samplesnnsp + 
  aes(x = samplesnnsp[,1],
      y = samplesnnsp[,2],
      z = samplesnnsp[,3])+
  stat_summary2d(fun = median, 
                 binwidth = c(.0005, .0005),
                 alpha = 0.4) + 
  scale_fill_gradientn(name = "Median",
                       colours = colormap,
                       space = "Lab") + 
  labs(x = "Longitude",
       y = "Latitude") +
  coord_map()

print(sim.data)

some data could be

samplesnnsp <-

         lon     lat    var1.pred var1.var
1   24.90720 60.1477 -0.459263483       NA
2   24.90734 60.1477 -1.470819439       NA
3   24.90748 60.1477  0.341401207       NA
4   24.90761 60.1477 -0.627459141       NA
5   24.90775 60.1477 -0.945025977       NA
6   24.90789 60.1477  1.593166965       NA
7   24.90803 60.1477  1.388875605       NA
8   24.90817 60.1477  1.573702349       NA
9   24.90830 60.1477 -1.456883473       NA
10  24.90844 60.1477 -0.044724669       NA
11  24.90858 60.1477  0.492260162       NA
12  24.90872 60.1477  1.301121758       NA
13  24.90886 60.1477 -0.995094857       NA
14  24.90900 60.1477 -0.134303726       NA
15  24.90913 60.1477  0.742252392       NA
16  24.90927 60.1477 -1.252147160       NA
17  24.90941 60.1477 -0.526063594       NA
18  24.90955 60.1477  0.244057093       NA
19  24.90969 60.1477 -0.509438179       NA
20  24.90982 60.1477  0.240845774       NA
21  24.90996 60.1477  1.214138185       NA
22  24.91010 60.1477  0.169762721       NA
23  24.91024 60.1477  0.064761291       NA
24  24.91038 60.1477  2.135645208       NA
25  24.91051 60.1477 -0.320435955       NA
26  24.91065 60.1477  0.192032441       NA
27  24.91079 60.1477  1.717479829       NA
28  24.91093 60.1477  0.917301565       NA
29  24.91107 60.1477 -0.532562593       NA
30  24.91120 60.1477  0.963605718       NA
31  24.91134 60.1477  0.397451616       NA
32  24.91148 60.1477  1.818823108       NA
33  24.91162 60.1477  0.345882622       NA
34  24.91176 60.1477 -0.655113243       NA
35  24.91190 60.1477 -0.052475131       NA
36  24.91203 60.1477 -0.533822804       NA
37  24.91217 60.1477 -1.192470544       NA
38  24.91231 60.1477 -1.004180985       NA
39  24.91245 60.1477 -1.531023312       NA
40  24.91259 60.1477 -0.629444162       NA
41  24.91272 60.1477  0.282512182       NA
42  24.91286 60.1477  0.560439088       NA
43  24.91300 60.1477 -0.922094517       NA
44  24.91314 60.1477 -0.121183201       NA
45  24.91328 60.1477  0.897210951       NA
46  24.91341 60.1477  1.598458357       NA
47  24.91355 60.1477 -0.023978283       NA
48  24.91369 60.1477  0.979520714       NA
49  24.91383 60.1477 -1.558292167       NA
50  24.91397 60.1477 -0.857758865       NA
51  24.91410 60.1477  2.436103790       NA
52  24.91424 60.1477 -0.716014292       NA
53  24.91438 60.1477 -1.509418144       NA
54  24.91452 60.1477  0.421319628       NA
55  24.91466 60.1477  0.134673030       NA
56  24.91480 60.1477  0.236527420       NA
57  24.91493 60.1477 -0.131870867       NA
58  24.91507 60.1477 -0.764709492       NA
59  24.91521 60.1477 -1.125855959       NA
60  24.91535 60.1477 -0.835676397       NA
61  24.91549 60.1477 -0.010050297       NA
62  24.91562 60.1477 -0.689060241       NA
63  24.91576 60.1477 -0.255953439       NA
64  24.91590 60.1477  1.503940499       NA
65  24.91604 60.1477 -0.647302524       NA
66  24.91618 60.1477  0.032949746       NA
67  24.91631 60.1477 -0.302106163       NA
68  24.91645 60.1477 -0.869169561       NA
69  24.91659 60.1477 -1.125110141       NA
70  24.91673 60.1477  0.028541911       NA
71  24.91687 60.1477 -0.519090322       NA
72  24.91700 60.1477 -0.029579821       NA
73  24.91714 60.1477  0.208527926       NA
74  24.91728 60.1477  0.893646836       NA
75  24.91742 60.1477 -0.887658105       NA
76  24.91756 60.1477  1.304644066       NA
77  24.91770 60.1477 -2.235914148       NA
78  24.91783 60.1477 -0.732714812       NA
79  24.91797 60.1477 -1.068315393       NA
80  24.91811 60.1477  0.989133329       NA
81  24.91825 60.1477 -0.658703883       NA
82  24.91839 60.1477  1.174113274       NA
83  24.91852 60.1477 -2.435087039       NA
84  24.91866 60.1477 -1.462809655       NA
85  24.91880 60.1477  0.433320476       NA
86  24.91894 60.1477  0.203231497       NA
87  24.91908 60.1477 -0.654920115       NA
88  24.91921 60.1477 -0.247426889       NA
89  24.91935 60.1477 -1.282573973       NA
90  24.91949 60.1477 -0.189281908       NA
91  24.91963 60.1477  0.004738164       NA
92  24.91977 60.1477  0.619994186       NA
93  24.91990 60.1477 -1.553654198       NA
94  24.92004 60.1477 -0.260587710       NA
95  24.92018 60.1477 -0.316618098       NA
96  24.92032 60.1477 -0.617153029       NA
97  24.92046 60.1477 -1.496197677       NA
98  24.92060 60.1477 -1.433603039       NA
99  24.92073 60.1477 -0.549665645       NA
100 24.92087 60.1477 -0.315078410       NA
Was it helpful?

Solution

You can add expand_limits(fill = seq(from = -2, to = 4, by = 1)) to your code. expand_limits "specif[ies] the value (or values) that should be included in each scale." enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top