Reputation: 1447
I have the following dataset:
Category <- c("Bankpass", "Bankpass", "Bankpass", "Moving", "Moving")
Subcategory <- c("Stolen", "Lost", "Login", "Address", "New contract")
Weight <- c(10,20,13,40,20)
Duration <- as.character(c(0.2,0.4,0.5,0.44,0.66))
df <- data.frame(Category, Subcategory, Weight, Duration)
I use that to create the following plot:
#install.packages("ggmosaic")
ggplot(data = df) +
geom_mosaic(aes(weight = Weight, x = product(Category), fill=Duration),
na.rm=TRUE) + theme(axis.text.x=element_text(angle=-25, hjust= .1))
This works however, I see small slices in the bar that dont make sense.
Any thoughts on how I can get rid of them?
Upvotes: 1
Views: 154
Reputation: 389
I see small slices in the bar that dont make sense.
It appears that the geom_mosaic call is plotting all levels of Subcategory for both Categories. Many would consider this to be a "feature" and not a "bug." See the plot below, which uses your exact call, but uses fill = Subcategory
You can also see this by using the command table(df$Category, df$Subcategory)
which shows
Address Login Lost New contract Stolen Bankpass 0 1 1 0 1 Moving 1 0 0 1 0
In any event, the easiest solution is the one mentioned by @esm above, use offset = 0
to hide these factors which have no entries.
Upvotes: 1
Reputation: 105
inside geom_mosaic
add offset = 0
.
ggplot(data = df) +
geom_mosaic(aes(weight = Weight, x = product(Category), fill=Duration),
offset = 0, na.rm=TRUE) +
theme(axis.text.x=element_text(angle=-25, hjust= .1))
Upvotes: 2