将子字符串中的字符指定给facet_grid labeller的原始字符串

将子字符串中的字符指定给facet_grid labeller的原始字符串,r,ggplot2,labels,facet,R,Ggplot2,Labels,Facet,我为这里缺乏恰当的术语提前道歉,这在我的标题中最为明显。我是自学成才的,我的基本技能源于需要实现他人的生物研究代码。请在适用的地方更正 因此,要建立一个工作示例,请使用钻石: library(ggplot2) data(diamonds) diamonds <- diamonds[sample(nrow(diamonds), 1000), ] diamonds$cut <- factor(diamonds$cut,levels = c("Ideal", "Very Good", "

我为这里缺乏恰当的术语提前道歉,这在我的标题中最为明显。我是自学成才的,我的基本技能源于需要实现他人的生物研究代码。请在适用的地方更正

因此,要建立一个工作示例,请使用钻石:

library(ggplot2)
data(diamonds)

diamonds <- diamonds[sample(nrow(diamonds), 1000), ]
diamonds$cut <- factor(diamonds$cut,levels = c("Ideal", "Very Good", "Fair", "Good", "Premium"))

p <- ggplot(diamonds, aes(carat, ..density..)) +
    geom_histogram(binwidth = 1)
p + facet_grid(. ~ cut)
我的问题的解决方案应采用上面的列表,并优雅地概括如下:

sig_miRs_short<-list('hsa-miR-10b-5p_hsa-mir-10b'="hsa-miR-10b-5p", 'hsa-miR-143-3p_hsa-mir-143' = "hsa-miR-143-3p",
                   'hsa-miR-146b-5p_hsa-mir-146b'="hsa-miR-146b-5p",'hsa-miR-150-5p_hsa-mir-150' = "hsa-miR-150-5p",
                   'hsa-miR-196a-3p_hsa-mir-196a-2'="hsa-miR-196a-3p",'hsa-miR-199a-3p_hsa-mir-199a-2'="hsa-miR-199a-3p",
                   'hsa-miR-199b-3p_hsa-mir-199b'="hsa-miR-199b-3p",'hsa-miR-23c_hsa-mir-23c'="hsa-miR-23c",
                   'hsa-miR-4326_hsa-mir-4326'="hsa-miR-4326",'hsa-miR-4485-3p_hsa-mir-4485'="hsa-miR-4485-3p",
                   'hsa-miR-668-3p_hsa-mir-668'="hsa-miR-668-3p",'hsa-miR-6840-5p_hsa-mir-6840'="hsa-miR-6840-5p")
    sig_miR_labeller<-function(variable,value){return(sig_miRs_short[value])}

sig_miRs_short由于您只对长名称中下划线之前的部分感兴趣,因此有多种方法可以访问它

选项1:使用正则表达式。此标签器将下划线(和下划线)后的字符串的每个部分替换为空字符串

sig_miR_labeller2 <- function(variable, value){
  return(gsub("_.+","",value))
}

sig_miR_labeller2在这个例子中做得很好。看起来你已经准备好了,我想你可能需要一些正则表达式,但是你能用一些原始名称更新你的例子吗,以及你想要的基于这些名称的标签是什么?谢谢你的回复,Heroka。然而,我担心我误导了你。我生成的长名称列表既不包含“=”符号,也不包含缩写形式。我用一个更准确的例子编辑了Q。此外,使用diamonds示例,该列表将相当于执行:>LAB_NAMES,很高兴它成功了。但是我不理解“=”的问题,因为我使用下划线。那么我想我不理解如何实现您的解决方案。我生成了向量sig_miRs,其中包含长名称列表,但是sig_miRs不会出现在代码中。那么,“return(gsub(“.+”,“”,value)”是如何提取短名称的呢?我发现这个答案很有帮助。当我实施类似的建议时,我得到了:警告消息:2:labeller API已经更新。使用
变量
参数的labeller现在已被弃用。请参阅labeller文档。
NAMES<-c("Ideal", "Very Good", "Fair", "Good", "Premium")
SHORT_NAMES<-substr(NAMES, 1, 1)
LAB_NAMES<-list('Ideal'="I", 'Very Good' = "V",
               'Fair'="F",'Good' = "G",
               'Premium'="P")
>sig_miRs()
[1] "hsa-miR-10b-5p_hsa-mir-10b", "hsa-miR-143-3p_hsa-mir-143",
                   "hsa-miR-146b-5p_hsa-mir-146b","hsa-miR-150-5p_hsa-mir-150",
                   "hsa-miR-196a-3p_hsa-mir-196a-2","hsa-miR-199a-3p_hsa-mir-199a-2",
                   "hsa-miR-199b-3p_hsa-mir-199b","hsa-miR-23c_hsa-mir-23c",
                   "hsa-miR-4326_hsa-mir-4326","hsa-miR-4485-3p_hsa-mir-4485",
                   "hsa-miR-668-3p_hsa-mir-668","hsa-miR-6840-5p_hsa-mir-6840"
sig_miRs_short<-list('hsa-miR-10b-5p_hsa-mir-10b'="hsa-miR-10b-5p", 'hsa-miR-143-3p_hsa-mir-143' = "hsa-miR-143-3p",
                   'hsa-miR-146b-5p_hsa-mir-146b'="hsa-miR-146b-5p",'hsa-miR-150-5p_hsa-mir-150' = "hsa-miR-150-5p",
                   'hsa-miR-196a-3p_hsa-mir-196a-2'="hsa-miR-196a-3p",'hsa-miR-199a-3p_hsa-mir-199a-2'="hsa-miR-199a-3p",
                   'hsa-miR-199b-3p_hsa-mir-199b'="hsa-miR-199b-3p",'hsa-miR-23c_hsa-mir-23c'="hsa-miR-23c",
                   'hsa-miR-4326_hsa-mir-4326'="hsa-miR-4326",'hsa-miR-4485-3p_hsa-mir-4485'="hsa-miR-4485-3p",
                   'hsa-miR-668-3p_hsa-mir-668'="hsa-miR-668-3p",'hsa-miR-6840-5p_hsa-mir-6840'="hsa-miR-6840-5p")
    sig_miR_labeller<-function(variable,value){return(sig_miRs_short[value])}
sig_miR_labeller2 <- function(variable, value){
  return(gsub("_.+","",value))
}
#making some testdata, sampling from the long names
set.seed(123)
nobs=500

sig_miRs_short<-list('hsa-miR-10b-5p_hsa-mir-10b'="hsa-miR-10b-5p", 'hsa-miR-143-3p_hsa-mir-143' = "hsa-miR-143-3p",
                     'hsa-miR-146b-5p_hsa-mir-146b'="hsa-miR-146b-5p",'hsa-miR-150-5p_hsa-mir-150' = "hsa-miR-150-5p",
                     'hsa-miR-196a-3p_hsa-mir-196a-2'="hsa-miR-196a-3p",'hsa-miR-199a-3p_hsa-mir-199a-2'="hsa-miR-199a-3p",
                     'hsa-miR-199b-3p_hsa-mir-199b'="hsa-miR-199b-3p",'hsa-miR-23c_hsa-mir-23c'="hsa-miR-23c",
                     'hsa-miR-4326_hsa-mir-4326'="hsa-miR-4326",'hsa-miR-4485-3p_hsa-mir-4485'="hsa-miR-4485-3p",
                     'hsa-miR-668-3p_hsa-mir-668'="hsa-miR-668-3p",'hsa-miR-6840-5p_hsa-mir-6840'="hsa-miR-6840-5p")

testnames <- names(sig_miRs_short)
testdata <- data.frame(x=runif(nobs),y=runif(nobs),miR=sample(testnames,nobs,T))
sig_miR_labeller <- function(variable, value){
  return(gsub("_.+","",value))
}

p1 <- ggplot(testdata, aes(x=x,y=y))+
  geom_point() +
  facet_grid(.~miR, labeller=sig_miR_labeller)
p2 <- ggplot(testdata, aes(x=x,y=y))+
  geom_point()+
  facet_grid(.~pretty_miR)