I have tried unsuccessfully to reverse the direction of the legend color ramp in a ggplot2 raster graph. I want the temperature ranges to be ordered from highest to lowest without changing the color assignment of the variable.
The dataframe was built from a raster: EneroT5cmSC
datene <- as.data.frame(EneroT5cmSC,xy=TRUE)%>%drop_na()
datene$cuts <- cut(datene$layer, breaks=seq(21, 29, length.out=12))
ggplot2 code:
p1 <-ggplot()+
geom_raster(aes(x=x,y=y,fill=cuts),data=datene_stuc)+
geom_sf(fill='transparent',data=conturu)+
geom_sf(fill='transparent',data=locuru)+
scale_fill_viridis_d( option = "B", 'Temp (Cº)')+
theme_minimal()+
theme(axis.title.x=element_blank(),
axis.title.y=element_blank())+
labs(title="Soil Temperature at 5cm depth",
subtitle='January',
caption='Fagro, 2022')
Graph:
Legend in reverse direction with correct color assignment
dput:
datene_stuc <-
structure(
list(
x = c(-57.063098328,-57.021448328,-56.996458328,-56.988128328),
y = c(-30.087481664,-30.087481664,-30.087481664,-30.087481664),
layer = c(
25.6227328470624,
26.6386584334308,
26.0636709134397,
26.0580615984563
),
cuts = structure(
c(7L, 9L,
8L, 8L),
.Label = c(
"(20,20.8]",
"(20.8,21.6]",
"(21.6,22.5]",
"(22.5,23.3]",
"(23.3,24.1]",
"(24.1,24.9]",
"(24.9,25.7]",
"(25.7,26.5]",
"(26.5,27.4]",
"(27.4,28.2]",
"(28.2,29]"
),
class = "factor"
)
),
row.names = c(NA,
4L),
class = "data.frame")