How to create a dot plot from multiple columns in one plot?

Viewed 211

Consider a df that I would like to plot.

The exemplary df:

df
        Entry     A.    B.    C.    D.    Value  
        O60701    1     1     1     0     2.7181970
        Q8WZ42    1     1     1     1     3.6679832
        P60981    1     1     0     0     2.2974231
        Q15047    1     0     0     0     0.5535473
        Q9UER7    1     0     0     0     4.1030394

I want Entry to be on y axis and Value on x axis. Do you have any ideas how to create a plot, so that if a protein is found (==1) let us say in column A it would be a dot on a plot? Since we have four columns (A-D), there can be maximum 4 dots. Hence, I would like to be able to distinguish which dot (or any other shape) comes from which column.

Here is what I have so far:

ggplot(df, aes(x=Value, y=Entry)) + 
  geom_point(size=1) +
  theme_ipsum()
1 Answers
library(tidyverse)
df %>%
  pivot_longer(cols = A:D) %>%
  # by default, pivot_longer creates `name` column with either A/B/C/D,
  # and a `value` column holding the original 0/1 value from those columns
  filter(value == 1) %>% # only plot if protein found (A/B/C/D==1)
  ggplot(aes(Value, Entry, color = name)) + 
  geom_jitter(height = 0.1, width = 0.1) + # since you have multiple points at the same locations
  hrbrthemes::theme_ipsum()

enter image description here

Related