数据代码
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")
在操作之前和操作之后,我的数据集
[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输出
执行g + scale_x_discrete(labels = parse(text = datm$variable))
给我
代码相关行
"female Bi,Bp,Br,Bt,B0,Bpr,Bpt,Brt,Bprt
输出
R:3.3.2
操作系统:Debian 8.5
答案 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))