Rmarkdown Latex pdf output problem with figure scale

Hello everyone,

I use R and Rmarkdown to generate dynamic reports. I have a problem with one of my figure that I can not resolve:

The figure generated by ggplot2 is not scaled correctly in Rmarkdown. I can obtain good proportion of this figure without Rmarkdown..

My out.width chunk parameter is already 100%, so I don't know what to do to get proportions that I want..

The proportions that I want:

What I get in the PDF rendering:

lang: fr-FR
title: "test"
date: "`r format(Sys.time(), '%d/%m/%y')`"
        toc: yes
        number_sections: true
        toc_depth: 3
        fig_caption: yes
{r out.width = "100%"}
blank_theme <- theme_minimal()+
  axis.title.x = element_blank(),
  axis.title.y = element_blank(),
  panel.border = element_blank(),
  axis.ticks = element_blank(),
  plot.title=element_text(size=14, face="bold"),

data <- structure(list(fill_col = structure(c(2L, 2L, 2L, 6L, 6L, 6L, 4L,
4L, 4L, 3L, 3L, 3L, 1L, 1L, 1L, 5L, 5L, 5L), levels = c("Signalement",
"6&+", "Pré-suivi inactif", "Pré-suivi actif", "Suivi", "Post-suivi"
), class = "factor"), facet_col = structure(c(3L, 2L, 1L, 3L, 2L,
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L), levels = c("2022",
"2021", "2020"), class = "factor"), y_col = c(1L, 2L, 1L, 7L,
4L, 1L, 3L, 3L, 1L, 2L, 7L, 2L, 3L, 2L, 4L, 4L, 3L, 7L)), row.names = c(NA,
-18L), class = c("tbl_df", "tbl", "data.frame"))

data <- data %>%
    group_by(facet_col) %>%
    mutate(sum_for_facet_col = sum(y_col, na.rm = TRUE), .groups = "drops")

  ggplot(data, aes(x = "", y = y_col, fill = fill_col)) +
    geom_bar(stat = "identity", width = 1, position = position_fill()) +
    geom_text(aes(label = paste0(round(y_col/sum_for_facet_col *100, digits = 1),"%")),
    size = 4,
    colour = "white",
    check_overlap = TRUE, position = position_fill(vjust = 0.5)) +
    coord_polar(theta = "y")  +
    facet_wrap(~facet_col) +
    blank_theme +
    theme(axis.text.x=element_blank(), strip.text.x = element_text(size = 20))

Thank in advance for your help

Hi @Poumon, in Rmarkdown you could get the size plot control with fig.height, fig.width

Im use fig.height=6, fig.width=10. Add this in the chunk head.

The result was:

Thank you for your answer, it fit well :slight_smile:

The reason I did not use it is because my graphic are dynamically generated. There are multiples plots by chunk, and it is variable. I think your solution is the best for most of the case.

I found another for my specific case: reducing the margin between plots and putting the legend box to the top.

    plot +
    theme(axis.text.x=element_blank(), strip.text.x = element_text(size = 10),legend.box = 
"horizontal", legend.direction = "horizontal", legend.position = "top", panel.margin = unit(-1, "lines"))

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.