1

The information for scale_color_viridis_c states:

scale_colour_viridis_c(
  ...,
  alpha = 1,
  begin = 0,
  end = 1,
  direction = 1,
  option = "D",
  values = NULL,
  space = "Lab",
  na.value = "grey50",
  guide = "colourbar",
  aesthetics = "colour"
)

I want to specify a color palette ranging from very light blue to darke blue, but I don't know how to relate the 'alpha','begin' and 'end' (which relate to hue) to RGB values? Is there a way to find what 'alpha' to start at if you want a specific color?

#Create a dataset

vap=array(1:100, c(351,451,365))

#create the list
Variable <- list(Data = vap)
xyCoords <- list(x = seq(from = -10.763300, to = -5.238170, by = 0.01578607), y = seq(from = 51.36150, to = 55.43160, by = 0.009044647))
Dates <- list(start = seq(as.Date("2005-01-01"), as.Date("2005-12-31"), by="days"), end=seq(as.Date("2005-01-01"), as.Date("2005-12-31"), by="days"))
#All <- list(Variable = Variable, xyCoords=xyCoords,Dates=Dates)
All <- list(Variable = Variable,Data=vap, xyCoords=xyCoords,Dates=Dates)

data<-aperm(All$Data)
#----------------
# Make dimensions
#----------------
xvals<- All$xyCoords$x
yvals<- All$xyCoords$y

nx <- length(xvals)
ny <- length(yvals)

xdim2 <- ncdim_def( 'lon', 'degrees_east',longname="longitude", xvals )
ydim2 <- ncdim_def( 'lat', 'degrees_north',longname="latitude", yvals )
tdim2 <- ncdim_def( 'time', 'months since 1949-12-01 00:00:00Z',longname="time", 0:11, unlim=TRUE )
#---------
# Make var
#---------
mv <- 1.00000002004088e+20     # missing value
var_hur <- ncvar_def( 'hur', '%', list(xdim2,ydim2,tdim2), mv )

#---------------------
# Make new output file
#---------------------
output_fname <- "test.nc"
ncid_hur <- nc_create( output_fname, list(var_hur))

#-------------------------------
# Put the data in the file
#-------------------------------
ncvar_put( ncid_hur, var_hur, data, start=c(1,1,1), count=c(nx,ny,365))

nc_close( ncid_hur)

#reopen the file

ncin <- nc_open("test.nc")

# get longitutes and latitudes 
lon <- ncvar_get(ncin,"lon") 

lat <- ncvar_get(ncin,"lat", verbose=F) 
#get the data
masked<-ncvar_get(ncin,"hur")
mydata<-masked[,,1]

#get it ready for plotting
permdata<-aperm(mydata,c(2,1))
gridtest<-expand.grid(lon,lat)
renametest<-rename(gridtest,lon = Var1, lat = Var2)
d <- mutate(renametest, tas = as.vector(permdata))

#plot it using ggplot:

ggplot() + 
  geom_point(aes(x = d$lon, y = d$lat, color = d$tas),
             size = 0.8) + 
  # borders("world", colour="black", fill=TRUE) + 
  lims(x = c(-10.77119, -5.230277), y = c(51.35698, 55.43612)) +     
  scale_color_viridis(na.value="white", limits= c(0,250), name = "Precipitation [mm]") +    
  theme(legend.direction="vertical", legend.position="right", legend.key.width=unit(0.4,"cm"), legend.key.heigh=unit(2,"cm")) + 
  coord_quickmap(clip = "on") + 
  ggtitle("titletext") 

I want to change the colorscale to go from light blue to dark blue, so I tried changing the line above to:

scale_colour_viridis_c(d$tas,alpha = 0.5,begin = 0.6,end = .8,direction = 1,option = "G",values = NULL,space = "Lab",na.value = "transparent",guide = "colourbar",aesthetics = "colour")

I was just randomly changing the alpha,begin and end to see what color palette I would get, but I'm sure there is probably a way to define a color palette using RGB or something similar to sample from?

Additionally, I noticed when I changed this line of code and tried to add in a legend.title="title", it wouldn't plot:

Error in `mapply()`:
! The `LEGEND.TITLE` theme element is not defined in the element hierarchy.
Run `rlang::last_error()` to see where the error occurred.
matlabcat
  • 172
  • 2
  • 12

1 Answers1

1

Here is another approach how to get a gradient from light to dark blue in ggplot:

ggplot uses a blue color scale by default for continuous variables (here: hp)

ggplot(data = mtcars, aes(x = mpg, y = disp, color = hp)) + 
  geom_point(size = 4)

enter image description here

If you want to tweak this we could use scale_color_gradient():

ggplot(data = mtcars, aes(x = mpg, y = disp, color = hp)) + 
  geom_point(size = 4) +
  scale_color_gradient(low = "lightblue", high = "darkblue")

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66