颜色以ggplot2中的列作为列 [英] Color points with the color as a column in ggplot2

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

问题描述

  d <-data.frame(

我有一个数据框,其中给出了列中每个点的颜色: x = 1:10,y = 1:10,col = c(rep(red,n = 5),rep(green,n = 5)))
d $ col< -as.character (d $ col)
ggplot(data = d,aes(x = x,y = y,color = col))+ geom_point()

正如您所看到的,颜色不会被解释为颜色,而是一个组,



可以通过ggplot处理这种情况吗? >

这个问题可能以前曾被问及过。然而,在设置数据时还有另外一个问题。



OP正在创建数据



<$ p $ (x = 1:10,
y = 1:10,
col = c(rep(red,n = 5), rep(green,n = 5)))

这会导致两种颜色

  d 
#xy col
#1 1 1红色
#2 2 2绿色
#3 3 3红色
#4 4 4绿色
#5 5 5红色
#6 6 6绿色
#7 7 7红色
# 8 8 8绿色
#9 9 9红色
#10 10 10绿色

原因是 n 不是 rep()函数的定义参数。根据?rep ,有效参数是 lenght.out 每个



可能是OP的含义是

  d < -  data.frame(x = 1:10,
y = 1:10,
col = c(rep(red ,5),rep(green,5)))

以相同的颜色:

  d 
#xy col
#1 1 1红色
#2 2 2红色
#3 3 3红色
#4 4 4红色
#5 5 5红色
#6 6 6绿色
#7 7 7绿色
#8 8 8绿色
#9 9 9绿色
#10 10 10绿色

顺便说一句,

  col = c(rep(red,5),rep(绿色,5))

可以更清晰地写成

  col = rep(c(red,green),each = 5)






有了这个,下面的plo t语句

  library(ggplot2)
#variant 1(OP自己的回答)
ggplot(data = d,aes(x = x,y = y))+ geom_point(color = d $ col)
#变体2(aosmith'comment,更多ggplot2-like)
ggplot(data = d ,aes(x = x,y = y,color = col))+ geom_point()+
scale_colour_identity()

产生相同的图表:


I have a dataframe where a color is given for each point in a column:

d<-data.frame(x=1:10,y=1:10,col=c(rep("red",n=5),rep("green",n=5)))
d$col<-as.character(d$col)
ggplot(data=d,aes(x=x,y=y,colour=col))+geom_point()

As you can see, the colour is not interpreted as a colour, but a group,

can ggplot handle such cases?

解决方案

This question has probably been asked and answered before. However, there is another issue in setting up the data.

The OP is creating the data by

d <- data.frame(x = 1:10,
                y = 1:10,
                col = c(rep("red", n = 5), rep("green", n = 5)))

This results in an alternation of the two colours

d
#    x  y   col
#1   1  1   red
#2   2  2 green
#3   3  3   red
#4   4  4 green
#5   5  5   red
#6   6  6 green
#7   7  7   red
#8   8  8 green
#9   9  9   red
#10 10 10 green 

Reason is that n is not a defined parameter to the rep() function. According to ?rep, valid parameters are times, lenght.out, and each.

Probably, the OP has meant

d <- data.frame(x = 1:10,
                y = 1:10,
                col = c(rep("red", 5), rep("green", 5)))

which results in successive rows being coloured in the same colour:

d
#    x  y   col
#1   1  1   red
#2   2  2   red
#3   3  3   red
#4   4  4   red
#5   5  5   red
#6   6  6 green
#7   7  7 green
#8   8  8 green
#9   9  9 green
#10 10 10 green

By the way,

col = c(rep("red", 5), rep("green", 5))

can be written more clearly as

col = rep(c("red", "green"), each = 5)


With this, the following plot statements

library(ggplot2)
# variant 1 (OP's own answer)
ggplot(data = d, aes(x = x, y = y)) + geom_point(colour = d$col)
# variant 2 (aosmith' comment, more "ggplot2-like")
ggplot(data = d, aes(x = x, y = y, colour = col)) + geom_point() + 
  scale_colour_identity()

produce the same chart:

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

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