3

I've two Factors and I'm making a crosstab of it.

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

I actually would like to graphically represent it like this:

enter image description here

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

The normal mosaicplot is not very attractive and I couldn't use ggplot2 package make this more attractive. Either something like a heatmap or with bubbles?

The actual Contingency table:

        No   Yes
  No    27  239
  Yes  243 2159
Jaap
  • 81,064
  • 34
  • 182
  • 193
amrrs
  • 6,215
  • 2
  • 18
  • 27
  • 2
    Please describe what's wrong with this plot and how would like it to be. Also include a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – Jaap Sep 23 '15 at 15:15
  • Please provide a more concise description of "more attractive" otherwise your question may be closed as "unclear" or "too broad". There are hundreds of examples of heatmaps and bubble plots around - what have you tried and why didn't it work the way you want? – Henrik Sep 23 '15 at 15:17
  • Hello @Jaap, I thought i mentioned it in the question. More attractive like a heatmap where different numbers have different shades level or a bubble like this: http://i.stack.imgur.com/Ypeqm.png – amrrs Sep 23 '15 at 16:21
  • sorry @Henrik for being not clear. I would like to represent my crosstab better than what i've done. either like a mosaic plot with different shades of color depending upon the value of it or like a table with bubbles like this http://i.stack.imgur.com/Ypeqm.png Please suggest me if this question is so naive i'd remove it – amrrs Sep 23 '15 at 16:25

1 Answers1

10

First reshape you data to long format:

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

After that you can easily make a plot with for example 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()

this gives:

enter image description here

As an alternative, you could also consider 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()

wich gives:

enter image description here

Used data:

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))
Jaap
  • 81,064
  • 34
  • 182
  • 193