R ggplot绘图报colours encodes as numbers must be positive错误求助
ggplot2报错
Error: colours encodes as numbers must be positive的诱因与解决方案 核心诱因
该报错由ggplot2语法使用错误导致:
- 映射数据框变量到图形属性(x轴、y轴、颜色、大小等)时,必须将映射逻辑放在
aes()函数内部,你当前的代码直接在aes()外指定col=all$V1.y,R会将V1.y列的数值直接识别为原生颜色编码值。 - R的数值型颜色编码要求必须为正整数,对应内置调色板的位置,当你的大规模数据中V1.y列存在负数、零或非正整数时,就会触发该报错。
修正后的代码
## Load packages library(tidyverse) require(data.table) ## Read in data frames m1<-fread("m1.csv", header = F) m2<-fread("m2.csv", header = F) L<-fread("l.csv", header = F) LP<-fread("LP.csv", header = F) ## Get rate by taking m1 from m2 rate<-m1[1,]-m2[1,] ### subtract p1 rate from p2 ## Transpose the data frame t_rate <- transpose(rate) ## Create row ID's to merge data frames L$row_num <- seq.int(nrow(L)) t_rate$row_num <- seq.int(nrow(t_rate)) all<-merge(L, t_rate, by = "row_num") ## merge the dataframes based on their ID ## Get rid of ID now we don't need it all$row_num=NULL ## 修正后的绘图代码 ggplot(all, aes(x = V1.x, y = V2, col = V1.y)) + geom_point(size = 0.1) + geom_point(data = LP, aes(x = V1, y = V2), size = 1)
补充说明:如果需要为第二组LP点设置固定颜色,可在对应
geom_point的aes()外添加颜色参数,例如设置为黑色可写为geom_point(data = LP, aes(x = V1, y = V2), size = 1, col = "black")
内容的提问来源于stack exchange,提问作者Sophie Williams
相关产品推荐
相关产品推荐

