根据data.frame列值R分配点颜色 [英] Assign point color depending on data.frame column value R

查看:181
本文介绍了根据data.frame列值R分配点颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在从csv读取数据,其中 R data< -read.csv(/ data.csv)并得到如下所示:

 组别xy大小颜色
中1 2 2000黄
小-1 2 1000红
大2 -1 4000绿
其他-1 -1 2500蓝色

每组颜色可能有所不同,它们由公式当生成 csv 文件时,这些都是可能的颜色(组数也可能不同)。



我一直在尝试使用 ggplot(),例如:

 <$ c $ (abs(min(data $ x)),abs(max(data $ x))))$ b $(数据< -read.csv(data.csv))
xlim <-max b ylim <-max(c(abs(min(data $ y)),abs(max(data $ y))))
data $ Color <-as.character(data $ Color)
print(data)
ggplot(data,aes(x = x,y = y,label = Group))+
geom_point(a es(size = size,color = Group),show.legend = TRUE)+
scale_color_manual(values = c(data $ Color))+
geom_text(size = 4)+
scale_size (range = c(5,15))+
scale_x_continuous(name =x,limits = c(xlim * -1-1,xlim + 1))+
scale_y_continuous(name =y ,limits = c(ylim * -1-1,ylim + 1))+
theme_bw()

一切都是正确的,除了颜色


  • 小被绘制为蓝色

  • 中已被绘制为红色

  • 其他绘制绿色

  • 大绘制黄色



我注意到右侧的图例按字母顺序排列组(大,中,小,小),但颜色保持在 csv 文件顺序中。



以下是情节的截图。





任何人都可以告诉我什么是mi在我的代码中解决这个问题?其他方法来达到相同的结果是可喜的。

解决方案

help(scale_colour_manual)是使用命名字符向量:

  col<  -  as .character(data $ Color)
names(col)< - as.character(data $ Group)

然后将比例尺的参数映射到这个向量中

 #只显示相关行
scale_color_manual(values = col)+

完整代码

  xlim <-max(c(abs(min(data $ x)),abs(max(data $ x )))))
ylim< -max(c(abs(min(data $ y)),abs(max(data $ y))))

col < - as。字符(数据$ Color)
名称(col)< - as.character(数据$ Group)

ggplot(data,aes(x = x,y = y,label = Group ))+
geom_point(aes(size = size,color = Group),show.legend = TRUE)+
scale_color_manual(values = col)+
geom_text(s ize = 4)+
scale_size(range = c(5,15))+
scale_x_continuous(name =x,limits = c(xlim * -1-1,xlim + 1))+
scale_y_continuous(name =y,limits = c(ylim * -1-1,ylim + 1))+
theme_bw()

输出:





数据

 数据<  -  read.table(Group xy size Color 
中1 2 2000黄
小-1 2 1000红
大2 -1 4000绿
其他-1 -1 2500蓝,头= TRUE)


this is my first question on SO, I hope someone can help me answer it.

I'm reading data from a csv with R with data<-read.csv("/data.csv") and get something like:

Group    x   y  size    Color
Medium   1   2  2000    yellow
Small   -1   2  1000    red
Large    2  -1  4000    green
Other   -1  -1  2500    blue

Each group color may vary, they are assigned by a formula when the csv file is generated, but those are all the possible colors (the number of groups may also vary).

I've been trying to use ggplot() like so:

data<-read.csv("data.csv")
xlim<-max(c(abs(min(data$x)),abs(max(data$x))))
ylim<-max(c(abs(min(data$y)),abs(max(data$y))))
data$Color<-as.character(data$Color)
print(data)
ggplot(data, aes(x = x, y = y, label = Group)) +
geom_point(aes(size = size, colour = Group), show.legend = TRUE) +
scale_color_manual(values=c(data$Color)) +
geom_text(size = 4) +
scale_size(range = c(5,15)) +
scale_x_continuous(name="x", limits=c(xlim*-1-1,xlim+1))+
scale_y_continuous(name="y", limits=c(ylim*-1-1,ylim+1))+
theme_bw()

Everything is correct except for the colors

  • small is drawn blue
  • Medium is drawn red
  • Other is drawn green
  • Large is drawn yellow

I noticed the legend at the right orders the Groups alphabetically (Large, Medium, Other, Small), but the colors stay in the csv file order.

Here is a screenshot of the plot.

Can anyone tell me what's missing in my code to fix this? other approaches to achieve the same result are welcome.

解决方案

One way to do this, as suggested by help("scale_colour_manual") is to use a named character vector:

col <- as.character(data$Color)
names(col) <- as.character(data$Group)

And then map the values argument of the scale to this vector

# just showing the relevant line
scale_color_manual(values=col) +

full code

xlim<-max(c(abs(min(data$x)),abs(max(data$x))))
ylim<-max(c(abs(min(data$y)),abs(max(data$y))))

col <- as.character(data$Color)
names(col) <- as.character(data$Group)

ggplot(data, aes(x = x, y = y, label = Group)) +
  geom_point(aes(size = size, colour = Group), show.legend = TRUE) +
  scale_color_manual(values=col) +
  geom_text(size = 4) +
  scale_size(range = c(5,15)) +
  scale_x_continuous(name="x", limits=c(xlim*-1-1,xlim+1))+
  scale_y_continuous(name="y", limits=c(ylim*-1-1,ylim+1))+
  theme_bw()

Ouput:

Data

data <- read.table("Group    x   y  size    Color
Medium   1   2  2000    yellow
Small   -1   2  1000    red
Large    2  -1  4000    green
Other   -1  -1  2500    blue",head=TRUE)

这篇关于根据data.frame列值R分配点颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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