Updating old ggplot2 code - Help!

67 views
Skip to first unread message

L Dyer

unread,
Dec 14, 2017, 2:06:10 PM12/14/17
to ggplot2
Hi All,

I've been trying to re-create some nice graphs that I found online using ggplot2. I assume that the code used by the original developer is now out of date, and I was wondering if somebody could help me update it, so that it stops running into errors. 

I'll outline the data.frame I am calling the plot on:

head(d,10)

             value          Rep time agent
D0-H2O-1  9.659073   1    0   H20
D0-ISO-1  9.925745   2    0   Iso
D1-H2O-1 11.137918   3    1   H20
D1-ISO-1 11.101013   4    1   Iso
D2-H2O-1 10.694017   5    2   H20
D2-ISO-1 10.421580   6    2   Iso
D4-H20-1  8.820280   7    4   H20
D4-ISO-1  9.274550   8    4   Iso
D7-H2O-1  8.757489   9    7   H20
D7-ISO-1  9.287158  10    7   Iso


Below is the code I am using to generate said frame and then plot:

d <- as.data.frame(e[p, ])
colnames(d) <- "value"
d$Rep <- md$Replicate
d$time <- md$Time
d$agent <- md$agent
gg <- ggplot(d, aes(time, value)) + geom_boxplot(aes(factor(time)), 
                                                                               outlier.shape = NA) + theme_bw() + scale_x_continuous(breaks = unique(d$time)) + 
                                                       geom_jitter(aes(color = factor(d$agent))) + geom_smooth() + 
                                                       labs(title = paste("1", "2", sep = "/"), x = "time (hours)", y = "RMA value") + scale_color_discrete(name = "treatment")



This seems to work, but does not produce an actual plot. Following this, I tried to call the plot...

> gg
Error: Discrete value supplied to continuous scale

And following this, I attempted to traceback...

>traceback(gg)

8: <environment>
   <environment>
   <environment>
Error in as.vector(x, "character") : 
  cannot coerce type 'environment' to vector of type 'character'


I'm not sure I quite understand the problem or what the error is trying to tell me. Initially, I thought the problem was with the "scale_x_continuous" argument due to the error message, however, removing this argument entirely does nothing to prevent either error from forming.

Any help is greatly appreciated, thanks for your time.


                                                                               



Thierry Onkelinx

unread,
Dec 15, 2017, 2:44:45 AM12/15/17
to L Dyer, ggplot2
Dear L,

A reproducible example would be helpful. Try to remove the geom's one
at the time to see which one generates the error. There is probably
one variable which is not numeric as it should be.

Best regards,

ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE
AND FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
thierry....@inbo.be
Kliniekstraat 25, B-1070 Brussel
www.inbo.be

///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no
more than asking him to perform a post-mortem examination: he may be
able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does
not ensure that a reasonable answer can be extracted from a given body
of data. ~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////


Van 14 tot en met 19 december 2017 verhuizen we uit onze vestiging in
Brussel naar het Herman Teirlinckgebouw op de site Thurn & Taxis.
Vanaf dan ben je welkom op het nieuwe adres: Havenlaan 88 bus 73, 1000 Brussel.

///////////////////////////////////////////////////////////////////////////////////////////
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

L Dyer

unread,
Dec 15, 2017, 7:06:16 AM12/15/17
to ggplot2
Hi Thierry,

Thanks for your response.

I have created a reproducible example below, after trying to remove each term of the plot separately. Unfortunately, I always end up with the same error:

x = c(9.659073,  9.925745, 11.137918, 11.101013, 10.694017, 10.421580,  8.820280,  9.274550,  8.757489,  9.287158,  9.924307, 10.282867, 11.178629, 11.091285,
      10.680091, 10.544983,  9.148373,  9.615934,  8.942005,  9.348149, 10.000511,  9.995393, 11.425498, 11.137202, 10.590028, 10.779298,  9.220715,  9.856640,
      9.505597, 9.495094, 10.318790, 10.539182, 12.173751, 12.196196, 11.633356, 11.568737,  9.672060, 10.257759, 9.461777,  9.619654)


