Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

r - How to change color palette of mosaic plot

I have following data and code:

> mat <- matrix(c(120,230,84,70,130,83,13,26,18),3)
> dimnames(mat) <- list(c("good","fair","poor"),c("a","b","c"))
> mat

> mat
       a   b  c
good 120  70 13
fair 230 130 26
poor  84  83 18
> 
> chisq.test(mat)

        Pearson's Chi-squared test

data:  mat
X-squared = 11.411, df = 4, p-value = 0.02231

> library(vcd)
> mosaic(mat, shade=T)

enter image description here

The chi-squared test shows significant difference but no color is seen since residuals are small. With higher residuals, colors are seen:

enter image description here

?mosaic shows this information:

highlighting_fill: color vector or palette function used for a highlighted variable, if any.

How can I change fill color for above plot?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No really an elegant solution but you can manually define your colours using gpar

mosaic(mat, shade=T, colorize = T, gp = gpar(fill=matrix(c("red","blue","light green", "green", "black","red","brown","white","blue"), 3, 3)))

The next step is to populate the matrix in gpar with sensible colours. There is a pretty good post here about how to assign a colour ramp based on values. https://stackoverflow.com/a/6320940/1502898


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...