简体   繁体   English

R直方图。 如何标记一些而非全部数据点

[英]R histogram. How can I label some but not all individual data points

My dataframe contains three variables: 我的数据框包含三个变量:

Row_Number    Sample_ID    Expression_Level
1             hum_449      0.25
2             hum_459      0.35
4             mur_223      0.45

I want to produce histograms of the third column using 我想使用生成第三列的直方图

hist(dataframe$Expression_Level)

And I want to label some of the bars with a list a list of Sample_ID values that correspond to that particular expression level. 我想用列表标记一些条形,这些列表对应于特定表达水平的Sample_ID值列表。

I have the desired Sample_IDs stored as a list object and also as a data frame with corresponding Row_Number and Expression_Level values (essentially just a subset of the original data frame). 我将所需的Sample_IDs存储为列表对象,还存储为具有相应Row_Number和Expression_Level值的数据框(基本上只是原始数据框的一个子集)。 I don't know what to do next or even what to type into a search engine. 我不知道下一步该怎么做,甚至不知道该在搜索引擎中键入什么内容。

I have ggplot2 installed because friends told me it would probably be helpful but I am unfamiliar with it and face the same problem of not knowing what to look for when reading the documentation. 我安装了ggplot2,因为朋友告诉我这可能会有所帮助,但是我不熟悉它,并且面临着同样的问题,即在阅读文档时不知道要查找什么。 Would prefer not to install more packages if possible. 如果可能,不希望安装更多软件包。

You could use the following to add a label corresponding to the third element of Sample_ID to the third "bar" of a histogram. 您可以使用以下代码将与Sample_ID的第三个元素相对应的标签添加到直方图的第三个“条”上。 But, this seems like an odd way to go really, since the bars of a histogram are counts. 但是,这实际上似乎是一种奇怪的方法,因为直方图的条形图是计数。 Might you be wanting to use barplot instead? 您可能想使用barplot吗? same code would work with "barplot" instead of hist. 相同的代码将使用“ barplot”代替hist。

temp <- hist(dataframe$Expression_Level) mtext(text=Expression_Level[3],side=1,line=2,at=temp[3]) temp <-hist(dataframe $ Expression_Level)mtext(text = Expression_Level [3],side = 1,line = 2,at = temp [3])

Something like this? 像这样吗

set.seed(1)      # for reproduceale example
# crate sample data - you have this already
df <- data.frame(sample_ID=paste0("S-",1:100),
                 Expression_Level=round(runif(100),1),
                 stringsAsFactors=F)

# you start here...
labels     <- aggregate(sample_ID~Expression_Level,df,c)
labels$lab <- sapply(labels$sample_ID,function(x)paste(unlist(x),collapse="|"))

library(ggplot2)
ggplot(df, aes(x=factor(Expression_Level))) + 
  geom_histogram(fill="lightgreen",color="grey50")+
  geom_text(data=labels,aes(y=.1,label=lab),hjust=0)+
  labs(x="Expression_Level")+
  coord_flip()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM