Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
988 views
in Technique[技术] by (71.8m points)

r - ggplot2 change colours how

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

scale_color_manual let's you pick the colors used.

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

The cyl in the example refers to the cyl column of the mtcars dataset used in the example. If you would rather use shapes then colors, don't use the colour aesthetic, use the shape aesthetic instead.

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

If you want to choose shapes (using usual R pch codes), then use scale_shape_manual.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...