2

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
  • 3
    convert your data to tidy by using pivot_longer and then try again. Don't think in excel terms. Commented Apr 29, 2021 at 6:10

1 Answer 1

4
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

Sign up to request clarification or add additional context in comments.

3 Comments

PS use mutate(Entry = fct_inorder(Entry)) %>% if you want to preserve the data frame order of Entries instead of going alphabetically.
Thank you Jon Spring. Could you just explain why filter(value == 1).
OP asked to plot if "protein is found (==1)". pivot_longer by default makes a column called value to hold the original values of A:D, not to be confused with Value from the original data. Probably good practice in this case to rename more distinctly, e.g. withpivot_longer(cols = A:D, values_to = "found")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.