-
Notifications
You must be signed in to change notification settings - Fork 17
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
Incorrect height for the richtext_grob object #6
Comments
Not sure what exactly is going on in your code. I'll have to ask you to try to figure out the specific problem in more detail. The moment you start pushing viewports inside viewports and then try to calculate the width or height of a grob, things can go wrong, in particular when you're rotating grobs and using different types of units along x and y. (For a possibly somewhat related problem, see here: r-lib/gtable#83) In my testing code, the width and height are calculated correctly. library(grid)
library(gridtext)
f <- function(rot) {
gb = richtext_grob(
month.name, x = ((3:14)+.5)/18,
box_gp = gpar(col = "red"),
rot = rot
)
grid.newpage()
grid.draw(
rectGrob(height = grobHeight(gb), width = grobWidth(gb))
)
grid.draw(gb)
}
f(30) f(45) f(90) f(-45) Created on 2020-02-04 by the reprex package (v0.3.0) |
The reason is I want to create a viewport with the same height as the richtext_grob. Let's say, when I implement the heatmap package, I want to create a viewport to put the column names in, thus, I need the height of this viewport to have the same height of the column names, also when they are rotated. Since the texts have absolute width and height, does it matter to calculate the size inside or outside a certain viewport? A strange thing is when I run your example code in a completely new R session, there is still empty space above and below the text, no matter I directly plot it in an interactive window or save into a PDF file. f(45) But, it seems it only happens on my laptop (Macbook). If I run the same code in other PC, it is fine: |
Also, I think the height of the grob is calculated from E.g. for following grob where text is rotated with 45 degrees: gb = richtext_grob(
month.name, x = ((3:14)+.5)/18,
box_gp = gpar(col = "red"),
rot = 45
) and I check gb$children[[1]]$yext On my laptop:
On another PC:
On Rstudio server:
|
I'm not sure why you get this wide range of different numbers for library(gridtext)
library(grid)
diff(range(c(10.89174, 41.19233, 19.40884, 49.70943)))
#> [1] 38.81769
diff(range(c(-11.15314, 19.14218, -19.14218, 11.15314)))
#> [1] 38.28436
diff(range(c(-19.42964, 10.91254, -10.91254, 19.42964)))
#> [1] 38.85928
png()
gb <- richtext_grob(
month.name, x = ((3:14)+.5)/18,
box_gp = gpar(col = "red"),
rot = 45
)
gb$children[[1]]$yext
#> [1] -19.09486 11.20573 -11.20573 19.09486
diff(range(gb$children[[1]]$yext))
#> [1] 38.18972
dev.off()
#> quartz_off_screen
#> 2
pdf()
gb <- richtext_grob(
month.name, x = ((3:14)+.5)/18,
box_gp = gpar(col = "red"),
rot = 45
)
gb$children[[1]]$yext
#> [1] -19.14218 11.15314 -11.15314 19.14218
diff(range(gb$children[[1]]$yext))
#> [1] 38.28437
dev.off()
#> quartz_off_screen
#> 2 Created on 2020-02-06 by the reprex package (v0.3.0) A couple of additional comments:
Thanks! |
Thanks for your reply. Now I tried to set On other machines, the three numbers are identical. library(gridtext)
library(grid)
gb = richtext_grob(month.name, rot = 90, align_widths = FALSE)
convertHeight(grobHeight(gb), "mm")
#> [1] 27.29755859375mm
gb = richtext_grob(month.name, rot = 90, align_widths = TRUE)
convertHeight(grobHeight(gb), "mm")
#> [1] 20.7077799479167mm
# if only September
gb = richtext_grob(month.name[9], rot = 90, align_widths = FALSE)
convertHeight(grobHeight(gb), "mm")
#> [1] 20.7077799479167mm Created on 2020-02-06 by the reprex package (v0.3.0) |
This is what I see (on OS X). Could you run the reprex with library(gridtext)
library(grid)
gb = richtext_grob(month.name, rot = 90, align_widths = FALSE)
convertHeight(grobHeight(gb), "mm")
#> [1] 20.7077799479167mm
gb = richtext_grob(month.name, rot = 90, align_widths = TRUE)
convertHeight(grobHeight(gb), "mm")
#> [1] 20.7077799479167mm
# if only September
gb = richtext_grob(month.name[9], rot = 90, align_widths = FALSE)
convertHeight(grobHeight(gb), "mm")
#> [1] 20.7077799479167mm Created on 2020-02-06 by the reprex package (v0.3.0) Session infodevtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 3.6.0 (2019-04-26)
#> os macOS Mojave 10.14.6
#> system x86_64, darwin15.6.0
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz America/Chicago
#> date 2020-02-06
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0)
#> backports 1.1.5 2019-10-02 [1] CRAN (R 3.6.0)
#> callr 3.4.1 2020-01-24 [1] CRAN (R 3.6.0)
#> cli 2.0.1 2020-01-08 [1] CRAN (R 3.6.0)
#> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0)
#> desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.0)
#> devtools 2.2.1 2019-09-24 [1] CRAN (R 3.6.0)
#> digest 0.6.23 2019-11-23 [1] CRAN (R 3.6.0)
#> ellipsis 0.3.0 2019-09-20 [1] CRAN (R 3.6.0)
#> evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.0)
#> fansi 0.4.1 2020-01-08 [1] CRAN (R 3.6.0)
#> fs 1.3.1 2019-05-06 [1] CRAN (R 3.6.0)
#> glue 1.3.1 2019-03-12 [1] CRAN (R 3.6.0)
#> gridtext * 0.1.0.9000 2020-02-03 [1] local
#> highr 0.8 2019-03-20 [1] CRAN (R 3.6.0)
#> htmltools 0.3.6 2017-04-28 [1] CRAN (R 3.6.0)
#> knitr 1.26 2019-11-12 [1] CRAN (R 3.6.0)
#> magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.0)
#> markdown 1.1 2019-08-07 [1] CRAN (R 3.6.0)
#> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.0)
#> pkgbuild 1.0.6 2019-10-09 [1] CRAN (R 3.6.0)
#> pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.6.0)
#> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 3.6.0)
#> processx 3.4.1 2019-07-18 [1] CRAN (R 3.6.0)
#> ps 1.3.0 2018-12-21 [1] CRAN (R 3.6.0)
#> R6 2.4.1 2019-11-12 [1] CRAN (R 3.6.0)
#> Rcpp 1.0.3 2019-11-08 [1] CRAN (R 3.6.0)
#> remotes 2.1.0 2019-06-24 [1] CRAN (R 3.6.0)
#> rlang 0.4.3 2020-01-24 [1] CRAN (R 3.6.0)
#> rmarkdown 1.15 2019-08-21 [1] CRAN (R 3.6.0)
#> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.0)
#> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0)
#> stringi 1.4.5 2020-01-11 [1] CRAN (R 3.6.0)
#> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.0)
#> testthat 2.3.1 2019-12-01 [1] CRAN (R 3.6.0)
#> usethis 1.5.1 2019-07-04 [1] CRAN (R 3.6.0)
#> withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.0)
#> xfun 0.12 2020-01-13 [1] CRAN (R 3.6.0)
#> xml2 1.2.2 2019-08-09 [1] CRAN (R 3.6.0)
#> yaml 2.2.0 2018-07-25 [1] CRAN (R 3.6.0)
#>
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library |
Hi, I updated the version of gridtext to 0.1.0.9000 (the github version. The previous one installed on my laptop was 0.1.0, from CRAN). Now everything is fine (also the in the plots). library(gridtext)
library(grid)
gb = richtext_grob(month.name, rot = 90, align_widths = FALSE)
convertHeight(grobHeight(gb), "mm")
#> [1] 20.7077799479167mm
gb = richtext_grob(month.name, rot = 90, align_widths = TRUE)
convertHeight(grobHeight(gb), "mm")
#> [1] 20.7077799479167mm
# if only September
gb = richtext_grob(month.name[9], rot = 90, align_widths = FALSE)
convertHeight(grobHeight(gb), "mm")
#> [1] 20.7077799479167mm Created on 2020-02-07 by the reprex package (v0.3.0) Session infodevtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 3.6.2 (2019-12-12)
#> os macOS Mojave 10.14.2
#> system x86_64, darwin15.6.0
#> ui X11
#> language (EN)
#> collate en_GB.UTF-8
#> ctype en_GB.UTF-8
#> tz Europe/Berlin
#> date 2020-02-07
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0)
#> backports 1.1.5 2019-10-02 [1] CRAN (R 3.6.0)
#> callr 3.4.0 2019-12-09 [1] CRAN (R 3.6.0)
#> cli 2.0.1 2020-01-08 [1] CRAN (R 3.6.0)
#> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0)
#> desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.0)
#> devtools 2.2.1 2019-09-24 [1] CRAN (R 3.6.1)
#> digest 0.6.23 2019-11-23 [1] CRAN (R 3.6.0)
#> ellipsis 0.3.0 2019-09-20 [1] CRAN (R 3.6.0)
#> evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.0)
#> fansi 0.4.1 2020-01-08 [1] CRAN (R 3.6.0)
#> fs 1.3.1 2019-05-06 [1] CRAN (R 3.6.0)
#> glue 1.3.1 2019-03-12 [1] CRAN (R 3.6.0)
#> gridtext * 0.1.0.9000 2020-02-07 [1] Github (wilkelab/gridtext@5cc4b40)
#> highr 0.8 2019-03-20 [1] CRAN (R 3.6.0)
#> htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.6.0)
#> knitr 1.26 2019-11-12 [1] CRAN (R 3.6.0)
#> magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.0)
#> markdown 1.1 2019-08-07 [1] CRAN (R 3.6.0)
#> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.0)
#> pkgbuild 1.0.6 2019-10-09 [1] CRAN (R 3.6.0)
#> pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.6.0)
#> prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.6.0)
#> processx 3.4.1 2019-07-18 [1] CRAN (R 3.6.0)
#> ps 1.3.0 2018-12-21 [1] CRAN (R 3.6.0)
#> R6 2.4.1 2019-11-12 [1] CRAN (R 3.6.0)
#> Rcpp 1.0.3 2019-11-08 [1] CRAN (R 3.6.0)
#> remotes 2.1.0 2019-06-24 [1] CRAN (R 3.6.0)
#> rlang 0.4.2 2019-11-23 [1] CRAN (R 3.6.0)
#> rmarkdown 2.0 2019-12-12 [1] CRAN (R 3.6.0)
#> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.0)
#> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0)
#> stringi 1.4.5 2020-01-11 [1] CRAN (R 3.6.0)
#> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.0)
#> testthat 2.3.1 2019-12-01 [1] CRAN (R 3.6.1)
#> usethis 1.5.1 2019-07-04 [1] CRAN (R 3.6.0)
#> withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.0)
#> xfun 0.11 2019-11-12 [1] CRAN (R 3.6.0)
#> xml2 1.2.2 2019-08-09 [1] CRAN (R 3.6.1)
#> yaml 2.2.0 2018-07-25 [1] CRAN (R 3.6.0)
#>
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library The f <- function(rot) {
gb = richtext_grob(
month.name, x = ((3:14)+.5)/18,
box_gp = gpar(col = "red"),
rot = rot
)
grid.newpage()
grid.draw(
rectGrob(height = grobHeight(gb), width = grobWidth(gb))
)
grid.draw(gb)
}
f(45) |
Now I re-installed the version 0.1.0 from CRAN, everything is also working. I don't know why, but it works. Thank you for your help! |
I'm glad things work now. 0.1.0 and 0.1.0.9000 are essentially the same. It would have been strange if one worked and the other didn't. |
Hi, thanks for developing this package for better configuring text under grid system! I am developing a package called ComplexHeatmap and I want to support gridtext with it. I found when
rot
is set to e.g. 30 inrichtext_grob()
function, the height calculated bygrobHeight()
is not identical to the height (or the width if the rotation is 90 degrees) of the longest text.I attached an example here. In the following plot, the height of the rectangles should be the same as the height of
September
, regardless of the rotation of the text. As a comparison, I also attached plots if usingtextGrob()
function and you can see the height of rectangles is exactly the same asSeptember
.I haven't tested
grobWidth()
onrichtext_grob
object, but I guess it should be similar.Thanks!
The text was updated successfully, but these errors were encountered: