2

I want to do a ggplot2 scatter plot

    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()

this code colours the data points in a gradient, so that thez are often not really distinguishable. I saw that

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

uses

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

for colouring but if I enter

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

I get an error message

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

can somebody tell me how I can colour the scatter graph with either not gradient colours OR different symbols?

4

1 回答 1

6

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()

如果您想选择形状(使用通常的 Rpch代码),请使用scale_shape_manual.

于 2013-04-30T21:18:00.387 回答