davidgohel / ReporteRs

Note that ReporteRs has been removed from CRAN the 16th of July 2018 and is not maintained anymore. please migrate to officer.
245 stars 44 forks source link

Alignment of the legend entries #186

Closed danielsjf closed 6 years ago

danielsjf commented 7 years ago

The alignment of the legend entries seems to be unequal when I export a ggplot to powerpoint.

library(ggplot2)
library(ReporteRs)

n <- 11
name_element <- character(n)
for(i in 1:n){
  name_element[i] <- paste0(letters[round(runif(round(runif(1, 1, n)),1,26))], collapse = '')
}

p <- ggplot(data.frame(Name = name_element, val = runif(n)), aes(x = Name, y = val, fill = Name)) + 
  geom_col() +
  theme(legend.position="bottom")

file_name <- paste0(tempfile(),'.pptx')
myplot <- function(pl=p) print(pl)
doc <- pptx()
doc <- addSlide(doc, slide.layout = "Blank") 
doc <- addPlot(doc, myplot, vector.graphic = TRUE,
                          offx = 0.1*dim(doc)$slide.dim["width"], offy = 0.05*dim(doc)$slide.dim["height"], 
                          width = 0.8*dim(doc)$slide.dim["width"], height = 0.9*dim(doc)$slide.dim["height"])
writeDoc(doc, file_name)

cat(file_name)

The result is the following legend (in the powerpoint). image

However, when I rescale the image (e.g. make it more narrow), it becomes obvious that the legend entries are wrongly aligned: image

This is a powerpoint issue since the anchors in a group are in the middle of the shape. However, this could be fixed by making the boxes of the legend entries equal in size (e.g. the same width as the widest object).

davidgohel commented 7 years ago

Hi

Are you saying you want to resize the graph manually?

That's not a internal MS graphic, it's a composite graphic made of many elements that can't be adjusted (easily) manually. Maybe you would prefer to use a emf output? It let you resize the elements (but not edit them).

danielsjf commented 7 years ago

@davidgohel We use your package as a replacement for the png files that we used before. The R output is more or less made for the slide. However, often we change some last-minute text and in those cases we might want to slightly resize the charts.

So far this worked perfectly, but now I have some charts in which the names of the legend entries are really different in size (as in my example). You package creates a grouped combo of shapes in powerpoint. When you resize a group like this, each element is resized, but also their anchor in the group. Unfortunately this anchor is always in the center of the shape in powerpoint.

For my case, each legend entry has a different anchor. They are not aligned. You don't see this until you scale the image.

I tried to show the problem below by marking the hypothetical anchors. image

When you now make the figure more narrow (reducing the width while leaving the height untouched), the red dots will shift according to the group position, while the width of the legend entries remains the same (the text has the same font size).

A fix would be to increase the size of the legend text boxes so that they're all equal.

davidgohel commented 7 years ago

[...]A fix would be to increase the size of the legend text boxes so that they're all equal.[...]

I am 96.34% sure this is not a valid solution. This is coded in package rvg, it's a graphical device implemented for R, we can't split behavior for legend text boxes and other text boxes. I can't spend time on that these next days but if you want to try, this is the line you want to modify: https://github.com/davidgohel/rvg/blob/master/src/pptx.cpp#L392

I will have a look later to see if it is possible. there may be an option to adjust automatically text size within boxes...