为 R 图的多个图例使用下标和变量
Using subscripts and variables for multiple legends for R plots
我的目标是在我的图例中有多行看起来像这种格式,Nsubscript(M), Nsubscript(F)。下标将用于制作我的图例shorthand。将需要评估变量以获得数值。如何制作带有下标和变量的多行图例?
我需要获得多个 bquotes 才能工作。下面的格式是我想要的图例部分。
#this works
N_male<-4#random variable
N_female<-6#random variable
plot(x=2,y=3)#random plot
legend("topright",legend=bquote('N'['F']*' = '*.( N_female)),
text.col="black",box.col=0, bty="n", cex = .75, lty= c(2)) #this legend has
#correct syntax
但是我需要让多个 bquotes 在同一个图例中工作。我该怎么做?
#this does not work
N_male<-4#random variable
N_female<-6#random variable
plot(x=2,y=3)
legend("topright",legend=c(bquote('N'['M']*' = '*.( N_male)),
bquote('N'['M']*' = '*.( N_female))),
text.col="black",box.col=0, bty="n", cex = .75, lty= c(1,2)) #this legend does not evaluate the bquote part
这有效。
plot(x=2,y=3)
legend("topright",legend=c(expression(paste('N'[M], ' = ', 4)),
expression(paste('N'[F], ' = ', 6))),
text.col="black",box.col=0, bty="n", cex = .75, lty= c(1,2))
我测试了其他方法。 lengend
中的变量和下标一起使用似乎是不可能的。等待高手解答!!
您需要在 c
调用中将 as.expression
与您的 bquote
一起使用。
my.expressions <-c(as.expression(bquote('N'['F']*' = '*.( N_female))),as.expression(bquote('N'['M']*' = '*.( N_male))) )
N_male<-4#random variable
N_female<-6#random variable
plot(x=2,y=3)#random plot
legend("topright",legend=my.expressions,
text.col="black",box.col=0, bty="n", cex = .75, lty= c(2:3))
我的目标是在我的图例中有多行看起来像这种格式,Nsubscript(M), Nsubscript(F)。下标将用于制作我的图例shorthand。将需要评估变量以获得数值。如何制作带有下标和变量的多行图例?
我需要获得多个 bquotes 才能工作。下面的格式是我想要的图例部分。
#this works
N_male<-4#random variable
N_female<-6#random variable
plot(x=2,y=3)#random plot
legend("topright",legend=bquote('N'['F']*' = '*.( N_female)),
text.col="black",box.col=0, bty="n", cex = .75, lty= c(2)) #this legend has
#correct syntax
但是我需要让多个 bquotes 在同一个图例中工作。我该怎么做?
#this does not work
N_male<-4#random variable
N_female<-6#random variable
plot(x=2,y=3)
legend("topright",legend=c(bquote('N'['M']*' = '*.( N_male)),
bquote('N'['M']*' = '*.( N_female))),
text.col="black",box.col=0, bty="n", cex = .75, lty= c(1,2)) #this legend does not evaluate the bquote part
这有效。
plot(x=2,y=3)
legend("topright",legend=c(expression(paste('N'[M], ' = ', 4)),
expression(paste('N'[F], ' = ', 6))),
text.col="black",box.col=0, bty="n", cex = .75, lty= c(1,2))
我测试了其他方法。 lengend
中的变量和下标一起使用似乎是不可能的。等待高手解答!!
您需要在 c
调用中将 as.expression
与您的 bquote
一起使用。
my.expressions <-c(as.expression(bquote('N'['F']*' = '*.( N_female))),as.expression(bquote('N'['M']*' = '*.( N_male))) )
N_male<-4#random variable
N_female<-6#random variable
plot(x=2,y=3)#random plot
legend("topright",legend=my.expressions,
text.col="black",box.col=0, bty="n", cex = .75, lty= c(2:3))