Reputation: 627
I have got a df
with over 400 datapoints and want to colour those accourding to the RColorBrewer
package with the palette = "Blues"
.
I plotted my data and even expanded the color input maximum of the palette
to the length of my data points to avoid getting "Error messages" (see below), but the colors in the plot aren't changing (only a black line).
Error: Aesthetics must be either length 1 or the same as the data (427): fill
I've created a dummy df
to make my problem reproducible:
library(ggplot2)
library(RColorBrewer)
color = colorRampPalette(rev(brewer.pal(n = 9, name = "Blues")))(300)
df = (curve(3*x^2 + x, from=1, to=10, n=300, xlab="xvalue", ylab="yvalue",
col="blue", lwd=2, main="Plot of (3x^2 + x)"))
dfx = matrix(data = df$x, ncol = 1)
dfy = matrix(data = df$y, ncol = 1)
dfa = cbind(dfx,dfy)
DF = ggplot(dfa, aes(x = dfx, y = dfy)) +
geom_point(fill = color)
I expect the curve to change into a light blue at the start (1,4) with an increase in darkness till the end (dark blue at the end (10,310)).
Thanks in advance!
Upvotes: 0
Views: 410
Reputation: 1001
exDF <- data.frame(dataX = seq(1, 10, .1),
dataY = sapply(seq(1, 10, .1), function(x) 3*x^2 + x))
exDFcolors <- colorRampPalette(brewer.pal(9, "Blues"))(nrow(exDF))
ggplot(exDF, aes(dataX, dataY)) +
geom_line(size = 2, color = exDFcolors)
Upvotes: 1
Reputation: 669
color_seq = colorRampPalette(brewer.pal(n = 9, name = "Blues"))(300)
df = (curve(3*x^2 + x, from=1, to=10, n=300, xlab="xvalue", ylab="yvalue",
col="blue", lwd=2, main="Plot of (3x^2 + x)"))
dfx = matrix(data = df$x, ncol = 1)
dfy = matrix(data = df$y, ncol = 1)
dfa = as.data.frame(cbind(dfx , dfy, color_seq), stringsAsFactors = FALSE)
dfa$V1 <- as.numeric(dfa$V1) ## convert both to numeric so the scale is continous
dfa$V2 <- as.numeric(dfa$V2)
ggplot(dfa, aes(x = V1, y = V2, color = color_seq)) +
geom_point() +
scale_color_identity()
Upvotes: 2