e <- as.data.frame(x)
rownames(e) <- c("D0-H2O-1", "D0-ISO-1", "D1-H2O-1", "D1-ISO-1", "D2-H2O-1", "D2-ISO-1", "D4-H20-1", "D4-ISO-1", "D7-H2O-1", "D7-ISO-1", "D0-H2O-2", "D0-ISO-2", "D1-H2O-2",
                     "D1-ISO-2", "D2-H2O-2", "D2-ISO-2", "D4-H20-2", "D4-ISO-2", "D7-H2O-2", "D7-ISO-2", "D0-H2O-3", "D0-ISO-3", "D1-H2O-3", "D1-ISO-3", "D2-H2O-3", "D2-ISO-3",
                     "D4-H20-3", "D4-ISO-3", "D7-H2O-3", "D7-ISO-3", "D0-H2O-4", "D0-ISO-4", "D1-H2O-4", "D1-ISO-4", "D2-H2O-4", "D2-ISO-4", "D4-H20-4", "D4-ISO-4", "D7-H2O-4",
                     "D7-ISO-4")
e <- t(e)

rownames(e) <- "Irx5"



time <- c(rep(c(0,0,1,1,2,2,4,4,7,7),4))
md$Time <- time
replicate <- c(rep(c(1,2,3,4,5,6,7,8,9,10),4))
md$Replicate <- replicate
control <- c(rep(c(1,0),20))
md$Control <- control
iso <- c(rep(c(0,1),20))
md$Iso <- iso

p <- "Irx5"

d <- as.data.frame(e[p, ])
colnames(d) <- "value"
d$Rep <- md$Replicate
d$time <- md$Time
d$agent <- c(rep(c("H20","Iso"),10))

gg <- ggplot(d, aes(time, value)) + 
  geom_boxplot(aes(factor(time)), outlier.shape = NA) + theme_bw() + 
  geom_jitter(aes(color = factor(d$agent))) + 
  geom_smooth() + labs(title = paste("1","2", sep = "/"), x = "time (hours)", y = "RMA value") + 
  scale_color_discrete(name = "agent")

gg

>traceback(gg)

9: c(9.659073, 9.925745, 11.137918, 11.101013, 10.694017, 10.42158, 8.82028, 9.27455, 8.757489, 9.287158, 9.924307, 10.282867, 11.178629, 11.091285, 10.680091, 10.544983, 9.148373, 9.615934, 8.942005, 9.348149, 10.000511, 9.995393, 11.425498, 11.137202, 10.590028, 10.779298, 9.220715, 9.85664, 9.505597, 9.495094, 10.31879, 10.539182, 12.173751, 12.196196, 11.633356, 11.568737, 9.67206, 10.257759, 9.461777, 9.619654)
   c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
   c(0, 0, 1, 1, 2, 2, 4, 4, 7, 7, 0, 0, 1, 1, 2, 2, 4, 4, 7, 7, 0, 0, 1, 1, 2, 2, 4, 4, 7, 7, 0, 0, 1, 1, 2, 2, 4, 4, 7, 7)
   c("H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso", "H20", "Iso")
8: <environment>
   <environment>
   <environment>
Error in as.vector(x, "character") : 
  cannot coerce type 'environment' to vector of type 'character'

I hope that this helps, and thank you very much for your attention.

Laurence

Joyce Robbins

unread,
Dec 15, 2017, 7:13:38 AM12/15/17
to L Dyer, ggplot2
That code works for me:

Inline image 1

> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.6

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils    
[5] datasets  methods   base     

other attached packages:
[1] ggplot2_2.2.1

loaded via a namespace (and not attached):
 [1] labeling_0.3     colorspace_1.3-2
 [3] scales_0.5.0     lazyeval_0.2.1  
 [5] plyr_1.8.4       tools_3.3.2     
 [7] gtable_0.2.0     tibble_1.3.4    
 [9] yaml_2.1.15      Rcpp_0.12.14    
[11] grid_3.3.2       digest_0.6.12   
[13] rlang_0.1.4      munsell_0.4.3   



More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.

L Dyer

unread,
Dec 15, 2017, 7:22:29 AM12/15/17
to ggplot2
Hi Joyce,

Thanks for taking the time to respond. It's really helpful.

What should my next step be in trying to figure out the problem? Is there an obvious problem with my session info? I'm sorry about all the extra loaded packages... I've been messing around a lot lately!

> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS  10.13.1

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_2.2.1

