Grid_plot + tikzDevice + general legend with latex mark up

I tried to follow this vignette on how to create a common legend for several ggplot2. These examples work fine as they are, but in my case I use tikzDevice to export the environment tikzpicture. The main problem is that the widths of the legend keys are not fixed correctly grid_plot.

I came up with a minimal R code that reproduces the problem:

require(ggplot2)
require(grid)
require(gridExtra)
require(cowplot)
require(tikzDevice)

tikz(file = "./tmp.tex", width = 5.6, height = 2.2, standAlone = T )
mpg2 <- mpg

mpg2$cyl = as.factor(mpg2$cyl)
levels(mpg2$cyl) <- c("\\textbf{\\textsc{four}}",
                      "\\textbf{\\textsc{five}}",
                      "\\textbf{\\textsc{six}}",
                      "\\textbf{\\textsc{seven}}",
                      "\\textbf{\\textsc{eight}}")

plot.mpg <- ggplot(mpg2, aes(x=cty, colour=cyl, y = hwy)) + 
     geom_point() +
     theme(legend.position='none')

legend <- get_legend(plot.mpg + theme(legend.position = "top"))

print(plot_grid(legend, 
                 plot.mpg, nrow=2, ncol=1,align='h', 
                 rel_heights = c(.1, 1)))

dev.off()

The generated PDF file (after compiling tmp.tex) is as follows:

enter image description here

As we can see, the first legend key (four) is only partially displayed, and the legend key (eight) is completely invisible. I tried to change the width of the command to tikzno avail.

, , , grid_plot , . , , mpg2 $cyl :

levels(mpg2$cyl) <- c("four",
                      "five",
                      "six",
                      "seven",
                      "eight")

:

enter image description here

, , . , .

- , , ?

, sessionInfo():

R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X Yosemite 10.10.5

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] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] tikzDevice_0.10-1 dplyr_0.5.0       gdata_2.17.0      cowplot_0.7.0    
[5] gridExtra_2.2.1   ggplot2_2.2.0    

loaded via a namespace (and not attached):
 [1] gtools_3.5.0       colorspace_1.2-6   DBI_0.5            RColorBrewer_1.1-2
 [5] plyr_1.8.4         munsell_0.4.3      gtable_0.2.0       labeling_0.3      
 [9] Rcpp_0.12.6        scales_0.4.1       filehash_2.3       digest_0.6.10     
[13] tools_3.3.2        magrittr_1.5       lazyeval_0.2.0     tibble_1.1        
[17] assertthat_0.1     R6_2.1.3         

.

+4
2

ggplot2, , , , TeX. , , (.. ) . gtable, getLatexStrWidth. . , .

require(ggplot2)
require(grid)
require(gridExtra)
require(tikzDevice)

setTikzDefaults(overwrite = TRUE)
preamble <- options("tikzLatexPackages") 
options("tikzLatexPackages" = c(preamble$tikzLatexPackages, "\\usepackage{bold-extra}"))

tikz(file = "./tmp.tex", width = 5.6, height = 2.2, standAlone = TRUE )
mpg2 <- mpg

mpg2$cyl = as.factor(mpg2$cyl)

levels(mpg2$cyl) <- c("\\textbf{\\textsc{four}}",
                      "\\textbf{\\textsc{five}}",
                      "\\textbf{\\textsc{six}}",
                      "\\textbf{\\textsc{seven}}",
                      "\\textbf{\\textsc{eight}}")

p <- ggplot(mpg2, aes(x=cty, colour=cyl, y = hwy)) + 
  geom_point() +
  theme(legend.position='none')



leg <- cowplot::get_legend(p + theme(legend.position = "top"))

ids <- grep("label",leg$grobs[[1]]$layout$name)
pos <- leg$grobs[[1]]$layout$l[ids]
wl <- sapply(leg$grobs[[1]][["grobs"]][ids], function(g) getLatexStrWidth(g[["label"]]))

leg$grobs[[1]][["widths"]][pos] <- unit(wl, "pt")

grid.arrange(p, top=leg)

dev.off()

enter image description here

+4

, , tex R ,

levels(mpg2$cyl) <- c("$four$",
                      "$five$",
                      "$six$",
                      "$seven$",
                      "$eight$")

[...]

tmp <- readLines("tmp.tex")
gs <- gsub("\\$(four|five|six|seven|eight)\\$", "\\\\textsc{\\1}", tmp, perl=TRUE)
cat(paste(gs, collapse="\n"), file="tmp2.tex")

enter image description here

+1

Source: https://habr.com/ru/post/1674344/


All Articles