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

标签 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")

我想要有下标,例如表达式(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

最佳答案

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

关于r - 如何在 R read.csv 中为轴标签添加多个下标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40835424/

相关文章:

r - 控制 write.csv 中 R 中的数字

r - 如何在R中加载大数据?

r - ggplot2 是否有一种简单的方法来包装注释文本?

r - 在 R 中使用 timevis 创建嵌套子组

android - 如何将特定的 CSV 内容显示到单击的列表项?

python - 使用嵌套 for 和 if 循环时加快 Python 速度

r - 使用变量标签作为标题和轴标题时自动化 ggplots

r - 使用 'stat_compare_means' 函数时出现 'facet_wrap' 问题

excel - 提取然后将 Csv.gz 文件导入 Excel/SPSS...识别文本限定符有问题吗?

R 函数(循环?)为数据集中的每一列创建一个新图表