Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove empty level from Box plot with multiple levels

Tags:

r

I have a box plot with multiple levels d$a + d$b

d = data.frame(value = c(1,2,3,100), a = c("A","A","B","A"), b = c("C","D","C","C") )
boxplot(d$value ~ d$a + d$b, horizontal = TRUE)

when you run that code you will see that the B.D combination still shows up but it is empty. How do I remove it?

This is just a toy example. In reality I have 40+ combinations and do not want to remove the blank one by hand.

like image 961
user3022875 Avatar asked Oct 26 '25 09:10

user3022875


1 Answers

You can first use interaction (together with its drop argument) to create a new column of your data.frame, then plot it:

d <- data.frame(value = c(1,2,3,100), a = c("A","A","B","A"), b = c("C","D","C","C"))
d <- within(d, interaction <- interaction(a, b, drop = TRUE))
boxplot(value ~ interaction, data = d, horizontal = TRUE)

enter image description here


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!