How do I make this conditional ggplot in R?

121 views Asked by At

I have around 40 variables, all products. The categories for all are either yes or no. They then link up to a rating of either low, medium or high.

My aim is to make a colored scatterplot of the ratings of each of the products that are marked yes.

reproducible example:

tab <- matrix(c('kmart',"y", "y", "n","low", 'target', "n", "n", "n","moderate",'bigw',"y", "y", "y","high"), ncol=5, byrow=TRUE)

colnames(tab) <- c('shop','dress','skirt','shoes','rating')

I need a scatterplot with the rating on the y axis, coloured based on the product. the dots will only show up when there is a yes in the category section.

Thank you!

1

There are 1 answers

0
Elia On

Do you mean something like this?

library(ggplot2)
tab <- as.data.frame(tab)
m <- reshape2::melt(tab,id.vars=c("shop","rating"))
ggplot(m[m$value=="y",],aes(x=shop,y=rating,col=variable))+
  geom_point()