简体   繁体   中英

VDS/R: change order of mosaicplot

I made the following 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")

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)))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM