繁体   English   中英

ggplot2如何更改颜色

[英]ggplot2 change colours how

我想做一个ggplot2散点图

    scores <- data.frame( SampleID = rep(LETTERS[1:3], 5), PC1 = rnorm(15), PC2 = rnorm(15) )
library( ggplot2 )
ggplot( scores, aes( x = PC1, y = PC2, colour = SampleID ) ) +
  geom_point()

此代码以渐变方式为数据点着色,因此z常常无法真正区分。 我看见了

http://docs.ggplot2.org/current/geom_point.html

用途

geom_point(aes(colour = factor(cyl)))

用于着色,但如果我输入

ggplot( scores, aes( x = PC1, y = PC2, colour = SampleID ) ) +
      geom_point(aes(colour = factor(cyl)))

我收到一条错误消息

 in factor(cyl) : object 'cyl' not found

有人可以告诉我如何使用非渐变颜色或其他符号为散点图着色吗?

scale_color_manual让我们选择使用的颜色。

ggplot( scores, aes( x = PC1, y = PC2, colour = SampleID ) ) +
    geom_point() +
    scale_color_manual(values = c("red", "black", "dodgerblue2"))

所述cyl中的示例指的是cyl所述的柱mtcars在实施例中使用的数据集。 如果您宁愿使用形状而不是颜色,则不要使用colour美学,而应使用shape美学。

ggplot( scores, aes( x = PC1, y = PC2, shape = SampleID ) ) +
    geom_point()

如果要选择形状(使用常规的R pch代码),请使用scale_shape_manual

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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