0

I hope I can explain this correctly... basically, what Im trying to do is to remove points on a grid where there is no data... but the issue is, Im trying to do this with 2 factors!

Hopefully I can explain more clearly below.

To begin I have 2 factors drink and food, as shown below. Then I'm creating a grid (which im using to calculate something else) but I'm trying to remove 'points' from the grid where there is no data... for example:

drink = as.factor(c("A","A","A","A","A","A","A","A","A","A","A","B"))
food = as.factor(c('pizza','pizza','pizza','fries','fries','taco','taco','pizza','taco','pizza','taco','fries'))

# looking at a contingency table
table(drink, food)

>     food
drink fries pizza taco
    A     2     5    4
    B     1     0    0

Now Im creating the grid that spans the entire range of the data like so:

# create the grid
gridvals1 <- levels(drink)
gridvals2 <- levels(food)
gridvalsNew <- expand.grid(gridvals1, gridvals2)

If we plot the data and the grid side-by-side, we can see that the grid covers area where there is no data:

par(mfrow=c(1,2))
plot(drink, food)
plot(gridvalsNew)

plots

What Im trying to do is resize the grid so it removes the area where there is no data (i.e., where the count iz zero) . But I cant figure it out.

Electrino
  • 2,636
  • 3
  • 18
  • 40
  • 1
    If you only want the combinations present in the data, maybe instead of computing all combinations and trying to narrow them down, you could go directly to the unique combinations present in the data - `unique(data.frame(drink, food))`? – Gregor Thomas Mar 18 '21 at 00:18
  • See `mosaicplot` [et al](https://stackoverflow.com/questions/19233365/how-to-create-a-marimekko-mosaic-plot-in-ggplot2), maybe. – Henrik Mar 18 '21 at 00:18

0 Answers0