How to color only one of the labels of a histogram in R?

走远了吗. 提交于 2020-01-04 09:10:23

问题


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))

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 :-)


回答1:


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)
})

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.



来源:https://stackoverflow.com/questions/26249385/how-to-color-only-one-of-the-labels-of-a-histogram-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!