【问题标题】:How to change the color of geom_line ()?如何改变geom_line()的颜色?
【发布时间】:2021-04-14 19:31:23
【问题描述】:

如何更改geom_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

【问题讨论】:

标签: r ggplot2


【解决方案1】:

使用 pivot_longer 转换数据,以便您可以使用用于分组和着色的另一列对每个系列进行分类:


d <- dataset %>% pivot_longer(
                     cols=c("GPHY_G_K_PCT", "GPHY_G_TH_PPM", "GPHY_G_U_PPM"),
                     names_to = "Set", values_to = "Measurement"
                 )

p  <-  ggplot(d, aes(x = DATE_TIME, y=Measurement, color=Set, group=Set)) +
    geom_line() +
    geom_point() +
    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)   +
    geom_blank()
p

作为奖励,您可以减少所有这些点和线调用,而让 ggplot 处理这些调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    相关资源
    最近更新 更多