loaded via a namespace (and not attached):
  [1] maSigPro_1.46.0            backports_1.1.1            Hmisc_4.0-3                plyr_1.8.4                 lazyeval_0.2.1            
  [6] splines_3.3.3              gamlss_5.0-6               BiocParallel_1.8.2         NOISeq_2.18.0              GenomeInfoDb_1.10.3       
 [11] digest_0.6.12              foreach_1.4.4              BiocInstaller_1.24.0       htmltools_0.3.6            viridis_0.4.0             
 [16] gdata_2.18.0               magrittr_1.5               checkmate_1.8.5            memoise_1.1.0              cluster_2.0.6             
 [21] gclus_1.3.1                limma_3.30.13              sitar_1.0.10               annotate_1.52.1            prettyunits_1.0.2         
 [26] colorspace_1.3-2           blob_1.1.0                 gamlss.data_5.0-0          dplyr_0.7.4                RCurl_1.95-4.8            
 [31] genefilter_1.56.0          lme4_1.1-14                bindr_0.1                  survival_2.41-3            iterators_1.0.9           
 [36] glue_1.2.0                 registry_0.5               gtable_0.2.0               zlibbioc_1.20.0            XVector_0.14.1            
 [41] Rsubread_1.24.2            MatrixModels_0.4-1         car_2.1-6                  kernlab_0.9-25             prabclus_2.2-6            
 [46] BiocGenerics_0.20.0        DEoptimR_1.0-8             SparseM_1.77               scales_0.5.0               mvtnorm_1.0-6             
 [51] DBI_0.7                    edgeR_3.16.5               Rcpp_0.12.14               progress_1.1.2             viridisLite_0.2.0         
 [56] xtable_1.8-2               htmlTable_1.11.0           foreign_0.8-69             bit_1.1-12                 mclust_5.4                
 [61] Formula_1.2-2              stats4_3.3.3               htmlwidgets_0.9            httr_1.3.1                 gplots_3.0.1              
 [66] RColorBrewer_1.1-2         fpc_2.1-10                 acepack_1.4.1              modeltools_0.2-21          pkgconfig_2.0.1           
 [71] XML_3.98-1.9               flexmix_2.3-14             nnet_7.3-12                locfit_1.5-9.1             labeling_0.3              
 [76] rlang_0.1.4                AnnotationDbi_1.36.2       munsell_0.4.3              tools_3.3.3                RSQLite_2.0               
 [81] devtools_1.13.4            evaluate_0.10.1            stringr_1.2.0              yaml_2.1.16                corrgram_1.12             
 [86] knitr_1.17                 bit64_0.9-7                robustbase_0.92-8          caTools_1.17.1             purrr_0.2.4               
 [91] dendextend_1.6.0           bindrcpp_0.2               nlme_3.1-131               whisker_0.3-2              quantreg_5.34             
 [96] venn_1.5                   biomaRt_2.30.0             pbkrtest_0.4-7             rstudioapi_0.7             curl_3.1                  
[101] tibble_1.3.4               geneplotter_1.52.0         stringi_1.1.6              lattice_0.20-35            trimcluster_0.1-2         
[106] Matrix_1.2-12              nloptr_1.0.4               data.table_1.10.4-3        bitops_1.0-6               seriation_1.2-2           
[111] GenomicRanges_1.26.4       R6_2.2.2                   latticeExtra_0.6-28        TSP_1.1-5                  KernSmooth_2.23-15        
[116] gridExtra_2.3              IRanges_2.8.2              codetools_0.2-15           MASS_7.3-47                gtools_3.5.0              
[121] assertthat_0.2.0           SummarizedExperiment_1.4.0 DESeq2_1.14.1              rprojroot_1.2              withr_2.1.0               
[126] S4Vectors_0.12.2           diptest_0.75-7             mgcv_1.8-22                parallel_3.3.3             grid_3.3.3                
[131] rpart_4.1-11               tidyr_0.7.2                class_7.3-14               minqa_1.2.4                rmarkdown_1.8             
[136] git2r_0.19.0               Biobase_2.34.0             gamlss.dist_5.0-4          base64enc_0.1-3   

Laurence

Thierry Onkelinx

unread,
Dec 15, 2017, 7:30:25 AM12/15/17
to L Dyer, ggplot2
Dear Laurence,

Your example is not reproducible. Try running the code in a vanilla R
session. You'll notice that the md object is missing.

Upgrading R and the packages to a recent version would be a good thing too.

Best regards,

ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE
AND FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
thierry....@inbo.be
Kliniekstraat 25, B-1070 Brussel
www.inbo.be

///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no
more than asking him to perform a post-mortem examination: he may be
able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does
not ensure that a reasonable answer can be extracted from a given body
of data. ~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////


Van 14 tot en met 19 december 2017 verhuizen we uit onze vestiging in
Brussel naar het Herman Teirlinckgebouw op de site Thurn & Taxis.
Vanaf dan ben je welkom op het nieuwe adres: Havenlaan 88 bus 73, 1000 Brussel.

///////////////////////////////////////////////////////////////////////////////////////////



Reply all
Reply to author
Forward
0 new messages