ggplot2改变颜色如何 [英] ggplot2 change colours how

查看:205
本文介绍了ggplot2改变颜色如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


使用

  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_manual
让我们选择使用的颜色。

  ggplot(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 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.

这篇关于ggplot2改变颜色如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