9

I am using R (ggplot2) to plot a graph. There I need to manually adjust the limits.

When I use the following code I get the warning: "Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale."

plot = ggplot(Light.d220, aes(x=V1, y=V2)) +
geom_raster(aes(fill=V3), interpolate=TRUE, hjust = 0, vjust = 0)  +
scale_fill_gradient(low = "red",high = "green") +
guides(fill = guide_colorbar(barwidth = 1, barheight = 10, title="Lux")) +
labs(x = "X", y = "Y", title= "Illumination at 2,2 m [Lux]") +
geom_text(aes(label=V3),size=5) +
xlim(-0.6,4.4) +
ylim(0,1.02) +
scale_y_reverse()

I know I get this error because of the "ylim", but I have not clue how to correct this.

It would be very nice if you could tell me, how to solve this.

Thank you very much :-)

user94499
  • 91
  • 1
  • 1
  • 2
  • 8
    Remove the `ylim` and change the last line to `scale_y_reverse(limits = 0, 1.02)`. `ylim` is basically a short-cut that you can use when you don't need to specify anything else for that axis. – Axeman Feb 17 '16 at 10:25
  • Thanks, sadly now get another error. If I just use ylim(0,1.02) or scale_y_reverse() everything is plotted. If I use your solution then I get an empty sheet plotted and warning messages saying: "1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to max; returning -Inf 3: In min(diff(sort(x))) : no non-missing arguments to min; returning Inf 4: Removed 27 rows containing missing values (geom_raster). 5: Removed 27 rows containing missing values (geom_text)." – user94499 Feb 17 '16 at 10:44
  • 4
    You don't have a working minimal example for me to play with, so I made a mistake. It probably should be `scale_y_reverse(limits = c(0, 1.02))`, or perhaps `scale_y_reverse(limits = c(1.02, 0))` – Axeman Feb 17 '16 at 10:46
  • 3
    Possible duplicate of [ggplot2 axis: how to combine scale\_x\_reverse with scale\_x\_continous](https://stackoverflow.com/questions/45986809/ggplot2-axis-how-to-combine-scale-x-reverse-with-scale-x-continous) – Roman Dec 23 '18 at 18:00
  • if you use any function with `scale_y...` in the beginning you should set limits within on of them with `limits = c(min,max)` An example is: `scale_y_continuous(limits = c(0,30),labels = number_format(suffix = "%")) +` – PotterFan Mar 15 '23 at 20:12

0 Answers0