简体   繁体   中英

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.

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)
In column_b possible values are: aaa , bbb , cccc (type: character)
In column_c different values in range (0,1000) (type:integer)

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 . The plotted graph seems to accept the line type command but it ignores the 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)

and I got as return

[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"

To have a discrete scale, the column needs to be a factor or character class. Numeric (including integer) will have a discrete scale.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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