ggplot没有为图表类别设置正确的颜色 [英] ggplot does not set correct colors to chart category

查看:74
本文介绍了ggplot没有为图表类别设置正确的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to R and any help is greatly appreciated. I have the following (sample only) data of robot moves between spots (output of dt2). The x axis is a timestamp of each move and the Y axis is the spot index it moved to. The problem is I cannot seem to assign fixed colors to each robot and limit the plot to only the robots in the data (3 in this case). 1 to 5 robots (G to K) may be represented in a dataset. I want one script to handle all datasets, no matter how many robots are recorded.

       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

我正在使用的代码如下:

The code I am using is as follows:

   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个机器人有数据.

Currently the chart looks like the following. There are 5 plots but only 3 robots have data.

推荐答案

您需要使用 scale_color _ * 函数设置颜色. ggplot 使用 line_color 作为确定颜色组的因素,但是它并不关心颜色组是否以颜色命名:它对待它们的方式就像对待颜色一样它们是 dog cat fish .

You need to set the colors with a scale_color_* function. ggplot is using line_color as a factor to determine the color groups, but it doesn't care that the color groups are named after colors: it treats them the same way as if they were dog, cat, and fish.

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()

您需要指定将什么颜色传递给 aes color = 参数的变量的值.您可以使用 scale_color _ * 函数执行此操作.由于 col 是分类的,因此将命名向量传递给 scale_color_manual values = 参数是最简单的:

You need to specify what colors go with what values of the variable you passed to the color= argument of aes. You use the scale_color_* functions to do this. Since col is categorical, passing a named vector to the values= argument of scale_color_manual is the easiest:

ggplot(df, aes(x, y, color = col)) +
    geom_line() +
    scale_color_manual(values = c('red' = 'red', 'green' = 'green', 'blue' = 'blue'))

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

In your case, since the levels are already colors, you can do a trick using setNames to use those values in one line:

ggplot(df, aes(x, y, color = col)) +
    geom_line() +
    scale_color_manual(values = setNames(unique(df$col), unique(df$col)))

values = 需要一个命名向量:值是颜色,名称是传递给 aes 的颜色的变量值. setNames(unique(df $ col),unique(df $ col))只是对 df $ col 的所有唯一值进行命名,并使用相同的名称和值:,则每种颜色"都会在其行上应用该颜色.

values= expects a named vector: The values are the colors and the names are the values of the variable passed to aes to assign that color to. setNames(unique(df$col), unique(df$col)) just makes a named vector of all unique values of df$col with identical names and values: therefore, each 'color' will have that color applied to its line.

这篇关于ggplot没有为图表类别设置正确的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