ggplot2改变颜色如何 [英] ggplot2 change colours how
问题描述
我想做一个ggplot2分散图
分数< - data.frame(SampleID = rep(LETTERS [1 :PC1 = rnorm(15)PC2 = rnorm(15))
library(ggplot2)
ggplot(scores,aes(x = PC1,y = PC2,color = SampleID ))+
geom_point()
此代码以渐变方式着色数据点,所以它们通常并没有真正的区分。我看到
http://docs.ggplot2.org/current/geom_point.html
让我们选择使用的颜色。
$ c
使用
geom_point(aes( color = factor(cyl)))
用于着色,但如果输入 $ b $ (aes(color = factor(cyl))b
ggplot(aes(x = PC1,y = PC2,color = SampleID))+
geom_point )))
我收到一条错误消息
< pre $
$有人可以告诉我如何使用不是渐变颜色或不同的符号来为散点图着色? $ c $> scale_color_manualggplot(scores,aes(x = PC1,y = PC2,color = SampleID))+
geom_point()+
scale_color_manual(values = c(red,black,dodgerblue2))
示例中的
cyl
引用的
数据集。如果您更愿意使用形状和颜色,请不要使用cyl
示例中使用的mtcars颜色
美学,而应该使用形状
审美。
ggplot(分数,aes(x = PC1,y = PC2,shape = SampleID))+
geom_point()
如果您想选择形状(使用通常的R
pch
codes),然后使用scale_shape_manual
。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?
解决方案
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 thecyl
column of themtcars
dataset used in the example. If you would rather use shapes then colors, don't use thecolour
aesthetic, use theshape
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 usescale_shape_manual
.这篇关于ggplot2改变颜色如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!