控制ggplot中的点颜色 [英] Control dot colour in ggplot

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

问题描述

如何通过ggplot2控制散点图中点的颜色?我需要前20个点才有颜色,接下来20个要有不同的颜色。此刻,我正在使用base R绘图输出。矩阵看起来像这样

How can I control the colour of the dots in the scatter plot by ggplot2? I need the first 20 points to have a colour, then the next 20 to have a different colour. At the moment I am using base R plot output. The matrix looks like this

1 4
1 3
2 9
-1 8
9 9

我有一个颜色向量,看起来像

and I have a colour vector which looks like

cols<-c("#B8DBD3","#FFB933","#FF6600","#0000FF")

然后

then

plot(mat[,1],mat[,2],col=cols)

有效。

我怎样才能做到这个ggplot?

How could I do this ggplot?

关于颜色

我的cols向量看起来像这个
100-> n

my cols vector looks ike this 100->n

colours<-c(rep("#B8DBD3",n),rep("#FFB933",n),rep("#FF6600",n),rep("#0000FF",n),rep("#00008B",n),rep("#ADCD00",n),rep("#008B00",n),rep("#9400D3",n))

当然我做了

when I then do

d<-ggplot(new,aes(x=PC1,y=PC2,col=rr))
d+theme_bw() +
  scale_color_identity(breaks = rep(colours, each = 1)) +
  geom_point(pch=21,size=7)

颜色看起来完全不同于
plot(new [,1],new [,2],col = colors)
这看起来像
http://fs2.directupload.net/images/150417/2wwvq9u2.jpg
,而ggplot使用相同的颜色看起来像

the colours look completely different from plot(new[,1],new[,2],col=colours) this looks like http://fs2.directupload.net/images/150417/2wwvq9u2.jpg while ggplot with the same colours looks like

http:// fs1.directupload.net/images/150417/bwc5wn7b.jpg

推荐答案

我会建议创建一个指定列

I would recommend creating a column that designates to which group a point belongs to.

library(ggplot2)
xy <- data.frame(x = rnorm(80), y = rnorm(80), col = as.factor(rep(1:4, each = 20)))

cols<-c("#B8DBD3","#FFB933","#FF6600","#0000FF")

ggplot(xy, aes(x = x, y = y, col = col)) +
  theme_bw() +
  scale_colour_manual(values = cols) +
  geom_point()

这篇关于控制ggplot中的点颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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