Skip to content

Discrete scale broken with negative limits #3918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dhyou21 opened this issue Mar 31, 2020 · 20 comments
Closed

Discrete scale broken with negative limits #3918

dhyou21 opened this issue Mar 31, 2020 · 20 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@dhyou21
Copy link

dhyou21 commented Mar 31, 2020

I recently reinstalled my R/Rstudio due to the issue with one of the packages, and during this process, I also (1) downloaded and installed R development tools and libraries (clang and gfortran) from https://cran.r-project.org/bin/macosx/tools/ (2) downloaded and installed gfortran compiler for R from https://github.com/fxcoudert/gfortran-for-macOS/releases/tag/6.3 (3) uninstalled Java/JDK 14 and installing Java/JDK 11 version; and (4) ran “R CMD javareconf” comman on the terminal. All of these actions were taken to resolve the error associated with one of the packages requiring Java connection.

However, during theses resolution steps, something else seemed to be messed up because the existing function/package/script started working inappropriately (it worked before I tried the resolution steps above). So the problem is (and this problem is SEPARATE from the package problem that I was trying to solve with the resolution steps mentioned above): I run this script that utilizes several packages including ggplot2, tibble, dplyr, etc. to generate my graphs. Just several days ago when I tried to generate these graphs, the script worked fine (see below picture, on the left labeled as a "good example").

image

