library(ggplot2)
library(dplyr)
library(stringr)
##生成测试数据集
dt <- as.data.frame(cbind(x=c(1:17),y=paste("col",1:17,sep = "")))
##生成填充颜色数据集
plt <-cbind(c(rep("A",8),c(rep("B",9)))) %>% as.data.frame()
colnames(plt) <- "plt"
##下面的aa是我特意这么做成这结构(实际用于其他地方),这里是把代码扣出来
aa <- "A='yellow',B='yellow',C='yellow'"
if(isTRUE(str_detect(aa,"="))){
hh <- strsplit(aa,split=",")[[1]] %>% as.factor()
}else{
hh <- c(1:300)
}
###开始绘图
ggplot(data = dt,aes(factor(y),factor(y),color=plt$plt,shape=plt$plt))+geom_point()+
scale_color_manual(values = hh)###这里的hh没有起到作用
问题:hh里指定的颜色没有起到作用,
我知道若把aa转换为: mycol <- c(A='red',B='red')这种结构就能被
ggplot2识别,但我尝试了用as.factor、as.vector函数转换,无果。
软件信息:R4.0, ggplot2 version 3.3.0
希望得到大佬的帮忙,谢谢。