Data code
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")
I would like to have subscripts instead like expression(B[11])
so my pseudocode is
text=
paste0("female ", expression(B[ij]), expression(B[11]), ..., expression(B[123]))
Sinus,1.0,0.0,...")
Maybe, this can be done with a function better or later directly with ggplot2
.
I plot the data eventually as where the data B11,...,B123
is in 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")
Before the operation and after the operation, my data set
[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
Fig. 1 Output
Doing g + scale_x_discrete(labels = parse(text = datm$variable))
gives me
Code relevant line
"female Bi,Bp,Br,Bt,B0,Bpr,Bpt,Brt,Bprt
Output
R: 3.3.2
OS: Debian 8.5
parse(text=...))
is an easy way to turn strings into expressions especially useful for plotting
parse(text = c('B[ij]', 'B[12]'))
# expression(B[ij], B[12])
In your example you can insert the brackets and use parse/text
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))