R ggplot2如何改变颜色
我想做一个ggplot2散点图R ggplot2如何改变颜色,r,ggplot2,R,Ggplot2,我想做一个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() 使用 为了上色,但如果我进去 ggplot( scores, aes( x =
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()
使用
为了上色,但如果我进去
ggplot( scores, aes( x = PC1, y = PC2, colour = SampleID ) ) +
geom_point(aes(colour = factor(cyl)))
我收到一条错误消息
in factor(cyl) : object 'cyl' not found
有人能告诉我如何用非渐变色或不同符号给散点图上色吗?
scale\u color\u manual
让我们来选择使用的颜色
ggplot( scores, aes( x = PC1, y = PC2, colour = SampleID ) ) +
geom_point() +
scale_color_manual(values = c("red", "black", "dodgerblue2"))
示例中的cyl
指示例中使用的mtcars
数据集的cyl
列。如果您更喜欢使用形状而不是颜色,请不要使用颜色
美学,而是使用形状
美学
ggplot( scores, aes( x = PC1, y = PC2, shape = SampleID ) ) +
geom_point()
如果要选择形状(使用常规的R
pch
code),请使用scale\u shape\u manual
错误说明一切<代码>气缸不存在。使用变量名(SampleID
)。
ggplot( scores, aes( x = PC1, y = PC2, colour = SampleID ) ) +
geom_point() +
scale_color_manual(values = c("red", "black", "dodgerblue2"))
ggplot( scores, aes( x = PC1, y = PC2, shape = SampleID ) ) +
geom_point()