But after all the resolution steps that I took as discussed above, the exact same script no longer works properly (see above picture, on the right side labeled as a "bad example" that I'm getting now). I used the exact same script, same packages to generate these graphs but now the graphs are being generated strangely…It seems like the tickmarks on the y axis are now not getting evenly spaced out throughout the whole y-axis and the problem starts at the stage where I create "Plot."

Does anyone have any idea what might have gone wrong during the resolution that could have damaged R (or its associated packages) to cause this problem? I want to go back to the ggplot2 that used to create that "good example"

The data that I used are basically table of different cell numbers in each well ("Number_of_Objects) in 384-well plate and I tried to depict the data using the heatmap approach as shown in the figure. The figure shows the collection of small rectangles corresponding to each well in 384-well plate and they are colored on gray gradient based on the cell density (darker, the greater density). The code that I used to plot this is:

library("tidyverse")
library("stringr")
library("ggplot2")
library("ggpubr")

#Table corresponds to the data table of 384-well plate that will be analyzed#

Plot=ggplot(Table, aes(x=Column, y=-Row)) + theme_classic()+
    scale_x_discrete(limits=c(1:24), name="") + 
    scale_y_discrete(limits=c(-1:-16), labels=LETTERS[1:16], name="")
  
  
  pShape=Plot + ggtitle("Number of non-border cells") +
    geom_tile(aes(fill=Number_of_Objects)) +
    scale_fill_gradient(low = "black", high = "gray80", name="")

I used the exactly the same script both times and both times the script runs without any error but generates completely different graphs. How can I fix the issue so that I can go back to the "good example" graph state?

During install/reinstall, some of those packages might have been updated as well. I checked the updates on all the packages that were installed and except for "tibble," which is a package that gets called along with those libraries when I load "tidyverse," everything was up-to-date. Here is the exact console output that I get when I call those libraries:

> library("tidyverse")
── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
✓ ggplot2 3.3.0     ✓ purrr   0.3.3
✓ tibble  2.1.3     ✓ dplyr   0.8.5
✓ tidyr   1.0.2     ✓ stringr 1.4.0
✓ readr   1.3.1     ✓ forcats 0.5.0
── Conflicts ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
> library("stringr")
> library("ggplot2")
> library("ggpubr")
Loading required package: magrittr

Attaching package: ‘magrittr’

The following object is masked from ‘package:purrr’:

    set_names

The following object is masked from ‘package:tidyr’:

    extract

I tried to create a reprex and the output is below. The example data is similar to the structure of my actual dataset - I created it using "datapasta" package's paste as tribble function. One thing to note is that on columns "row" "column" and "Number_of_Objects," there is a letter "L" after every number but it is not supposed to be there. The original sample data that I copied from Excel only contain numbers - and I don't know why "L" got added to every single number in the data. Anyhow...the thing that was strange with the reprex was that... in the rendered reprex, the image saving output was 7x5 whereas, when I actually ran the code on my R, the image was saved as 7x7 (see the last line, that says "saving 7x5 in image" - in my console, when the code was actually run, it says "Saving 7x7 in image")

library("tidyverse")
library("stringr")
library("ggplot2")
library("ggpubr")
#> Loading required package: magrittr
#> 
#> Attaching package: 'magrittr'
#> The following object is masked from 'package:purrr':
#> 
#>     set_names
#> The following object is masked from 'package:tidyr':
#> 
#>     extract

ExampleTable <- tibble::tribble(
                  ~Well, ~Row, ~Column, ~Number_of_Objects,
                  "A01",   1L,      1L,               532L,
                  "A02",   1L,      2L,               400L,
                  "A03",   1L,      3L,               875L,
                  "A04",   1L,      4L,               573L,
                  "A05",   1L,      5L,               970L,
                  "A06",   1L,      6L,               626L,
                  "A07",   1L,      7L,              1330L,
                  "A08",   1L,      8L,               623L,
                  "A09",   1L,      9L,               657L,
                  "A10",   1L,     10L,               964L,
                  "A11",   1L,     11L,               905L,
                  "A12",   1L,     12L,                80L,
                  "A13",   1L,     13L,               650L,
                  "A14",   1L,     14L,               250L,
                  "A15",   1L,     15L,               484L,
                  "A16",   1L,     16L,               493L,
                  "A17",   1L,     17L,               550L,
                  "A18",   1L,     18L,               401L,
                  "A19",   1L,     19L,               841L,
                  "A20",   1L,     20L,               410L,
                  "A21",   1L,     21L,              1166L,
                  "A22",   1L,     22L,                74L,
                  "A23",   1L,     23L,                54L,
                  "A24",   1L,     24L,               882L,
                  "B01",   2L,      1L,               113L,
                  "B02",   2L,      2L,                90L,
                  "B03",   2L,      3L,               682L,
                  "B04",   2L,      4L,               567L,
                  "B05",   2L,      5L,               949L,
                  "B06",   2L,      6L,               749L,
                  "B07",   2L,      7L,               771L,
                  "B08",   2L,      8L,               494L,
                  "B09",   2L,      9L,               806L,
                  "B10",   2L,     10L,               616L,
                  "B11",   2L,     11L,               865L,
                  "B12",   2L,     12L,                44L,
                  "B13",   2L,     13L,                65L,
                  "B14",   2L,     14L,               416L,
                  "B15",   2L,     15L,                51L,
                  "B16",   2L,     16L,              1279L,
                  "B17",   2L,     17L,               519L,
                  "B18",   2L,     18L,              1017L,
                  "B19",   2L,     19L,               763L,
                  "B20",   2L,     20L,               212L,
                  "B21",   2L,     21L,               763L,
                  "B22",   2L,     22L,               435L,
                  "B23",   2L,     23L,               532L,
                  "B24",   2L,     24L,               227L,
                  "C01",   3L,      1L,               714L,
                  "C02",   3L,      2L,               851L,
                  "C03",   3L,      3L,               530L,
                  "C04",   3L,      4L,              1296L,
                  "C05",   3L,      5L,               564L,
                  "C06",   3L,      6L,               979L,
                  "C07",   3L,      7L,               801L,
                  "C08",   3L,      8L,               680L,
                  "C09",   3L,      9L,               137L,
                  "C10",   3L,     10L,               655L,
                  "C11",   3L,     11L,               735L,
                  "C12",   3L,     12L,               855L,
                  "C13",   3L,     13L,              1374L,
                  "C14",   3L,     14L,              1380L,
                  "C15",   3L,     15L,               612L,
                  "C16",   3L,     16L,               475L,
                  "C17",   3L,     17L,              1241L,
                  "C18",   3L,     18L,               358L,
                  "C19",   3L,     19L,               731L,
                  "C20",   3L,     20L,               666L,
                  "C21",   3L,     21L,               943L,
                  "C22",   3L,     22L,              1056L,
                  "C23",   3L,     23L,               891L,
                  "C24",   3L,     24L,               738L,
                  "D01",   4L,      1L,               769L,
                  "D02",   4L,      2L,               556L,
                  "D03",   4L,      3L,               598L,
                  "D04",   4L,      4L,                95L,
                  "D05",   4L,      5L,               201L,
                  "D06",   4L,      6L,               912L,
                  "D07",   4L,      7L,               908L,
                  "D08",   4L,      8L,               865L,
                  "D09",   4L,      9L,               990L,
                  "D10",   4L,     10L,               859L,
                  "D11",   4L,     11L,              1439L,
                  "D12",   4L,     12L,               711L,
                  "D13",   4L,     13L,               812L,
                  "D14",   4L,     14L,               533L,
                  "D15",   4L,     15L,               572L,
                  "D16",   4L,     16L,              1056L,
                  "D17",   4L,     17L,                61L,
                  "D18",   4L,     18L,               293L,
                  "D19",   4L,     19L,              1071L,
                  "D20",   4L,     20L,               839L,
                  "D21",   4L,     21L,               830L,
                  "D22",   4L,     22L,               562L,
                  "D23",   4L,     23L,                75L,
                  "D24",   4L,     24L,                 5L,
                  "E01",   5L,      1L,               585L,
                  "E02",   5L,      2L,               751L,
                  "E03",   5L,      3L,               134L,
                  "E04",   5L,      4L,              1368L,
                  "E05",   5L,      5L,               991L,
                  "E06",   5L,      6L,               875L,
                  "E07",   5L,      7L,               617L,
                  "E08",   5L,      8L,               936L,
                  "E09",   5L,      9L,              1337L,
                  "E10",   5L,     10L,                78L,
                  "E11",   5L,     11L,               602L,
                  "E12",   5L,     12L,              1074L,
                  "E13",   5L,     13L,               940L,
                  "E14",   5L,     14L,               393L,
                  "E15",   5L,     15L,              1157L,
                  "E16",   5L,     16L,              1248L,
                  "E17",   5L,     17L,               881L,
                  "E18",   5L,     18L,               980L,
                  "E19",   5L,     19L,               675L,
                  "E20",   5L,     20L,              1114L,
                  "E21",   5L,     21L,               897L,
                  "E22",   5L,     22L,               650L,
                  "E23",   5L,     23L,               168L,
                  "E24",   5L,     24L,               271L,
                  "F01",   6L,      1L,               591L,
                  "F02",   6L,      2L,               257L,
                  "F03",   6L,      3L,               609L,
                  "F04",   6L,      4L,               539L,
                  "F05",   6L,      5L,              1015L,
                  "F06",   6L,      6L,               891L,
                  "F07",   6L,      7L,              1063L,
                  "F08",   6L,      8L,               972L,
                  "F09",   6L,      9L,               927L,
                  "F10",   6L,     10L,              1162L,
                  "F11",   6L,     11L,               762L,
                  "F12",   6L,     12L,               770L,
                  "F13",   6L,     13L,               706L,
                  "F14",   6L,     14L,               308L,
                  "F15",   6L,     15L,              1167L,
                  "F16",   6L,     16L,               750L,
                  "F17",   6L,     17L,               963L,
                  "F18",   6L,     18L,              1102L,
                  "F19",   6L,     19L,               407L,
                  "F20",   6L,     20L,               839L,
                  "F21",   6L,     21L,              1047L,
                  "F22",   6L,     22L,               815L,
                  "F23",   6L,     23L,               609L,
                  "F24",   6L,     24L,                65L,
                  "G01",   7L,      1L,               130L,
                  "G02",   7L,      2L,               435L,
                  "G03",   7L,      3L,               823L,
                  "G04",   7L,      4L,               291L,
                  "G05",   7L,      5L,              1278L,
                  "G06",   7L,      6L,               567L,
                  "G07",   7L,      7L,               124L,
                  "G08",   7L,      8L,               797L,
                  "G09",   7L,      9L,               950L,
                  "G10",   7L,     10L,              1095L,
                  "G11",   7L,     11L,               712L,
                  "G12",   7L,     12L,              1169L,
                  "G13",   7L,     13L,               689L,
                  "G14",   7L,     14L,               546L,
                  "G15",   7L,     15L,               602L,
                  "G16",   7L,     16L,               830L,
                  "G17",   7L,     17L,               123L,
                  "G18",   7L,     18L,              1222L,
                  "G19",   7L,     19L,               989L,
                  "G20",   7L,     20L,              1040L,
                  "G21",   7L,     21L,               426L,
                  "G22",   7L,     22L,               275L,
                  "G23",   7L,     23L,              1026L,
                  "G24",   7L,     24L,               670L,
                  "H01",   8L,      1L,               769L,
                  "H02",   8L,      2L,                86L,
                  "H03",   8L,      3L,              1263L,
                  "H04",   8L,      4L,               740L,
                  "H05",   8L,      5L,              1445L,
                  "H06",   8L,      6L,               607L,
                  "H07",   8L,      7L,               173L,
                  "H08",   8L,      8L,              1390L,
                  "H09",   8L,      9L,               749L,
                  "H10",   8L,     10L,               940L,
                  "H11",   8L,     11L,               669L,
                  "H12",   8L,     12L,               746L,
                  "H13",   8L,     13L,               787L,
                  "H14",   8L,     14L,               604L,
                  "H15",   8L,     15L,               912L,
                  "H16",   8L,     16L,               742L,
                  "H17",   8L,     17L,               119L,
                  "H18",   8L,     18L,               995L,
                  "H19",   8L,     19L,              1027L,
                  "H20",   8L,     20L,               745L,
                  "H21",   8L,     21L,               358L,
                  "H22",   8L,     22L,               119L,
                  "H23",   8L,     23L,               221L,
                  "H24",   8L,     24L,                15L,
                  "I01",   9L,      1L,               295L,
                  "I02",   9L,      2L,               682L,
                  "I03",   9L,      3L,               879L,
                  "I04",   9L,      4L,               847L,
                  "I05",   9L,      5L,               735L,
                  "I06",   9L,      6L,               760L,
                  "I07",   9L,      7L,               948L,
                  "I08",   9L,      8L,               736L,
                  "I09",   9L,      9L,              1225L,
                  "I10",   9L,     10L,              1333L,
                  "I11",   9L,     11L,               743L,
                  "I12",   9L,     12L,               713L,
                  "I13",   9L,     13L,               637L,
                  "I14",   9L,     14L,               652L,
                  "I15",   9L,     15L,               838L,
                  "I16",   9L,     16L,               783L,
                  "I17",   9L,     17L,               910L,
                  "I18",   9L,     18L,               766L,
                  "I19",   9L,     19L,               496L,
                  "I20",   9L,     20L,               992L,
                  "I21",   9L,     21L,              1291L,
                  "I22",   9L,     22L,              1475L,
                  "I23",   9L,     23L,               121L,
                  "I24",   9L,     24L,               484L,
                  "J01",  10L,      1L,               620L,
                  "J02",  10L,      2L,               110L,
                  "J03",  10L,      3L,               425L,
                  "J04",  10L,      4L,               889L,
                  "J05",  10L,      5L,               659L,
                  "J06",  10L,      6L,               745L,
                  "J07",  10L,      7L,               840L,
                  "J08",  10L,      8L,               721L,
                  "J09",  10L,      9L,               818L,
                  "J10",  10L,     10L,               601L,
                  "J11",  10L,     11L,               711L,
                  "J12",  10L,     12L,               772L,
                  "J13",  10L,     13L,               890L,
                  "J14",  10L,     14L,               771L,
                  "J15",  10L,     15L,               938L,
                  "J16",  10L,     16L,               518L,
                  "J17",  10L,     17L,               905L,
                  "J18",  10L,     18L,               784L,
                  "J19",  10L,     19L,               906L,
                  "J20",  10L,     20L,                59L,
                  "J21",  10L,     21L,               819L,
                  "J22",  10L,     22L,               544L,
                  "J23",  10L,     23L,               588L,
                  "J24",  10L,     24L,                29L,
                  "K01",  11L,      1L,              1008L,
                  "K02",  11L,      2L,               660L,
                  "K03",  11L,      3L,              1009L,
                  "K04",  11L,      4L,               581L,
                  "K05",  11L,      5L,               902L,
                  "K06",  11L,      6L,               639L,
                  "K07",  11L,      7L,              1322L,
                  "K08",  11L,      8L,               903L,
                  "K09",  11L,      9L,               807L,
                  "K10",  11L,     10L,              1150L,
                  "K11",  11L,     11L,               956L,
                  "K12",  11L,     12L,               879L,
                  "K13",  11L,     13L,               106L,
                  "K14",  11L,     14L,               624L,
                  "K15",  11L,     15L,               840L,
                  "K16",  11L,     16L,               840L,
                  "K17",  11L,     17L,               679L,
                  "K18",  11L,     18L,               711L,
                  "K19",  11L,     19L,               569L,
                  "K20",  11L,     20L,              1043L,
                  "K21",  11L,     21L,               751L,
                  "K22",  11L,     22L,               724L,
                  "K23",  11L,     23L,               646L,
                  "K24",  11L,     24L,              1000L,
                  "L01",  12L,      1L,               567L,
                  "L02",  12L,      2L,                22L,
                  "L03",  12L,      3L,               916L,
                  "L04",  12L,      4L,               538L,
                  "L05",  12L,      5L,               984L,
                  "L06",  12L,      6L,               663L,
                  "L07",  12L,      7L,               679L,
                  "L08",  12L,      8L,               488L,
                  "L09",  12L,      9L,               853L,
                  "L10",  12L,     10L,               855L,
                  "L11",  12L,     11L,               791L,
                  "L12",  12L,     12L,              1297L,
                  "L13",  12L,     13L,              1340L,
                  "L14",  12L,     14L,               741L,
                  "L15",  12L,     15L,              1075L,
                  "L16",  12L,     16L,               725L,
                  "L17",  12L,     17L,               735L,
                  "L18",  12L,     18L,               677L,
                  "L19",  12L,     19L,               460L,
                  "L20",  12L,     20L,               287L,
                  "L21",  12L,     21L,              1044L,
                  "L22",  12L,     22L,                27L,
                  "L23",  12L,     23L,               601L,
                  "L24",  12L,     24L,                28L,
                  "M01",  13L,      1L,               126L,
                  "M02",  13L,      2L,               784L,
                  "M03",  13L,      3L,               850L,
                  "M04",  13L,      4L,               580L,
                  "M05",  13L,      5L,              1085L,
                  "M06",  13L,      6L,               794L,
                  "M07",  13L,      7L,               879L,
                  "M08",  13L,      8L,               121L,
                  "M09",  13L,      9L,               901L,
                  "M10",  13L,     10L,               589L,
                  "M11",  13L,     11L,               695L,
                  "M12",  13L,     12L,               605L,
                  "M13",  13L,     13L,               691L,
                  "M14",  13L,     14L,               302L,
                  "M15",  13L,     15L,               878L,
                  "M16",  13L,     16L,               682L,
                  "M17",  13L,     17L,               666L,
                  "M18",  13L,     18L,              1191L,
                  "M19",  13L,     19L,               620L,
                  "M20",  13L,     20L,               634L,
                  "M21",  13L,     21L,               856L,
                  "M22",  13L,     22L,               484L,
                  "M23",  13L,     23L,                94L,
                  "M24",  13L,     24L,                24L,
                  "N01",  14L,      1L,               419L,
                  "N02",  14L,      2L,                27L,
                  "N03",  14L,      3L,               627L,
                  "N04",  14L,      4L,                37L,
                  "N05",  14L,      5L,               562L,
                  "N06",  14L,      6L,               434L,
                  "N07",  14L,      7L,               593L,
                  "N08",  14L,      8L,               888L,
                  "N09",  14L,      9L,              1284L,
                  "N10",  14L,     10L,               685L,
                  "N11",  14L,     11L,               643L,
                  "N12",  14L,     12L,               485L,
                  "N13",  14L,     13L,              1001L,
                  "N14",  14L,     14L,               460L,
                  "N15",  14L,     15L,               651L,
                  "N16",  14L,     16L,               679L,
                  "N17",  14L,     17L,               757L,
                  "N18",  14L,     18L,               172L,
                  "N19",  14L,     19L,               598L,
                  "N20",  14L,     20L,                94L,
                  "N21",  14L,     21L,               639L,
                  "N22",  14L,     22L,                31L,
                  "N23",  14L,     23L,               798L,
                  "N24",  14L,     24L,                 6L,
                  "O01",  15L,      1L,               921L,
                  "O02",  15L,      2L,                37L,
                  "O03",  15L,      3L,               597L,
                  "O04",  15L,      4L,               218L,
                  "O05",  15L,      5L,                68L,
                  "O06",  15L,      6L,               700L,
                  "O07",  15L,      7L,               711L,
                  "O08",  15L,      8L,               462L,
                  "O09",  15L,      9L,               885L,
                  "O10",  15L,     10L,               569L,
                  "O11",  15L,     11L,               804L,
                  "O12",  15L,     12L,               731L,
                  "O13",  15L,     13L,               404L,
                  "O14",  15L,     14L,               321L,
                  "O15",  15L,     15L,               903L,
                  "O16",  15L,     16L,               618L,
                  "O17",  15L,     17L,               798L,
                  "O18",  15L,     18L,              1133L,
                  "O19",  15L,     19L,               735L,
                  "O20",  15L,     20L,               346L,
                  "O21",  15L,     21L,               629L,
                  "O22",  15L,     22L,                14L,
                  "O23",  15L,     23L,               255L,
                  "O24",  15L,     24L,                27L,
                  "P01",  16L,      1L,               364L,
                  "P02",  16L,      2L,                27L,
                  "P03",  16L,      3L,               939L,
                  "P04",  16L,      4L,                31L,
                  "P05",  16L,      5L,               610L,
                  "P06",  16L,      6L,                27L,
                  "P07",  16L,      7L,               955L,
                  "P08",  16L,      8L,                28L,
                  "P09",  16L,      9L,               554L,
                  "P10",  16L,     10L,                20L,
                  "P11",  16L,     11L,              1195L,
                  "P12",  16L,     12L,                41L,
                  "P13",  16L,     13L,               611L,
                  "P14",  16L,     14L,                56L,
                  "P15",  16L,     15L,              1142L,
                  "P16",  16L,     16L,                28L,
                  "P17",  16L,     17L,               795L,
                  "P18",  16L,     18L,                28L,
                  "P19",  16L,     19L,               563L,
                  "P20",  16L,     20L,                33L,
                  "P21",  16L,     21L,               693L,
                  "P22",  16L,     22L,                12L,
                  "P23",  16L,     23L,               554L,
                  "P24",  16L,     24L,                20L
                  )


Plot=ggplot(ExampleTable, aes(x=Column, y=-Row)) + theme_classic()+
  scale_x_discrete(limits=c(1:24), name="") + 
  scale_y_discrete(limits=c(-1:-16), labels=LETTERS[1:16], name="")

ggsave("/Users/youd2/Desktop/testplot1.png", Plot)
#> Saving 7 x 5 in image

Also, after tracking down, I realized that creating the "Plot" was indeed the step that was getting messed up? I ran the script below alone, saved it "Plot" as .png and compared it between the working R and my malfunctioning R.

Plot=ggplot(ExampleTable, aes(x=Column, y=-Row)) + theme_classic()+
  scale_x_discrete(limits=c(1:24), name="") + 
  scale_y_discrete(limits=c(-1:-16), labels=LETTERS[1:16], name="")

Here are the images that I got. On the left is the good example, and on the right is the one that gets generated off my R.

image|690x390
image

Not sure if these details provide more clues to what might have happened.

If this is related to the package update, with breaking changes... the good example was generated from Macbook with the following versions of the packages:

ggplot2 3.2.1
tibble 2.1.3
tidyr 1.0.2
readr 1.3.1
tidyverse 1.3.0
purrr 0.3.3.
dplyr 0.8.4
stringr 1.4.0
forcats 0.5.0

R version 3.6.2
R Studio up-to-date

Not sure if this information will be helpful but wanted to at least provide the metrics so that you know where the "good example" is coming from. Again, my malfunctioning machine also used to perform well exactly as this Macbook which generated the "good example."

I understand that the problem can be anything here. If it is indeed the error due to installing different compilers, do I have to uninstall those as well (how can I figure this out)? I thought that the compilers will also be uninstalled when I uninstall the R but is that not the case? Do I have to go through uninstall/reinstall of Java as well? Can "R CMD javareconf" mess up the defaults on ggplot2? What are some commands that I can check on terminal to find a potential bug that I'm having with ggplot2? If this is related to the package update error, what could solve the problem? I already tried uninstalling/reinstalling all the packages used in this script and that didn’t solve the problem. If the issue is not particular to the version of the package, and from the error on my computer... due to incomplete update of packages, etc. how should I solve the problem?

I wanted to bring this up as an issue in case this is a bug inherent to ggplot2. Thank you!

@clauswilke
Copy link
Member

I can confirm this issue. Minimal reprex:

library(ggplot2)

df <- data.frame(x = 1:14, y = -2:-15)

ggplot(df, aes(x, y)) +
  scale_y_discrete(limits=c(-1:-16), labels=LETTERS[1:16])

Created on 2020-03-31 by the reprex package (v0.3.0)

@paleolimbot Any idea what might be going on here? The problem seems to be related to negative limits in scale_y_discrete().

@thomasp85
Copy link
Member

This is a bug in the new axis implantation (I think). It will get fixed in ggplot2

@clauswilke clauswilke changed the title ggplot2 behaving weird suddenly after some tweaks that I did with R Discrete scale broken with negative limits Mar 31, 2020
@clauswilke clauswilke added the bug an unexpected problem or unintended behavior label Mar 31, 2020
@clauswilke clauswilke added this to the ggplot2 3.3.1 milestone Mar 31, 2020
@paleolimbot
Copy link
Member

I'll take a look today!

@paleolimbot
Copy link
Member

For reference, here's what the reprex looked like in 3.2.0. It still looks wrong to me (although better)

library(ggplot2)

df <- data.frame(x = 1:14, y = -2:-15)

ggplot(df, aes(x, y)) +
  scale_y_discrete(limits = c(-1:-16), labels = LETTERS[1:16])

I assume we want it to look like this?

@clauswilke
Copy link
Member

clauswilke commented Mar 31, 2020

Dewey, positive limits look like this:

library(ggplot2)

df <- data.frame(x = 1:14, y = 2:15)

ggplot(df, aes(x, y)) +
  scale_y_discrete(limits = c(1:16), labels = LETTERS[1:16])

Created on 2020-03-31 by the reprex package (v0.3.0)

I would expect negative limits to go the other way, as they have the opposite sort order.

@dhyou21
Copy link
Author

dhyou21 commented Mar 31, 2020

Hi, all. Yes with the negative limits, the sort order is the opposite. The "good example" that I shared with my post earlier is using exactly the same script (with the negative limits) but it's on ggplot2 3.2.1. ver? And that version created that good example which look fine and not really compressed like what I see with my "bad example." Not sure if this is truly a version problem but that is the only thing that I can think of. Would compiler installation or java version possibly cause the conflict with ggplot2?

@dhyou21
Copy link
Author

dhyou21 commented Mar 31, 2020

One addition:

I tried the reprex on my malfunctioning R by (1) uninstalling the ggplot 3.3.0 version; and (2) re-installing ggplot 3.2.1 (downloaded from the archive), and tried the negative limits but still return the same result. I tried the whole process on a new windows computer (to see if change in the operating system makes any difference) - where I freshly installed R/Rstudio and then installed ggplot2 package. I first tried 3.3.0 version which resulted in the same error - then I uninstalled 3.3.0 version and installed 3.2.1 version from the archive - but still got the same error. Interestingly, the Macbook that functions well has R version 3.6.2 and ggplot 3.2.1. - and with this combination, the script works well. Not sure if by upgrading ggplot in that Macbook to 3.3.0 version would return an error but I think that there maybe a compatibility issue between ggplot2 package and the new R version (3.6.3).

@paleolimbot
Copy link
Member

Yes, it's the new expansion code that's in 3.3.0. I think it requires special handling of integer limits...hang tight.

@paleolimbot
Copy link
Member

I have a PR open for this, but out of curiosity, isn't the following more realistic usage?

library(ggplot2)

df <- data.frame(x = 1:14, y = -2:-15)

ggplot(df, aes(x, y)) +
  scale_y_continuous(breaks = -1:-16, labels = LETTERS[1:16])

Created on 2020-03-31 by the reprex package (v0.3.0)

@paleolimbot
Copy link
Member

Thinking more about this, I think it would be more correct to error if somebody attempts to pass continuous limits to a discrete scale: scale_y_discrete(limits = factor(-1:-16)) has meaning, but scale_y_discrete(limits = -1:-16) doesn't (I don't think?).

@dhyou21
Copy link
Author

dhyou21 commented Mar 31, 2020

Thanks, I tried "continuous" instead of "discrete" and that seems to work fine. I think I can edit the code in the way you suggested. The original developer of that script (who is my research collaborator) probably tried discrete because what she wanted to do was to create that 384 "grid" where we can layout our heatmap (so probably thought of discrete rather than continuous?) - and probably used negative values to allow the proper labeling (A on the top and P on the bottom). The code that follows the "Plot" is:

pShape=Plot + ggtitle("Number of non-border cells") +
  geom_tile(aes(fill=Number_of_Objects)) +
  scale_fill_gradient(low = "black", high = "gray80", name="") 

So this creates a heatmap with individual rectangles colored on varying gray colors depending on the "Number_of_Objects."

But with the continuous scale, the heatmap also seems to be generated fine so I think I can go to that route. I will share if I notice any problem while I use the "scale_y_continuous"

@clauswilke
Copy link
Member

Thinking more about this, I think it would be more correct to error if somebody attempts to pass continuous limits to a discrete scale: scale_y_discrete(limits = factor(-1:-16)) has meaning, but scale_y_discrete(limits = -1:-16) doesn't (I don't think?).

