'How to add gradient to a geom_line while using multiple colour scales and date_breaks in ggplot
I want to create a ggplot that includes a gradient colour fill for a geom_line
, but also includes other things like manual colour and shape overwrites, and (discrete?) date breaks.
If I only plot a geom_line
with a scale_colour_gradient
everything works fine:
set.seed(1)
# dataframe
df <- data.frame (UP = c(5, 5, 3, 2),
Date = c(sort(sample(seq(as.Date("2018-06-01"), as.Date("2019-06-01"), by = "day"), 4))))
# dataframe for gradient
df2 <- data.frame (UP = seq(from = df[2, 1], to = df[3, 1], length.out = 100),
Date = seq.Date(from = df[2, 2], to = df[3, 2], length.out = 100))
ggplot() +
geom_line (data = df[1:2, ], aes (Date, UP), lty = "longdash", col = "blue", size = 1) +
geom_line (data = df[3:4, ], aes (Date, UP), lty = 1, col = "red", size = 1) +
geom_point (data = df[1:2, ], aes (Date, UP), col = "blue", shape = 4, stroke = 2, size = 3) +
geom_point (data = df[3:4, ], aes (Date, UP), col = "red", size = 3) +
geom_line (data = df2, aes (Date, UP, color = as.integer(UP)), size = 1) +
scale_colour_gradient(name = "UP",
low = "red", high = "blue")
From here, I want to add a sequences of statistics that have certain colours and shapes, and also break the x-axis by years using the scales package.
set.seed(1)
# dataframe for statistics
df3 <- data.frame (Stat = c(sample(2:4, 4, replace = TRUE)),
Date = df$Date,
Shape = c("DO", "DO", "RE", "MI"))
# plot including statistics tags
p <- ggplot() +
geom_line (data = df[1:2, ], aes (Date, UP), lty = "longdash", col = "blue", size = 1) +
geom_line (data = df[3:4, ], aes (Date, UP), lty = 1, col = "red", size = 1) +
geom_point (data = df[1:2, ], aes (Date, UP), col = "blue", shape = 4, stroke = 2, size = 3) +
geom_point (data = df[3:4, ], aes (Date, UP), col = "red", size = 3) +
geom_line (data = df3, aes (Date, Stat), lty = 2, size = 1) +
geom_point (data = df3[1:2, ], aes (Date, Stat, col = Shape, shape = Shape), size = 7) +
geom_point (data = df3[3, ], aes (Date, Stat, col = Shape, shape = Shape), size = 7) +
geom_point (data = df3[4, ], aes (Date, Stat, col = Shape, shape = Shape), size = 7) +
scale_colour_manual(name=bquote(bold ("TITLE")),
values= c("red3", "black", "springgreen4"),
labels = c("DO","RE", "MI")) +
scale_shape_manual(name=bquote(bold ("TITLE")),
values= c(15:17),
labels = c("DO","RE", "MI")) +
scale_x_date(date_breaks = "year",
labels = scales::date_format ("%Y"),
limits = as.Date(c(df[1,2], df[4,2])))
The problems start when I try to add the gradient geom_line
to this latter plot, resulting in the following errors:
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale. Error: Discrete value supplied to continuous scale
p + geom_line (data = df2, aes (Date, UP, color = as.integer(UP)), size = 1) +
scale_colour_gradient(name = "UP",
low = "red", high = "blue")
Do you know how to tackle this problem? Any help is very much appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|