如何在R中仅对直方图的标签之一上色? [英] How to color only one of the labels of a histogram in R?

查看:218
本文介绍了如何在R中仅对直方图的标签之一上色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数据框:

I have a data frame like this:

CellLines   ZEB1
600MPE  2.8186
AU565   2.783
BT20    2.7817
BT474   2.6433
BT483   2.4994
BT549   3.035
CAMA1   2.718
DU4475  2.8005
HBL100  2.6745
HCC38   3.2884
HCC70   2.597
HCC202  2.8557
HCC1007 2.7794
HCC1008 2.4513
HCC1143 2.8159
HCC1187 2.6372
HCC1428 2.7327
HCC1500 2.7564
HCC1569 2.8093

我可以绘制一个直方图并为其添加标签(感谢这里的人提供的"

I could draw a histogram and add labels to that (thanks to the guys here for their help with this "Make a histogram clearer by assigning names to different segments of each bar in R"). I was wondering if there is a way to color only one of the labels on the histogram, e.g. only color "HCC1008". The codes so far is (thanks to @Carlos Cinelli) :

Heiser_hist <- hist(Heiser$ZEB1[1:19], breaks=50, col="grey")
Heiser$cut <- cut(Heiser$ZEB1, breaks=Heiser_hist$breaks)
library(dplyr)
Heiser <- Heiser %>% group_by(cut) %>% mutate(pos = seq(from=1, to=2, length.out=length(ZEB1)))
with(Heiser, text(ZEB1, pos, labels=CellLines, srt=45, cex=0.9))

值得一提的是,我检查了以下页面,但似乎没有一个页面可以解决以下问题: 在R中的智能点标签放置

It should be mentioned that I checked these pages but it seems that none of them address this issues: Intelligent point label placement in R, Labeling not all points on the plot

在此先感谢您的帮助:-)

Thanks in advance for any help with this :-)

推荐答案

一种简单的方法是创建一个新向量.像这样:

A simple way to do this is to create a new vector. Something like:

with(Heiser, {
  color_HCC1008 <- ifelse(Heiser$CellLines == "HCC1008", col1, col2)
  text(ZEB1, pos, labels = CellLines, srt = 45, cex = 0.9, col = color_HCC1008)
})

其中col1col2是您的不同颜色.您在这里所做的只是说:我希望该索引具有该颜色,而其他索引具有该颜色."这是R编程的一种非常通用的方法,我认为这是完成任何事情的最直接(但并不总是最好/最快)的方法.

where col1 and col2 are your different colors. All you're doing here is saying "I want this index to have this color, and the other indices to have that color." This is a very general approach to R programming and, in my opinion, is the most straightforward (but not always best/fastest) way to get anything done.

这篇关于如何在R中仅对直方图的标签之一上色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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