msmbstyle icon indicating copy to clipboard operation
msmbstyle copied to clipboard

Margin figures text size

Open aridus opened this issue 5 years ago • 2 comments

Using the code below the figure that appears in the margin has an overly small font size. Do you know if Is this something that is a result of the data/plot, or the way that msmbstyle is doing the formatting?

# If tidyverse package not installed in RStudio, remove '#' from next line 
# The read_csv comamnd is part of the Tidyverse package readr
# install.packages("tidyverse")
library(tidyverse)  

# Read the data
data <- read_csv("country,number_of_species
                 Ecuador,96
                 Peru,87
                 Colombia,52
                 Bolivia,43
                 Venezuela,18
                 Argentina,7")

# Default ggplot figure style
# Note that the package is called ggplot2 but the command is 'ggplot'
ggplot(data, aes(country, number_of_species)) + # Specify x first, then y
  geom_bar(stat = "identity") + # Plot type
  theme_gray()

With grateful thanks.

aridus avatar May 17 '19 08:05 aridus

msmbstyle doesn't really influence the plots directly, it just puts in the margin whatever has been created by the code + knitr/markdown.

If you want more readable fonts plots from ggplot2 I see two option:

  1. Use the chunk options fig.width, fig.height, dpi to modify the size of the image that is produced. The font size tends to remain static relative to the image resolution, so a high-resolution plot ends up having really small text. Conversely, choose smaller than default options (which is probably fine for a relatively small margin picture) and the text appears larger. This would get you a square image with 400px & 400px:
```{r block1a, fig.margin = TRUE, fig.width=2, fig.height=2, dpi=100}
  1. Use the theme elements in ggplot2 to increase the font size. For your example, the theme_grey() function has an option to set all fonts to a single value e.g.
ggplot(data, aes(country, number_of_species)) + # Specify x first, then y
  geom_bar(stat = "identity") + # Plot type
  theme_gray(base_size = 20) 

You can of course combine both of these to produce smaller images to speed up loading, and also adjust the font size in ggplot.

grimbough avatar May 17 '19 09:05 grimbough

Ah, that inspired me, thank you. Using

fig.width = 2.5, fig.height = 2.5, dpi = 100, out.width = '100%',

works well, as the plot is scaled to the full width of the margin.

aridus avatar May 17 '19 10:05 aridus