I'm always of the opinion that if there's a reasonable interpretation of the input possible then we should go that route rather than erroring out. Do you really want to distinguish between limits = -1:-16, which wouldn't have meaning, and limits = -1L:-16L, which would have?

A different way to think about it: Shouldn't a discrete scale just take any limit values given and treat them as the distinct levels of a factor?

@paleolimbot
Copy link
Member

Sure, but what would you expect scale_y_discrete(limits = c(1, 2, 10)) to do? Should those limits be equally spaced? With the PR, it looks like this:

library(ggplot2)

df <- data.frame(x = 1:10, y = 1:10)
ggplot(df, aes(x, y)) +
  scale_y_discrete(limits = c(1, 2, 10), labels = c("A", "B", "C"))

Created on 2020-04-01 by the reprex package (v0.3.0)

(is.discrete() returns FALSE for both numerics and integers...both train the continuous range of a discrete position scale)

@clauswilke
Copy link
Member

I would expect them to be equally spaced, yes. In fact, I think the scale names are a bit confusing. The defining characteristic is not discrete/continuous, it's categorical/numerical or qualitative/quantitative, I believe.

Having said that, I don't know whether treating the limits of scale_*_discrete() as strictly categorical would break any long-standing ggplot2 assumptions. Maybe @hadley knows?

