Home > Software engineering >  Ggplot Aesthetics must be either length 1 or the same as the data
Ggplot Aesthetics must be either length 1 or the same as the data

Time:04-02

My ggplot code below is working on my Mac, but isn't working on my Linux instance.

As per the title, I get the error Error: Aesthetics must be either length 1 or the same as the data (1): x, y and colour with this code.

library(tidyverse)
library(plotly)
library(ggplot2)

plot_total <-
          demo %>%
          as.data.frame() %>%
          ggplot(aes(log2(baseMean), log2FoldChange, color=padj < 0.01,
                     text = paste(" log2(baseMean):", formatC(log2(baseMean), format = "e", digits = 2), "\n",
                                  "log2FoldChange:", formatC(log2FoldChange, format = "e", digits = 2), "\n",
                                  "Gene Name:", Gene, "\n",
                                  "EnsGeneID:", ENSG)))  
          geom_point(data = . %>% filter(!padj<0.01), cex = 0.1, size=0.1)  
          geom_point(data = . %>% filter(is.na(padj)), cex = 0.1, size=0.1)  
          geom_point(data = . %>% filter(padj<0.01), cex = 1, size=1)  
          scale_color_manual(values=c("FALSE" = "#30B7BC", "TRUE" = "#DE653A", "NA" = "#B3B3B3"))  
          labs(title = "Treated vs. Control", x = "log2Expression")  
          theme_bw()  
          NULL
    
    ggplotly(plot_total,
             tooltip = "text",
             type = 'scattergl') %>% 
      config(displaylogo = FALSE,
             modeBarButtonsToRemove = c('sendDataToCloud', 'select2d', 'lasso2d')) %>% 
      toWebGL()

The culprit seems to be the text variable. I have tried placing this variable inside each geom_point(aes()) where I'm filtering the data, but I get the same error. This is a small subset of the data:

ENSG Gene baseMean log2FoldChange lfcSE stat pvalue padj
ENSG00000000003.15 TSPAN6 2106.78150011798 -0.214594792659649 0.0830671887250501 -2.58338816990607 0.00978351787155086 0.0632360231867511
ENSG00000000005.6 TNMD 3.51684529099911 1.33352064084301 0.680105184521041 1.96075647001887 0.0499074381755472 NA
ENSG00000000419.13 DPM1 1156.63019852077 -0.184963193359761 0.0697284461507299 -2.65262175726578 0.00798693161495278 0.0556311026232396
ENSG00000000457.14 SCYL3 270.00909649605 -0.0558608208897551 0.0945944838404759 -0.590529369386473 0.554835801729326 0.752831745155342
ENSG00000000460.17 C1orf112 423.916241362646 0.00337838445840835 0.0904413626324259 0.0373544179352855 0.970202416501722 0.985859261611512
ENSG00000000938.13 FGR 1.4952572446323 0.189881722278028 1.0225523780978 0.18569388360453 0.852684825077251 NA
ENSG00000000971.16 CFH 8.35615935084791 -0.407549583396925 0.407283654269094 -1.00065293346552 0.316994629452948 NA
ENSG00000001036.14 FUCA2 3286.83167836124 -0.113975393359072 0.0532994402577163 -2.13839756680318 0.032484489796959 0.140642991190518
ENSG00000001084.13 GCLC 1525.77400190981 -0.0964283327267667 0.0697918041485629 -1.38165697108938 0.167077052186927 0.387307483908263
ENSG00000001167.14 NFYA 775.15489019341 -0.0282781481284204 0.108165475282215 -0.2614341411124 0.793757731444887 0.89507246376111
ENSG00000001460.18 STPG1 1105.63501614957 0.154649228441584 0.0886193148590866 1.74509618684697 0.0809681272261289 0.252149556355932
ENSG00000001461.17 NIPAL3 1755.50307288809 -0.0313646069063427 0.0699845764115142 -0.448164560172753 0.654034437625039 0.815455908057006
ENSG00000001497.17 LAS1L 2188.7653564921 -0.0523385791864584 0.0712541067124691 -0.734534212851194 0.462623216454675 0.684620504636789
ENSG00000001561.7 ENPP4 830.269207280792 -0.298210398294484 0.10493466780185 -2.84186727362211 0.00448501591940391 0.0369683950248886
ENSG00000001617.12 SEMA3F 507.841167325578 0.220524182082938 0.107840828258815 2.04490438031211 0.0408642859982967 0.163022035306157
ENSG00000001626.16 CFTR 130.392965133479 2.01581704225126 0.726570495644851 2.7744273327011 0.00552989865962655 0.0428809290359717
ENSG00000001629.10 ANKIB1 1378.77776720815 -0.00295677498548296 0.0912941535974317 -0.0323873421130675 0.974163156724246 0.987546941506975
ENSG00000001631.16 KRIT1 1.9871339581813 -0.292437549054293 0.868146512011618 -0.336852760459377 0.736227897832157 NA
ENSG00000002016.18 RAD52 680.085519620988 0.373097140440191 0.156407957981969 2.3854102134829 0.0170600862524028 0.0916401980775939
ENSG00000002079.14 MYH16 1.86080100353 -0.182051454327792 1.01764691310774 -0.178894518307764 0.858020527111129 NA
ENSG00000002330.14 BAD 551.20996778861 -0.0039768348985057 0.12107851487019 -0.0328450914909992 0.973798119784072 0.98747939145517
ENSG00000002549.12 LAP3 852.041745825179 -0.254029783752867 0.0719977839970102 -3.52830003439294 0.000418237809317195 0.00642614461337485
ENSG00000002587.10 HS3ST1 2.57897400765919 0.0144688913294909 0.777073236085079 0.0186197267613869 0.985144465883661 NA
ENSG00000002745.13 WNT16 16.1270763411369 -0.00537408371714073 0.30400368641928 -0.0176776925978747 0.985895976595832 0.993837819473658
ENSG00000002746.15 HECW1 46.6208055525063 -0.273107000095639 0.217402004605246 -1.25623036729372 0.209032459705226 0.438247467420401
ENSG00000002834.18 LASP1 2081.18262394114 0.25353664096991 0.0740348392744757 3.42455853831129 0.000615799164096869 0.00849490484558848
ENSG00000002919.15 SNX11 630.57793127673 0.0459782434754682 0.105868331615849 0.434296477272386 0.664073161622284 0.820640102421514
ENSG00000002933.9 TMEM176A 1.2826428629398 -0.208251902266481 1.04854999265659 -0.198609416551381 0.842568287014726 NA
ENSG00000003056.8 M6PR 2075.74758354412 -0.298258970884233 0.0818583611419073 -3.64359812148181 0.000268853092542719 0.00454947228687621
ENSG00000003096.14 KLHL13 536.62234593866 0.158623735269079 0.107348560798392 1.47765125204599 0.139501148591557 0.348980620824275
ENSG00000003137.8 CYP26B1 287.854716803642 -0.0740116292474195 0.109594213807301 -0.675324240908867 0.499469786095141 0.712657283500843
ENSG00000003147.19 ICA1 535.667848254903 -0.143475821995906 0.0973591074296888 -1.47367643134487 0.140568731550252 0.350583904431043

