有谁知道如何从vcd包中为fourfold()添加自定义图例?
我可以在base中使用legend()函数添加到fourfoldplot()中,但我想使用fourfold()自带的OR≈1.0和OR≠1.0的自定义颜色。
当我使用fourfold()创建一个图,然后尝试使用legend()添加一个图例时,我得到了以下错误...
在strwidth(legend, units = "user", cex = cex, font = text.font)中出错。
plot.new还没有被调用
下面是一个例子。
# install.packages("vcd")
library(vcd)
x<-matrix(c(1146,210,471,897,
669,687,809,559,
761,595, 1030,338,
1227,129,1306,62,
844,737,773,370,
1134,447,344,799,
1001,580,790,353,
1461,120,1072,71,
820,837,797,270,
866,791,612,455,
1370,287,421,646,
1583,74,950,117,
1462,1056,155,51,
1349,1169,129,77,
1688,830,103,103,
2419,99,114,92),ncol=8,nrow=8)
x
x<-as.table(x)
x<-as.data.frame(x)
xarray<-array(x$Freq, dim=c(2,2,16))
xarray<-aperm(xarray,c(2,1,3))
dimnames(xarray)[[1]] <- c("No", "Yes")
dimnames(xarray)[[2]] <- c("No", "Yes")
dimnames(xarray)[[3]] <- c("Morning:Morning","Morning:Afternoon","Morning:Evening","Morning:Night-time",
"Afternoon:Morning","Afternoon:Afternoon","Afternoon:Evening","Afternoon:Night-time",
"Evening:Morning","Evening:Afternoon","Evening:Evening","Evening:Night-time",
"Night-time:Morning","Night-time:Afternoon","Night-time:Afternoon","Night-time:Night-time")
names(dimnames(xarray)) <- c("Post", "Pre", "Time of day")
fourfold(xarray, c("#99CCFF", "#6699CC", "#999999", "#E69F00", "#56B4E9", "#009E73"), ticks = 0.25,
main = "Did you usually train in the morning, afternoon, evening or night-time?", p_adjust_method="bon")
legend("right",legend = c("Non Sig.","Sig"), fill = c("#E69F00", "#009E73"))
fourfold
是在 网格, legend()
不起作用。你可以尝试使用vcd的grid_legend()函数。
library(vcd)
png("test.png",width=800,height=600)
fourfold(xarray, c("#99CCFF", "#6699CC", "#999999", "#E69F00", "#56B4E9", "#009E73"),
ticks = 0.25,
main = "Did you usually train in the morning, afternoon, evening or night-time?",
p_adjust_method="bon")
grid_legend(0.9, 0.5, c(15, 15), c("#E69F00", "#009E73"),
c("Non Sig.","Sig"))
dev.off()
在grid_legend()函数中,前两个参数是x和y,也就是图例在绘图设备上的相对位置。所以0.9就像90%在右边,0.5在中间,高度方面。