如何在这个R read.csv中为轴标签添加许多下标?

时间:2016-11-28 00:09:00

标签: r csv ggplot2 axis-labels subscript

数据代码

fem <- read.csv( text=
"female Bij,B11,B22,B33,B44,B21,B31,B32,B123
Sinus,1.0,0.0,0.0,0.0,0.0,0.0,12.0,0.0")

我希望订阅代替expression(B[11]),所以我的伪代码是

text=  
paste0("female ", expression(B[ij]), expression(B[11]), ..., expression(B[123]))  
Sinus,1.0,0.0,...")

也许,这可以通过更好的函数或稍后直接使用ggplot2来完成。 我最终将数据绘制为数据B11,...,B123位于female.Bij

的位置
library("ggplot2")
g <- ggplot(datm, aes(variable, value, fill=gender)) + geom_bar(stat="identity", position = position_dodge()) + facet_grid(female.Bij ~ group) + xlab("Type") 
#http://stackoverflow.com/a/17335258/54964
g + labs( y="Counts")

测试rawr的回答

在操作之前和操作之后,我的数据集

[1] "hello ==="
           male.Bij     gender             group              variable 
 Arr/AHB       :32   Length:128         Length:128         B11    :16  
 Digoxin arr   :32   Class :character   Class :character   B22    :16  
 Furosemide arr:32   Mode  :character   Mode  :character   B33    :16  
 Sinus         :32                                         B44    :16  
                                                           B21    :16  
                                                           B31    :16  
                                                           (Other):32  
     value                  male.Nij 
 Min.   : 0.000   Sinus         :32  
 1st Qu.: 0.000   Arr/AHB       :32  
 Median : 0.000   Digoxin arr   :32  
 Mean   : 1.407   Furosemide arr:32  
 3rd Qu.: 0.850                      
 Max.   :24.000                      

[1] "hello 2 ===="
           male.Bij     gender             group             variable        
 Arr/AHB       :32   Length:128         Length:128         Length:128        
 Digoxin arr   :32   Class :character   Class :character   Class :character  
 Furosemide arr:32   Mode  :character   Mode  :character   Mode  :character  
 Sinus         :32              

图。 1输出

enter image description here

执行g + scale_x_discrete(labels = parse(text = datm$variable))给我

enter image description here

用字母

测试rawr的回答

代码相关行

"female Bi,Bp,Br,Bt,B0,Bpr,Bpt,Brt,Bprt

输出

enter image description here

R:3.3.2
操作系统:Debian 8.5

1 个答案:

答案 0 :(得分:3)

parse(text=...))是将字符串转换为表达式的简单方法,尤其适用于绘图

parse(text = c('B[ij]', 'B[12]'))
# expression(B[ij], B[12])

在您的示例中,您可以插入括号并使用解析/文本

fem <- read.csv( text=
                   "female Bij,B11,B22,B33,B44,B21,B31,B32,B123
                 Sinus,1.0,0.0,0.0,0.0,0.0,0.0,12.0,0.0",
                 strip.white = TRUE)

datm <- reshape2::melt(fem)
datm <- within(datm, {
  ## take the first character as the base and the remaining
  ## characters as the subscript (wrap in brackets)
  variable <- gsub('(.)(.+)', '\\1[\\2]', variable)
})

library("ggplot2")
g <- ggplot(datm, aes(variable, value, fill=female.Bij)) +
  geom_bar(stat="identity", position = position_dodge()) +
  # facet_grid(female.Bij ~ group) +
  xlab("Type") 

g + labs( y="Counts") +
  scale_x_discrete(labels = parse(text = unique(datm$variable)),
                   breaks = unique(datm$variable))

enter image description here