R 如何更改几何图形线()的颜色?

R 如何更改几何图形线()的颜色?,r,ggplot2,R,Ggplot2,如何更改geom\u line()函数中每一行的颜色 geom_line()的线条颜色必须与geom_point()函数的点颜色匹配 我试图在geom_line()中手动添加颜色,但图例不会出现在图表上 颜色参考如下所示 geom_line () GPHY_G_K_PCT = "#FF0400" GPHY_G_TH_PPM = "#008000" GPHY_G_U_PPM = "#0300FF" 我的数据集 dataset = str

如何更改
geom\u line()
函数中每一行的颜色

geom_line()
的线条颜色必须与
geom_point()
函数的点颜色匹配

我试图在
geom_line()
中手动添加颜色,但图例不会出现在图表上

颜色参考如下所示

geom_line ()
GPHY_G_K_PCT = "#FF0400"
GPHY_G_TH_PPM = "#008000"
GPHY_G_U_PPM = "#0300FF"
我的数据集

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")
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
我的脚本

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")
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
库(“ggplot2”)
图书馆(“tibble”)
数据集$color\u K\u PCT=“K PCT”
数据集$color\U TH\U PPM=“U PPM”
数据集$color\U G\U PPM=“TH PPM”

zone_data_使用pivot_对数据进行更长时间的变换,以便您可以使用另一列对每个系列进行分类,该列用于分组和着色:


d%u更长(
cols=c(“GPHY_G_K_PCT”、“GPHY_G_TH_PPM”、“GPHY_G_PPM”),
名称到=“设置”,值到=“测量”
)

p你没有在geom_线中设置任何颜色。有任何匹配,都是巧合。您需要使用scale\u color\u手册,或将颜色传递到aes调用之外。请阅读此线程和更基本的ggplot教程,例如,烹饪书也应该很有用