我做了下面的镶嵌图

使用此代码,但如您所见,顺序是错误的。-3支应该在左边,然后是-2和-1。我搞不懂是什么原因造成的,并感谢您的帮助。(事先非常感谢;)
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)))我的数据如下:
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")发布于 2022-08-16 15:18:15
一种解决方案是将Q15_4变量转换为factor或numeric,因为该变量属于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)))

https://stackoverflow.com/questions/73376341
复制相似问题