0
我有数据,我想用ggplot
用渐变着色方案,然后注释一些要点。将geom_text_repel图层添加到由scale_colour_gradient2着色的geom_point
我的数据:
df <- data.frame(id = rep(LETTERS,100),
val1 = rnorm(100*length(LETTERS)), val2 = rnorm(100*length(LETTERS)),
sig = runif(100*length(LETTERS),0,1),
col = NA,stringsAsFactors = F)
在这里,我选择了几个点,我想注释,给他们颜色:
df$col[sample(nrow(df), 10, replace = F)] <- rainbow(10)
而这里的ggplot
代码我想:
library(ggplot2)
library(ggrepel)
ggplot(df,aes(x=val1,y=val2,color=col))+
geom_point(aes(color=sig),cex=2)+scale_colour_gradient2("Significance",low="darkred",mid="darkblue",high="darkred")+
geom_text_repel(data=dplyr::filter(df,!is.na(col)),aes(x=dplyr::filter(df,!is.na(col))$val1,y=dplyr::filter(df,!is.na(col))$val2,label=dplyr::filter(df,!is.na(col))$id,colour=dplyr::filter(df,!is.na(col))$col))+
theme_minimal()+theme(legend.position="none")
其中引发此错误:
Error: Discrete value supplied to continuous scale
任何想法?
非常感谢@missuse。你认为离散色彩方案是唯一的解决方案吗? – dan
@dan,增加了另一种解决方案。检查了它 – missuse