让子串在图中定义颜色

时间:2015-05-06 11:17:30

标签: r

请看下面的例子。我想使用sample-colmn中的数字后面的字母来定义我的绘图中的颜色。字母总是G或R.我如何在我的情节函数中定义它?

> head(p)
  sample           x           y
1   124G  0.67816902 -0.45960785
2   356G -0.35977946 -0.56862968
3   126G  0.50832373  0.59836738
4   235G -0.01906419 -0.17449796
5    26R -0.16938994  0.30487001
6    46G  0.89306942  0.05496897

p <- ggplot(p, aes(y,x)) 
p + geom_point(aes(colour=factor(Sample))) + geom_text(aes(colour=factor(Sample))

1 个答案:

答案 0 :(得分:1)

试试这个:

p <- read.table(text="
Sample x           y
1   124G  0.67816902 -0.45960785
2   356G -0.35977946 -0.56862968
3   126G  0.50832373  0.59836738
4   235G -0.01906419 -0.17449796
5    26R -0.16938994  0.30487001
6    46G  0.89306942  0.05496897",as.is=TRUE)

library("ggplot2")
ggplot(p, aes(y,x,colour=factor(gsub("\\d","",Sample)))) + 
  geom_point()

最好在绘图前定义组变量:

p$myGroup <- as.factor(gsub("\\d","",Sample))

#plot
ggplot(p, aes(y,x,colour=myGroup)) + 
  geom_point()

enter image description here