ggplot没有为图表类别设置正确的颜色

时间:2018-09-18 21:25:59

标签: r ggplot2 colors

我是R的新手,非常感谢您的帮助。我有以下(仅样本)机器人在点之间移动的数据(dt2的输出)。 x轴是每次移动的时间戳,Y轴是其移动到的位置索引。问题是我似乎无法为每个机器人分配固定的颜色,而只能将绘图限制为数据中的机器人(在这种情况下为3个)。 1至5个机器人(G至K)可以在数据集中表示。我希望一个脚本可以处理所有数据集,无论记录了多少个机器人。

       moveID robot    stepStartTime       spotIndex    chartCategory    line_color
1         1     G    2018-05-31 23:13:00         2          Robot G NA       white
2         2     G    2018-05-31 23:13:00         4          Robot G NA       white
3         3     G    2018-05-31 23:13:00         8          Robot G NA       white
....
29       29     G    2018-05-31 23:17:10        26   Robot G Efficient      green4
30       30     G    2018-05-31 23:20:10        26   Robot G Efficient      green4
31       31     G    2018-05-31 23:21:10        26   Robot G Efficient      green4
32       32     G    2018-05-31 23:23:10        26   Robot G Efficient      green4
....
115     115     G    2018-06-01 02:23:10        30   Robot G Efficient      green4
116     116     G    2018-06-01 02:25:10        18 Robot G Inefficient         red
117     117     G    2018-06-01 02:26:10        18   Robot G Efficient      green4
118     118     G    2018-06-01 02:27:10        18   Robot G Efficient      green4
119     119     G    2018-06-01 02:29:10        14   Robot G Efficient      green4
....
164     164     H    2018-05-31 23:12:00         2          Robot H NA       white
165     165     H    2018-05-31 23:12:00         4          Robot H NA       white
166     166     H    2018-05-31 23:12:00         8          Robot H NA       white
....
193     193     H    2018-05-31 23:12:00         6   Robot H Efficient    dodgerblue1
194     194     H    2018-05-31 23:14:12         6   Robot H Efficient    dodgerblue1
195     195     H    2018-05-31 23:21:12        10   Robot H Efficient    dodgerblue1

我正在使用的代码如下:

   dataset = read.csv("C:/Users/User/R Chart/Data_1.csv", header = TRUE, sep = ",", quote = "", dec = ".", fill = TRUE, comment.char = "")

library("ggplot2")
library("dplyr")

options(max.print = 2000)

dataset$stepStartTime <- as.POSIXct(dataset$stepStartTime, format="%Y/%m/%dT%H:%M:%S")

dt2 <- dataset %>%
  mutate(line_color = case_when(
    chartCategory == "Robot G NA" ~ "white",
    chartCategory == "Robot H NA" ~ "white",
    chartCategory == "Robot I NA" ~ "white",
    chartCategory == "Robot J NA" ~ "white",
    chartCategory == "Robot K NA" ~ "white",
    chartCategory == "Robot G Inefficient" ~ "red",
    chartCategory == "Robot H Inefficient" ~ "red",
    chartCategory == "Robot I Inefficient" ~ "red",
    chartCategory == "Robot J Inefficient" ~ "red",
    chartCategory == "Robot K Inefficient" ~ "red",
    chartCategory == "Robot G Efficient" ~ "green4", # green #229954  (ends bay 30)
    chartCategory == "Robot H Efficient" ~ "dodgerblue1",  #blue #5dade2   (ends bay 18)
    chartCategory == "Robot I Efficient" ~ "grey62", # violet #9b59b6  (ends bay 38)
    chartCategory == "Robot J Efficient" ~ "chocolate2",  # red  #EC7063   (ends bay 14)
    chartCategory == "Robot K Efficient" ~ "orange1",   # orange  #f5b041  (ends bay 22)

  ))
dt2
p = ggplot(data=dt2, aes(stepStartTime, spotIndex, group=robot, color=line_color))+
  geom_step(size = 1)+ 
  geom_point( size = 6, shape = 'I') +
  theme(
    legend.position='none',
    axis.line = element_line(colour = "#000000", size = 0.3, linetype = "solid"),
    axis.ticks.x =  element_line(size = 1), 
    axis.ticks.y =  element_line(size = 1), 
    axis.text.x = element_text(face="plain", color="#808080", size=9, angle=0), 
    axis.text.y = element_text(face="plain", color="#808080", size=9, angle=0), 
    axis.title.x = element_blank(), 
    axis.title.y = element_blank(),
    panel.border = element_rect(linetype = "solid", colour = "#808080", size = 1, fill = NA),
    panel.background = element_rect(fill = "transparent", colour = NA),
    plot.background = element_rect(fill = "transparent", colour = NA)
  )
