GGPLOT2改变颜色如何(ggplot2 change colours how)

2019-09-01 14:10发布

我想做一个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()

这个代码渐变中的颜色数据点,使thez往往不能真正区分。 我看到了

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

有人可以告诉我怎么可以用或者不渐变颜色或不同颜色的符号的散点图?

Answer 1:

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



文章来源: ggplot2 change colours how
标签: r ggplot2