doncherry doncherry - 1 month ago 8
R Question

Suppress legend in vcd assoc plot / Only one legend for multiple assoc plots

I’m producing several association plots in one plot with

vcd
’s
assoc()
. Instead of having one color legend for each plot, I’d like to have only one legend (on the right) that covers the residual range of all plots (but it’d be ok to set these values manually – in this case
c(-4.9, 6.4)
). Optimally, I’d like to keep the p-value for each plot, but that’s not that important.

I tried to find a solution in the
vcd
documentation, but was slightly overwhelmed, being relatively new to R and pretty much brand new to
vcd
and its underlying structures.

library(vcd)
data("HairEyeColor")
mplot(
assoc(HairEyeColor[,,Sex="Female"], ylim=c(-2, 6), shade=T, return_grob=T, main="Female"),
assoc(HairEyeColor[,,Sex="Male"], ylim=c(-2, 6), shade=T, return_grob=T, main="Male")
)


output

and and
Answer

enter image description here You can suppress the legend with the option legend=FALSE. Furthermore I add the option legend_width to fill the space next to the plot, if I don't put this option the both plots have a different size.

mplot(
  assoc(HairEyeColor[,,Sex="Female"], ylim=c(-2, 6), shade=T, return_grob=T, main="Female", 
        legend_width=5, legend=F),
  assoc(HairEyeColor[,,Sex="Male"],   ylim=c(-2, 6), shade=T, return_grob=T, main="Male", 
        legend=legend_resbased(pvalue=T, range=c(-4.9,6.4)))
)

With the function chisq.test() you can get the p-value. There should be a way to put in the plot...

a <- chisq.test(HairEyeColor[,,Sex="Male"])
a$p.value

The p-value in your case is part of the legend. On the first glance I did'nt found a option to suppress only parts of the legend. But maybe you will find more...

?strucplot
?legend_resbased