How can I change the color of each line in the geom_line ()
function?
The color of the lines of the geom_line ()
must match the color of the points of the geom_point ()
function
I tried to add the color manually in geom_line ()
, but the legend will not appear on the chart.
The color reference is the one below.
geom_line ()
GPHY_G_K_PCT = "#FF0400"
GPHY_G_TH_PPM = "#008000"
GPHY_G_U_PPM = "#0300FF"
My Dataset
dataset = structure(list(GPHY_G_K_PCT = c(1.9, 1.9, 2, 2, 2, 2, 2.1, 2.1
), GPHY_G_TH_PPM = c(21.4, 23.2, 23.6, 23, 23, 23.6, 23, 22.9
), GPHY_G_U_PPM = c(5.2, 5, 5.1, 4.6, 5.2, 5.7, 5.7, 5.1), GPHY_G_DATE = structure(c(2L,
2L, 1L, 2L, 2L, 2L, 1L, 2L), .Label = c("2015-08-14T00:00:00.0000000",
"2015-08-17T00:00:00.0000000"), class = "factor"), GPHY_G_TIME = structure(c(7L,
8L, 2L, 3L, 5L, 1L, 6L, 4L), .Label = c("1899-12-30T09:18:56.0000000",
"1899-12-30T09:31:13.0000000", "1899-12-30T10:54:01.0000000",
"1899-12-30T14:00:26.0000000", "1899-12-30T15:13:40.0000000",
"1899-12-30T15:31:26.0000000", "1899-12-30T16:27:14.0000000",
"1899-12-30T16:43:37.0000000"), class = "factor"), DATE_TIME = structure(c(7L,
8L, 1L, 4L, 6L, 3L, 2L, 5L), .Label = c("14/08/2015 09:31:13",
"14/08/2015 15:31:26", "17/08/2015 09:18:56", "17/08/2015 10:54:01",
"17/08/2015 14:00:26", "17/08/2015 15:13:40", "17/08/2015 16:27:14",
"17/08/2015 16:43:37"), class = "factor"), TITULO = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Gama - Standard High Value (HH)", class = "factor"),
color_K_PCT = c("K PCT", "K PCT", "K PCT", "K PCT", "K PCT",
"K PCT", "K PCT", "K PCT"), color_TH_PPM = c("U PPM", "U PPM",
"U PPM", "U PPM", "U PPM", "U PPM", "U PPM", "U PPM"), color_G_U_PPM = c("TH PPM",
"TH PPM", "TH PPM", "TH PPM", "TH PPM", "TH PPM", "TH PPM",
"TH PPM")), row.names = c(NA, -8L), class = "data.frame")
My Script
library("ggplot2")
library("tibble")
dataset$color_K_PCT = "K PCT"
dataset$color_TH_PPM = "U PPM"
dataset$color_G_U_PPM = "TH PPM"
zone_data_U <- tibble(ymin = min(dataset$GPHY_G_U_PPM)*0.9, ymax = max(dataset$GPHY_G_U_PPM)*1.1, xmin = -Inf, xmax = Inf)
zone_data_TH <- tibble(ymin = min(dataset$GPHY_G_TH_PPM)*0.9, ymax = max(dataset$GPHY_G_TH_PPM)*1.1, xmin = -Inf, xmax = Inf)
zone_data_K <- tibble(ymin = min(dataset$GPHY_G_K_PCT)*0.9, ymax = max(dataset$GPHY_G_K_PCT)*1.1, xmin = -Inf, xmax = Inf)
p = ggplot(dataset, aes(x = DATE_TIME)) +
geom_line(aes(y = GPHY_G_K_PCT, color="K PCT"), size=1, linetype=1, group = 1) +
geom_line(aes(y = GPHY_G_TH_PPM, color="U PPM"), size=1, linetype=1, group = 2) +
geom_line(aes(y = GPHY_G_U_PPM, color="TH PPM"), size=1, linetype=1, group = 3) +
geom_point(aes(y = GPHY_G_K_PCT), color="#FF0400", size=2, group = 1) +
geom_point(aes(y = GPHY_G_TH_PPM), color="#008000", size=2, group = 2) +
geom_point(aes(y = GPHY_G_U_PPM), color="#0300FF", size=2, group = 3) +
scale_y_continuous(trans='log2', labels = scales::comma) +
theme_bw() +
theme(legend.position = "bottom",
panel.background = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(),
axis.text.x = element_text(angle = 65, vjust = 1, hjust = 1),
plot.title = element_text(size=12, face='bold', hjust = 0.5)) +
labs(y = "K (%) U (ppm) TH (ppm) - Log Scale", x = "", color = "", title = unique(dataset$TITULO)) +
geom_rect(mapping = aes(ymin = ymin, ymax = ymax, xmin = xmin, xmax = xmax),
data = zone_data_K, alpha = 0.2, fill = "#FF0400",inherit.aes = FALSE)+
geom_rect(mapping = aes(ymin = ymin, ymax = ymax, xmin = xmin, xmax = xmax),
data = zone_data_TH, alpha = 0.2, fill = "#008000",inherit.aes = FALSE) +
geom_rect(mapping = aes(ymin = ymin, ymax = ymax, xmin = xmin, xmax = xmax),
data = zone_data_U, alpha = 0.2, fill = "#0300FF",inherit.aes = FALSE)
p