How to square a subgroup in the presence of a formula in R

Neither this post nor this post is applicable to my case.

Let's assume:

set.seed(42)
x<-rep(c("A","B","C"), c(3,4,1))
y<-rep(c("V","W"),c(5,3))
z<-rnorm(8,-2,1)
df<-data.frame(x,y,z)
boxplot(z~x+y,df)

      

I want my story to include groups with more than, say, one element. This means that I want my plot to only show AV, BV and BW Also, since my graph contains about 70 groups, I don't want to do this by writing the list by hand.

thank

+3


source to share


2 answers


You can create a new column (xy) with paste

, create a boolean index using ave

"xy" for groups that have more than one item, then do boxplot

.

df1$xy <- factor(paste(df1$x, df1$y, sep='.'))
index <-  with(df1, ave(1:nrow(df1), xy, FUN=length))>1
boxplot(z~xy, droplevels(df1[index,]))

      

enter image description here



Or using ggplot

library(dplyr)
library(tidyr)
library(ggplot2)

df %>% 
   group_by(x,y) %>%
   filter(n()>1) %>%
   unite(xy, x,y) %>% 
   ggplot(., aes(xy, z))+
                geom_boxplot()

      

+5


source


You can see if there is bp$n

0 and a subset with that

set.seed(42)
df <- data.frame(x = rep(c("A","B","C"), c(3,4,1)),
                 y = rep(c("V","W"),c(5,3)),
                 z = rnorm(8,-2,1))
bp <- boxplot(z ~ x + y, df, plot = FALSE)
wh <- which(bp$n == 0)

bp[] <- lapply(bp, function(x) if (length(x)) {
  ## `bp` contains a list of boxplot statistics, some vectors and
  ## some matrices which need to be indexed accordingly
  if (!is.null(nrow(x))) x[, -wh] else x[-wh] 
  ## some of `bp` will not be present depending on how you called
  ## `boxplot`, so if that is the case, you need to leave them alone
  ## to keep the original structure so `bxp` can deal with it
  } else x)

## call `bxp` on the subset of `bp`
bxp(bp)

      

enter image description here



Or you can use whatever value you like:

wh <- which(bp$n <= 1)

bp[] <- lapply(bp, function(x) if (length(x)) {
  if (!is.null(nrow(x))) x[, -wh] else x[-wh] 
} else x)

bxp(bp)

      

enter image description here

+3


source







All Articles