@paleolimbot
Copy link
Member

I stuck abort() in a few places and couldn't get any tests to fail, but maybe it's best just to restore the previous behaviour and add a warning("Did you mean scale_*_continuous()?")?

@hadley
Copy link
Member

hadley commented Apr 1, 2020

I don't remember for sure, but the complexity comes with something geom_jitter() with a categorical axis — categorical axes must also have implicit continuous behaviour to support things with non-integer widths (jittering, boxplots, bars, ...)

@clauswilke
Copy link
Member

Yes, categorical axes definitely need to be able to handle numerical values, e.g. 3.5 as half-way between 3 and 4. I think the question we're discussion here is different. Should a categorical scale ever take the limit values into account when determining the spacing between categories? My sense would be that it should not. But maybe these two things are not clearly separated in the code and that creates the issues?

@paleolimbot
Copy link
Member

The behaviour of the continuous/discrete ranges is pretty well tested. I think we just never considered what would happen if somebody typed scale_x_discrete(limits = c(1, 2, 8)). My sense is that if they did, they probably did it by accident.

@clauswilke
Copy link
Member

Dewey, I think we mostly agree. The main difference is our interpretation of what was meant to happen when somebody types scale_x_discrete(limits = c(1, 2, 8)). Your interpretation is they meant scale_x_continuous(limits = c(1, 2, 8)). My interpretation is they meant scale_x_discrete(limits = factor(c(1, 2, 8))). I'm fine with issuing a warning or abort, since the input is not clear, but I wouldn't want to suggest that people switch to the continuous scale. I'd rather suggest that people convert their limits input into a factor. Alternatively, maybe we could suggest both?

@paleolimbot
Copy link
Member

We're 100% in agreement! I'll work something up this eve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

5 participants