CodePudding user response:

I don't really understand your question, as your code works fine. The only thing that was not working was "WebGL is not supported by your browser". I solved it forcing RStudio to use 'Desktop OpenGL'. In RStudio, I went to [Tools/Global Options.../Advanced] and choosing 'Desktop OpenGL' in Rendering engine.

Sample code:

library(tidyverse)
library(plotly)
library(ggplot2)



plot_total <-demo %>%
  as.data.frame() %>%
  ggplot(aes(log2(baseMean), log2FoldChange, color=padj < 0.01,
             text = paste(" log2(baseMean):", formatC(log2(baseMean), format = "e", digits = 2), "\n",
                          "log2FoldChange:", formatC(log2FoldChange, format = "e", digits = 2), "\n",
                          "Gene Name:", Gene, "\n",
                          "EnsGeneID:", ENSG)))  
  geom_point(data = . %>% filter(!padj<0.01), cex = 0.1, size=0.1)  
  geom_point(data = . %>% filter(is.na(padj)), cex = 0.1, size=0.1)  
  geom_point(data = . %>% filter(padj<0.01), cex = 1, size=1)  
  scale_color_manual(values=c("FALSE" = "#30B7BC", "TRUE" = "#DE653A", "NA" = "#B3B3B3"))  
  labs(title = "Treated vs. Control", x = "log2Expression")  
  theme_bw()  
  NULL

ggplotly(plot_total,
         tooltip = "text",
         type = 'scattergl') %>% 
  config(displaylogo = FALSE,
         modeBarButtonsToRemove = c('sendDataToCloud', 'select2d', 'lasso2d')) %>% 
  toWebGL()

Plot:

enter image description here

CodePudding user response:

The reason I was getting this error was because the df didn't contain NA under padj column for my particular dataset, so it was trying to filter what wasn't there. Adding an if/else statement fixed.

  • Related