我想根据值绘制不同颜色的图形。我写下面的代码,
np_graph <- data.frame(C1 = -5:5, C2 = -5:5)
x=np_graph2$C1
y=np_graph2$C2
plot(x,y,xlab="PC1",ylab="PC2")
现在,如果X的值为> 0,则该值应为绿色(在图中)。如果Y的值为> 0,则该值应为红色(在图中)。
有人可以帮我吗
参数col将设置颜色,您可以将其与ifelse语句一起使用。有关详细信息,请参阅图。
# using base plot
plot(x,y,xlab="PC1",ylab="PC2", col = ifelse(x < 0,'red','green'), pch = 19 )
在ggplot2中做同样的事情。
#using ggplot2
library(ggplot2)
ggplot(np_graph) + geom_point(aes(x = C1, y = C2, colour = C1 >0)) +
scale_colour_manual(name = 'PC1 > 0', values = setNames(c('red','green'),c(T, F))) +
xlab('PC1') + ylab('PC2')
相关文章
转载注明原文:根据R中的值绘制条件颜色 - 代码日志