p +
  scale_y_reverse(breaks=c(66,64,62,60,58,56,54,52,50,48,46,44,42,40,38,36,34,32,30, 28,26,24,22,20,18,16,14,12,10,8,6,4,2),lim=c(66,2)) 

当前图表如下所示。有5个地块,但只有3个机器人有数据。 enter image description here

2 个答案:

答案 0 :(得分:1)

您需要使用scale_color_*功能设置颜色。 ggplot使用line_color作为确定颜色组的因素,但并不关心颜色组是否以颜色命名:它像对待{{1}一样对待它们},dogcat

fish

enter image description here

您需要指定将什么颜色传递给df <- data.frame(x = c(1,2,3,1,2,3,1,2,3), y = c(1,2,3,2,3,4,4,2,3), col = c('red', 'red', 'red', 'green', 'green', 'green', 'blue', 'blue', 'blue')) ggplot(df, aes(x, y, color = col)) + geom_line() 的{​​{1}}参数的变量的值。您可以使用color=函数来执行此操作。由于aes是分类的,因此将命名向量传递到scale_color_*的{​​{1}}参数是最简单的:

col

enter image description here

在您的情况下,由于级别已经是颜色,因此可以使用values=来在一行中使用这些值:

scale_color_manual

enter image description here

ggplot(df, aes(x, y, color = col)) + geom_line() + scale_color_manual(values = c('red' = 'red', 'green' = 'green', 'blue' = 'blue')) 需要一个命名向量:值是颜色,名称是传递给setNames的变量的值,以将该颜色分配给该变量。 ggplot(df, aes(x, y, color = col)) + geom_line() + scale_color_manual(values = setNames(unique(df$col), unique(df$col))) 只是为values=的所有唯一值创建一个命名向量,并具有相同的名称和值:因此,每种“颜色”都将该颜色应用于其行。

答案 1 :(得分:0)

这是我的最终解决方案。下一个任务-添加网格线并弄清楚如何在x轴上获得30分钟的日期间隔。

rm(list = ls())  # Clear out the environment/workspace
setwd("C:/Users/user/R_Chart/robot_moves/Chart_Final")

dataset = read.csv("C:/Users/User/R_Chart/Data_1.csv", header = TRUE, sep = ",", quote = "", dec = ".", fill = TRUE, comment.char = "")
dataset
library("ggplot2")
library("dplyr")
library("scales")

#options(max.print = 1000)  

dataset$stepStartTime <- as.POSIXct(dataset$stepStartTime, format="%Y-%m-%dT%H:%M:%S") # source data date format

cols = c("robot G NA"="grey90",
        "robot H NA"="grey90",
        "robot I NA"="grey90",
        "robot J NA"="grey90",
        "robot K NA"="grey90",
        "robot G Inefficient"="red",
        "robot H Inefficient"="red",
        "robot I Inefficient"="red",
        "robot J Inefficient"="red",
        "robot K Inefficient"="red",
        "robot G Efficient"="green4",
        "robot H Efficient"="dodgerblue1",
        "robot I Efficient"="darksalmon",
        "robot J Efficient"="thistle4",
        "robot K Efficient"="burlywood2")


p = ggplot(data=dataset, aes(stepStartTime, spotIndex, group=robot, color=chartCategory))+
  geom_step(direction = 'hv',size = 1)+ 
  geom_point( size = 6, shape = 'I') +
  scale_color_manual(values = cols)+

  scale_x_datetime(breaks = date_breaks ("1 hour"), labels = date_format("%d-%m\n%H:%M", tz="")) +

  theme(
    legend.position='none',
    axis.line = element_line(colour = "#000000", size = 0.3, linetype = "solid"),
    axis.ticks.x =  element_line(size = 1), 
    axis.ticks.y =  element_line(size = 1), 
    axis.text.x = element_text(face="plain", color="gray15", size=7, angle=0), 
    axis.text.y = element_text(face="plain", color="gray15", size=7, angle=0), 
    axis.title.x = element_blank(), 
    axis.title.y = element_blank(),
    panel.border = element_rect(linetype = "solid", colour = "#808080", size = 1, fill = NA),
    panel.background = element_rect(fill = "transparent", colour = NA),
    plot.background = element_rect(fill = "transparent", colour = NA)
  )
p +
  scale_y_reverse(breaks=c(66,64,62,60,58,56,54,52,50,48,46,44,42,40,38,36,34,32,30, 28,26,24,22,20,18,16,14,12,10,8,6,4,2),lim=c(68,2)) 

#ggsave("V4.png", width = 6, height = 6)

The plot