SUMIT
SUMIT

Reputation: 563

How to create dot plot with both negative and positive axes?

I have a r data frame and would like to make a dot plot in ggplot where x-axis would correspond to different groups present in Ensembl_ID row ("ENSG00000000003", "ENSG00000000005" etc. are the group for x-axis) and y-axis would correspond to respective numbers present in logFC.1 to logFC.9. I would like to display the logFC.1 to logFC.9 number of each respective group in form of dots like the attached image (to prevent confusion, each group should have the same color and only change with intensity like: if the mean x is bigger it gets more red and if the mean y is bigger it gets more green or something).

If possible then can you guys also help me in assigning the different color for positive and negative values.

Can we also assign the dot's color based on value (color intensity)?

df1 <- data.frame(Ensembl_ID = c("ENSG00000000003", "ENSG00000000005", "ENSG00000000419", "ENSG00000000457", "ENSG00000000460", "ENSG00000000938", "ENSG00000000971", "ENSG00000001036", "ENSG00000001084", "ENSG00000001167" ), logFC.1 = c(0.147447019707984, -0.278643924528991, 0.00638502079233481, 0.00248371473862579, 0.0591639590814736, -0.0892578080659792, -0.0139042150604349, 0.15210410748665, -0.0273174541997048, 0.0373813166759115 ), logFC.2 = c(0.14237211045168, -0.153847067952652, 0.00806519294435945, -0.0243298183425441, 0.0639184480028851, -0.0791126460573967, -0.0517704622015086, 0.100033161692714, 0.105136768894399, 0.0509474174745926 ), logFC.3 = c(0.0692402101693023, -0.212626837128185, 0.0665466667502187, 0.0189664498456434, 0.073631371224761, -0.0642014520794086, 0.0115060035255512, 0.104767159584613, 0.140378485980222, 0.0814931176279395), logFC.4 = c(0.175916688982428, -0.0606440302201137, 0.0862627141013101, 0.105179938123113, 0.128866411791584, -0.0988927171791539, 0.128758540724723, 0.0997656895899759, 0.345468063926355, 0.130898388184307), logFC.5 = c(0.144743421921328, 0.247159332221974, 0.0232237466183996, 0.0800788300610377, 0.178887735169961, -0.0592727391427514, -0.0723099661837084, 0.0387715967173523, -0.0607793368610136, 0.110464511693512), logFC.6 = c(0.0848187321362019, -0.299283590551811, 0.0366788808661408, -0.00763280370062748, 0.0145148270035513, -0.0384916970002755, -0.0000335640771631606, 0.0851895375297912, -0.00364050261322463, 0.0602143760128463), logFC.7 = c(0.305256444042024, -0.274308408751318, 0.0977066795857243, -0.0265659018074027, 0.136348613124811, -0.0938364533000299, -0.143634179166262, 0.139913812601005, 0.268708965044232, 0.133427360632365), logFC.8 = c(0.12744808339884, -0.285015311267508, 0.0459140048745496, -0.00976012971218515, 0.13292412700208, -0.184687147498946, -0.0411558715447517, 0.165717944056239, 0.323358546432839, 0.0502386767987279), logFC.9 = c(0.286824598926274, 0.095530985319937, 0.101370835445593, 0.0352336819150421, 0.0573659992830985, -0.0739779010955875, 0.00466993628480923, 0.0486643748696862, 0.0322601740536419, 0.0873158516027886))

Sample Image of expected output

This is the only test sample, in real data, each group has 1500-2000 values so if dot merging over each other is not an issue.

Thank you for your kind help. waiting to hear from you peoples

Upvotes: 1

Views: 647

Answers (2)

Jon Spring
Jon Spring

Reputation: 66490

library(tidyverse)
df1 %>%
  pivot_longer(-Ensembl_ID) %>%
  group_by(name) %>%
  mutate(grp_avg = mean(value)) %>%
  ungroup() %>%     # EDIT -- sort should happen outside groups
  mutate(name = fct_reorder(name, grp_avg)) %>%
  

ggplot(aes(Ensembl_ID, y = value, color = name)) +
  ggbeeswarm::geom_beeswarm() +
  scale_color_brewer() +
  theme(axis.text.x = element_text(angle = 20))

enter image description here

Upvotes: 2

TarJae
TarJae

Reputation: 78927

  1. bring your data in long format
  2. define a mid point mean
  3. use geom_jitter to account for overlaying points
  4. map the color and specify scale_color_gradient2

enter image description here

library(tidyverse)

df <- df1 %>% 
    pivot_longer(
        cols = -Ensembl_ID
    )

mid <- mean(df$value)

ggplot(df, aes(x = factor(Ensembl_ID), y = value, color=value)) +
    geom_jitter(size=4, 
                position = position_jitter(width = 0.2, height = 0.2)) +
    #scale_color_gradient(low="blue", high="red")
    scale_color_gradient2(midpoint=mid, low="blue", mid="grey",
                          high="red", space ="Lab" )+
    theme_classic()

Upvotes: 1

Related Questions