改进的马赛克图(如热图或气泡)

amr*_*rrs 3 r crosstab ggplot2

我有两个因素,我正在制作它的交叉表.

x<-table(clean$VMail.Plan,clean$International.Plan)
Run Code Online (Sandbox Code Playgroud)

我实际上想以图形方式表示它:

在此输入图像描述

mosaicplot(x,data=clean,color=2:4,legend=TRUE,las = 1)
Run Code Online (Sandbox Code Playgroud)

正常的镶嵌图不是很吸引人,我无法使用ggplot2包使这更具吸引力.要么像热图还是泡泡?

实际的列联表:

        No   Yes
  No    27  239
  Yes  243 2159
Run Code Online (Sandbox Code Playgroud)

Jaa*_*aap 8

首先将数据重新整形为长格式:

library(reshape2)
x <- melt(df)
names(x) <- c("iplan","vplan","value")
Run Code Online (Sandbox Code Playgroud)

之后,您可以轻松地制作一个情节,例如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()
Run Code Online (Sandbox Code Playgroud)

这给了:

在此输入图像描述

作为替代方案,您还可以考虑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()
Run Code Online (Sandbox Code Playgroud)

给出:

在此输入图像描述

使用数据:

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))
Run Code Online (Sandbox Code Playgroud)