Scale_fill in ggplot2 - Make a gradient color for each chart in the ggplots list

I am making a list of ggplots stored in an object called ga . Here is the code for playing the charts (the data are indicated below).

It is worth noting that I probably missed something simple and overly complicated the problem.

 RCfitter <- function(w,h,a,b){(a * ((w + h)^b))} fillfactor <- lapply(seq_len(length(dat.a)), function(i) { as.factor((gsub("-.*","",dat.a[[i]]$Date)))}) #I tried as.integer as well set.seed(92) lin.a <- lapply(seq_len(length(dat.a)), function(i) { data.frame(x = runif(100, -dB.coef.a[3,i], max(dat.a[[i]]$WL)+diff(0.2*range(dat.a[[i]]$Q))))}) library(ggplot2) ga <- lapply(seq_len(length(dat.a)), function(i) { ggplot() + geom_point(data=dat.a[[i]], aes(x=WL,y=Q, tltip = Date, fill =fillfactor[[i]]), colour = NA, pch=21) + scale_fill_manual(breaks = mybreaks, values = myfills)+ geom_line(data = lin.a[[i]], aes(x=x,y= RCfitter(x,dB.coef.a[3,i],dB.coef.a[1,i],dB.coef.a[2,i])),colour="red")+ xlab("WL") + ylab("Q") + ggtitle(paste("pLot ",i)) + ylim(c(0,(max(dat.a[[i]]$Q)+diff(0.2*range(dat.a[[i]]$Q))))) + xlim(c(0,(max(dat.a[[i]]$WL)+diff(0.2*range(dat.a[[i]]$WL))))) + theme(legend.position="none") }) 

If I ignore scale_fill , I can build them and I get a “colorful” plot. But with this, I get this warning:

 ga[[2]] ## Warning messages: ## 1: Removed 6 rows containing missing values (geom_point). ## 2: Removed 64 rows containing missing values (geom_path). 

This means that geom_point is not drawing anything.

I used scale_fill_discrete , scale_fill_continuous etc. and they give me errors like discrete value to continuous scale or vice versa.

I really want to make the gradient color over the years , say, from blue to red, so I can distinguish between years, seeing if there is a piece in similar years (for example, the 60s) in the same place.

PS In the end, I use ggplotly() (for example, ggplotly(ga[[2]],tooltip = c("x","y","tltip") ). So, if that changes the scale_fill behavior (for example, some of colors that I indicated are not valid for plotly ), please keep this in mind.

Sample data:

dat.a

  dat.a <- list(structure(list(Date = c("1974-02-14", "1974-02-16", "1974-02-28", "1974-02-28", "1974-02-28", "1974-02-28"), WL = c(0.24, 0.135, 0.395, 0.26, 0.22, 0.31), Q = c(0.237, 0.04, 0.9, 0.36, 0.52, 0.56), Velocity = c(0.3, 0.103, 0.367, 0.209, 0.34, 0.276), Area = c(0.79, 0.388, 2.452, 1.722, 1.529, 2.029), Flag = c(NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_), Shift = c("/", "/", "/", "/", "/", "/"), date = structure(c(130032000, 130204800, 131241600, 131241600, 131241600, 131241600), class = c("POSIXct", "POSIXt"), tzone = "UTC")), .Names = c("Date", "WL", "Q", "Velocity", "Area", "Flag", "Shift", "date"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")), structure(list( Date = c("1965-01-29", "1965-01-29", "1965-04-25", "1966-11-29", "1967-01-24", "1967-11-12"), WL = c(0.439, 0.439, 0.482, 0.463, 0.427, 0.475), Q = c(0.252, 0.269, 0.403, 0.314, 0.199, 0.4), Velocity = c(0.23, 0.232, 0.316, 0.279, 0.249, 0.36 ), Area = c(1.096, 1.159, 1.275, 1.125, 0.799, 1.111), Flag = c(NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_), Shift = c("/", "/", "/", "/", "/", "/"), date = structure(c(-155347200, -155347200, -147916800, -97545600, -92707200, -67478400), class = c("POSIXct", "POSIXt"), tzone = "UTC")), .Names = c("Date","WL", "Q", "Velocity", "Area", "Flag", "Shift", "date"), row.names = c(NA,-6L), class = c("tbl_df", "tbl", "data.frame"))) 

dB.coef.a

  dB.coef.a <- structure(c(-77.6915945552795, 0.594614568300253, 60.9718752625543, 7.96297849987566, 2.69599957356069, -0.183937755444007), .Dim = c(3L, 2L), .Dimnames = list(c("a", "b", "h"), NULL)) 

mybreaks

  mybreaks <- c(1955, 1956, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017) 

myfills

  myfills <- c("005ABF", "0358BC", "0757B9", "0A56B6", "0E54B3", "1153B0", "1552AD", "1851AA", "1C4FA7", "1F4EA4", "234DA2", "264B9F", "2A4A9C", "2D4999", "314896", "344693", "384590", "3B448D", "3F428A", "424187", "464085", "493F82", "4D3D7F", "503C7C", "543B79", "573976", "5B3873", "5E3770", "62366D", "65346A", "693368", "6D3265", "703062", "742F5F", "772E5C", "7B2D59", "7E2B56", "822A53", "852950", "89274D", "8C264B", "902548", "932445", "972242", "9A213F", "9E203C", "A11E39", "A51D36", "A81C33", "AC1B30", "AF192E", "B3182B", "B61728", "BA1525", "BD1422", "C1131F", "C4121C", "C81019", "CB0F16", "CF0E13", "D30D11") 
+4
source share
1 answer

Suggestion, if I understood your problem correctly:

1. I have an error message with colors ( myfills ):

Error in grDevices :: col2rgb (color, TRUE)

# missing:

 mycolors <- paste0("#", myfills) # correct colors names(mycolors ) <- mybreaks # name your colors mycolors[names(mycolors) == "1965"] <- "#D30D11" # swapped color to red to check its ok 


2. If you really do not want a “color” with pch = 21 for geom_point , better use pch = 16 , then forget to fill aesthetics and use color instead:

 ga <- lapply(seq_len(length(dat.a)), function(i) { ggplot(data = dat.a[[i]], aes(x = WL, y = Q, color = fillfactor[[i]])) + geom_point(pch = 16) + scale_color_manual(values = mycolors) + geom_line(data = lin.a[[i]], aes(x = x, y = RCfitter(x, dB.coef.a[3,i], dB.coef.a[1,i], dB.coef.a[2,i])), colour = "red") + ggtitle(paste("pLot ",i)) + ylim(c(0,(max(dat.a[[i]]$Q) + diff(0.2*range(dat.a[[i]]$Q))))) + xlim(c(0,(max(dat.a[[i]]$WL) + diff(0.2*range(dat.a[[i]]$WL))))) + theme(legend.position="none") } ) ga[[2]] 

enter image description here

3. The warning message about geom_path is associated with your xlim() .

+1
source

Source: https://habr.com/ru/post/1273904/


All Articles