簡體   English   中英

改進的馬賽克圖(如熱圖或氣泡)

[英]Improved Mosaic Plot (like Heatmap or bubbles)

我有兩個因素,我正在制作它的交叉表。

x<-table(clean$VMail.Plan,clean$International.Plan)

我實際上想以圖形方式表示它:

在此輸入圖像描述

mosaicplot(x,data=clean,color=2:4,legend=TRUE,las = 1)

正常的鑲嵌圖不是很吸引人,我無法使用ggplot2包使這更具吸引力。 要么像熱圖還是泡泡?

實際的列聯表:

        No   Yes
  No    27  239
  Yes  243 2159

首先將數據重新整形為長格式:

library(reshape2)
x <- melt(df)
names(x) <- c("iplan","vplan","value")

之后,您可以使用例如geom_point輕松制作繪圖:

library(ggplot2)
ggplot(x, aes(iplan, vplan)) +
  geom_point(aes(size = value), alpha=0.8, color="darkgreen", show_guide=FALSE) +
  geom_text(aes(label = value), color="white") +
  scale_size(range = c(15,50)) +
  theme_bw()

這給了:

在此輸入圖像描述

作為替代方案,您還可以考慮使用geom_tile

ggplot(x, aes(iplan, vplan)) +
  geom_tile(aes(fill = value)) +
  geom_text(aes(label = value), color="white") +
  scale_x_discrete(expand = c(0,0)) +
  scale_y_discrete(expand = c(0,0)) +
  scale_fill_gradient("Legend label", low = "lightblue", high = "blue") +
  theme_bw()

給出:

在此輸入圖像描述

使用數據:

df <- structure(list(iplan = structure(1:2, .Label = c("No", "Yes"), class = "factor"), No = c(27L, 243L), Yes = c(239L, 2159L)), .Names = c("iplan", "No", "Yes"), class = "data.frame", row.names = c(NA, -2L))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM