user2606645
user2606645

Reputation: 21

Mosaic plot for a single variable in R like a puzzle not just vertical bars

How can I obtain something similar to a mosaic plot but representing just the information from a frequency table for a single variable?

mosaicplot(table(my_var)) works fine, but only shows vertical bars.

Is it possible to obtain a mosaic plot like a puzzle of different tiles instead of just vertical bars? Something similar to this image:enter image description here

Upvotes: 1

Views: 209

Answers (2)

socialscientist
socialscientist

Reputation: 4242

As @Man.A noted, you can create a treemap with the treemap R package. A simple example:

# library
library(treemap)
#> Warning: package 'treemap' was built under R version 4.1.3

# Create data
group <- c("group-1","group-2","group-3")
value <- c(13,5,22)
data <- data.frame(group,value)

# treemap
treemap(data,
        index="group",
        vSize="value",
        type="index"
)

Upvotes: 0

Man.A.
Man.A.

Reputation: 31

I had the same question, today. I found out that the graph is called treemap and at least two libraries support creating it: treemap and plotly.

Upvotes: 2

Related Questions