Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

VDS/R: change order of mosaicplot

I made the following mosaicplot mosaicplot using this code, but as you can see, the order is wrong. -3 schould be on the left, followed by -2 & -1. I am confused as to what is causing this and would appreciate your help. Thank you very much in Advance 😉

mosaic(data = Test,
       ~Q15_4 + StrategischeWahl2021,
       direction = c("v", "h"),
       shade = TRUE,
       labeling_args = list(set_varnames = c(StrategischeWahl2021 = "StrategischeWähler2021", Q15_4 = "Zustimmung RotRotGrün (-3: Absolute Ablehnung, 3: Absolute Zustimmung) "), 
                            set_labels = list(StrategischeWahl2021 = c("Nein", "Ja")), rep = FALSE),
       gp = shading_hcl, gp_args = list(interpolate = c(1, 1.8)))

my data is the following:

df <- Test %>% 
  select(Q15_4, StrategischeWahl2021) 
dput(df)

structure(list(Q15_4 = c("1", "2", "-3", "0", "3", "-3", "-3", 
"2", "-2", "-3", "2", "3", "2", "2", "-3", "2", "-3", "-3", "1", 
"-3", "-3", "-3", "2", "-2", "-3", "-1", "-1", "3", "2", "-3", 
"2", "-3", "-3", "0", "-3", "-1", "-3", "-3", "3", "-3", "-2", 
"-2", "2", "-3", "-1", "-3", "3", "-2", "-3", "1", "-2", "-3", 
"1", "-1", "1", "-3", "-3", "-3", "2", "0", "-2", "-3", "1", 
"-3", "1", "-2", "3", "-3", "2", "1", "3", "3", "-3", "3", "2", 
"-2", "-3", "1", "-3", "-1", "-3", "2", "-1", "1", "1", "0", 
"-3", "1", "3", "2", "-1", "2", "-2", "0", "1", "-3", "-3", "-1", 
"3", "-1", "2", "3", "-3", "1", "3", "-2", "-1", "-3", "3", "-3", 
"-2", "-3", "-1", "1", "2", "-2", "-3", "-3", "-3", "3", "3", 
"-3", "2", "3", "-2", "1", "2", "2", "1", "2", "1", "3", "3", 
"2", "1", "-2", "3", "3", "-2", "-3", "-2", "1", "1", "2", "1", 
"-1", "-2", "-2", "2", "-2", "1", "-3", "2", "2", "-1", "-1", 
"-2", "2", "2", "1", "1", "-1", "-3", "0", "3", "-2", "-2", "-3", 
"1", "-3", "0", "-3", "1", "3", "-3", "3", "-3", "-2", "-3", 
"-3", "-3", "-1", "-3", "-2", "1", "1", "3", "-3", "-1", "3", 
"-3", "2", "-3", "-3", "-3", "-3", "-3", "1", "-1", "1", "2", 
"-3", "3", "-3", "1", "1", "3", "3", "-3", "-2", "1", "-3", "1", 
"2", "1", "2", "-3", "0", "2", "1", "-1", "-1", "1", "1", "-3", 
"-2", "-3", "0", "1", "1", "1", "-3", "-3", "-3", "2", "-2", 
"0", "3", "-1", "-3", "3", "2", "-2", "2", "2", "1", "-3", "-2", 
"1", "2", "0", "1", "-3", "3", "-3", "0", "2", "-3", "-3"), StrategischeWahl2021 = c("0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "1", "1", "0", 
"0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "1", "0", "0", 
"0", "0", "0", "0", "1", "0", "1", "1", "0", "1", "1", "0", "1", 
"0", "1", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "1", 
"1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"1", "0", "0", "1", "0", "1", "0", "0", "1", "0", "1", "1", "0", 
"0", "0", "1", "1", "0", "1", "0", "1", "1", "0", "0", "0", "0", 
"0", "1", "0", "0", "0", "1", "0", "1", "1", "1", "1", "0", "1", 
"1", "0", "0", "0", "0", "1", "0", "1", "0", "0", "0", "1", "0", 
"0", "0", "1", "1", "0", "0", "1", "0", "1", "0", "0", "0", "0", 
"0", "1", "0", "0", "0", "1", "1", "0", "0", "0", "0", "1", "0", 
"1", "0", "0", "0", "1", "0", "1", "0", "0", "1", "0", "0", "1", 
"1", "0", "0", "0", "0", "0", "0", "0", "1", "1", "0", "1", "0", 
"1", "0", "0", "0", "0", "0", "1", "0", "1", "0", "1", "0", "0", 
"1", "0", "0", "0", "0", "0", "0", "0", "1", "1", "0", "0", "0", 
"1", "0", "0", "1", "0", "1", "0", "0", "0", "0", "0", "1", "0", 
"1", "1", "0", "1", "0", "0", "1", "0", "0", "0", "0", "1", "0", 
"1", "0", "1", "1", "1", "0", "0", "0", "0", "1", "0", "0", "0", 
"1", "1", "0", "0", "0", "1", "1", "0", "0", "0", "0")), row.names = 3:261, class = "data.frame")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A solution is to convert the Q15_4 variable to factor or to numeric because this variable was of class character :

Test$Q15_4=as.numeric(Test$Q15_4) # or Test$Q15_4=factor(Test$Q15_4,levels=-3:3)
mosaic(data = Test,
       ~Q15_4 + StrategischeWahl2021,
       direction = c("v", "h"),
       shade = TRUE,
       labeling_args = list(set_varnames = c(StrategischeWahl2021 = "StrategischeWähler2021", Q15_4 = "Zustimmung RotRotGrün (-3: Absolute Ablehnung, 3: Absolute Zustimmung) "), 
                            set_labels = list(StrategischeWahl2021 = c("Nein", "Ja")), rep = FALSE),
       gp = shading_hcl, gp_args = list(interpolate = c(1, 1.8)))

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading