简体   繁体   English

R scale_color_discrete() 问题

[英]R problems with scale_color_discrete()

I am writing a script that is supposed to be able to read in a large dataset and plot the results.我正在编写一个应该能够读取大型数据集和 plot 结果的脚本。

Script:脚本:

paths <- list.files(path = path, pattern="\\.csv$", full.names = TRUE)

dataset <- data.frame()

for (x in paths) {  
   t <- fread(file)  
   dataset <- rbind(dataset, t)  
}  

mapping <- aes(
  x = column_c 
  , color = column_a
  , linetype = column_b
)

plot <- (ggplot(data=dataset, mapping=mapping)
         + stat_ecdf()
         + scale_color_discrete(name = "COL_A", breaks = c(1, 2, 3))
         + scale_linetype_discrete(name = "COL_B", breaks = c("aaa", "bbb", "ccc"))        
)

The dataset has three columns: column_a , column_b , column_c In column_a possible values are: 1, 2, 3 (type: integer)数据集有三列: column_acolumn_bcolumn_ccolumn_a中可能的值为:1、2、3(类型:整数)
In column_b possible values are: aaa , bbb , cccc (type: character)column_b中可能的值为: aaabbbcccc (类型:字符)
In column_c different values in range (0,1000) (type:integer)column_c范围内的不同值(0,1000)(类型:整数)

Problem: To organize the plot, I use in aes the color parameter to be based on column_a , and the line parameter to be based on column_b .问题:为了组织 plot,我在aes中使用基于column_a的颜色参数,以及基于column_b的 line 参数。 The plotted graph seems to accept the line type command but it ignores the color = column_a .绘制的图形似乎接受 line type 命令,但它忽略了color = column_a It doesn't show any error message, it just seems it doesn't show any different color lines.它没有显示任何错误消息,只是似乎没有显示任何不同的颜色线。 They are all the same color.它们都是相同的颜色。
This leads me to the conclusion that it either doesn't recognize the breaks.这使我得出结论,它要么不识别中断。
To check this out I preformed the following command:为了检查这一点,我执行了以下命令:

unique(dataset$column_a)唯一的(数据集$column_a)

and I got as return我得到了回报

[1] 2 3 1 [1] 2 3 1
So, I would say that the dataset does contain the breaks that I have set to be expected in line所以,我会说数据集确实包含我设置的预期的中断

 + scale_color_discrete(name = "COL_A", breaks = c(1, 2, 3))

Does someone notice what I am doing wrong?有人注意到我做错了吗?

UPDATE: class(dataset) [1] "tbl_df" "tbl" "data.frame"更新:类(数据集)[1]“tbl_df”“tbl”“data.frame”

To have a discrete scale, the column needs to be a factor or character class.要具有离散比例,列需要是factorcharacter class。 Numeric (including integer) will have a discrete scale.数字(包括整数)将具有离散比例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM