如何在这个 R read.csv 中为轴标签设置多个下标?
How to have many subscripts in this R read.csv for axis labels?
数据码
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")
#
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 输出
做 g + scale_x_discrete(labels = parse(text = datm$variable))
给了我
用字母测试 rawr 的答案
代码相关行
"female Bi,Bp,Br,Bt,B0,Bpr,Bpt,Brt,Bprt
输出
R: 3.3.2
OS:Debian 8.5
parse(text=...))
是一种将字符串转换为表达式的简单方法,特别适用于绘图
parse(text = c('B[ij]', 'B[12]'))
# expression(B[ij], B[12])
在您的示例中,您可以插入括号并使用 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))
数据码
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")
#
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 输出
做 g + scale_x_discrete(labels = parse(text = datm$variable))
给了我
用字母测试 rawr 的答案
代码相关行
"female Bi,Bp,Br,Bt,B0,Bpr,Bpt,Brt,Bprt
输出
R: 3.3.2
OS:Debian 8.5
parse(text=...))
是一种将字符串转换为表达式的简单方法,特别适用于绘图
parse(text = c('B[ij]', 'B[12]'))
# expression(B[ij], B[12])
在您的示例中,您可以插入括号并使用 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))