Search code examples
rggplot2

When scaling the plot of ggplot2 the textsize of geom_text() stays the same instead of scaling too


When scaling my plot, the text size stays the same instead of upsizing or downsizing (with the plot). As you can see in the screenshot on the left side the text left box 1 has plenty space in the box and on the right side text left box 1 is barely fitting.

So is there a possibility to make the text in some kind of scalable?

Any hints are appreciated - so thanks in advance for your support.

Screenshot: enter image description here

My code:

df=data.frame(
    xmin = 0,
    xmax = 7.5,
    ymin = 0,
    ymax = 2.5,
    radius = 0.1
)

plot = ggplot(df) +
        # testlabel box
        geom_rounded_rect(aes(xmin = xmin[1], xmax = xmax[1], ymin = ymin[1], ymax= ymax[1], radius = radius[1]), fill="lightgray", color="black", linewidth=1.5) +
        geom_text(aes(x=xmin[1]+0.25, y=ymax[1]-0.25),label="testlabel", size= 4, hjust = 0, vjust = 1) +
        # left box 1
        geom_rounded_rect(aes(xmin = xmin[1]-4.5, xmax = xmin[1]-0.5, ymin = ymin[1]-1.5, ymax= ymin[1], radius = radius[1]), fill="lightgray", color="black", linewidth=1.5) +
        geom_text(aes(x=xmin[1]-4.25, y=ymin[1]-0.25),label="left box 1", size= 4, hjust = 0, vjust = 1) +
        # left box 2
        geom_rounded_rect(aes(xmin = xmin[1]-4.5, xmax = xmin[1]-0.5, ymin = ymin[1]-3.25, ymax= ymin[1]-1.75, radius = radius[1]), fill="lightgray", color="black", linewidth=1.5) +
        geom_text(aes(x=xmin[1]-4.25, y=ymin[1]-2),label="left box 2", size= 4, hjust = 0, vjust = 1) +
        # theme stuff
        xlim(-10,10) + ylim(-10, 10) +
        xlab(NULL) +  
        ylab(NULL) +
        coord_fixed(ratio = 1)

# plotting
X11(width = 7, height = 7)
print(plot)
X11(width = 4, height = 4)
print(plot)

Solution

  • You can write a function that creates the plots and hand over a scale_factor which controls the text-size in geom_text.

    out

    Code

    library(ggplot2)
    library(grid)
    
    df = data.frame(
      xmin = 0,
      xmax = 7.5,
      ymin = 0,
      ymax = 2.5,
      radius = 0.1
    )
    
    # Function to create plot with dynamic text size
    create_plot <- function(base_size = 4, scale_factor = 1) {
      ggplot(df) +
        # testlabel box
        geom_rect(aes(xmin = xmin[1], xmax = xmax[1], ymin = ymin[1], ymax= ymax[1]), 
                  fill="lightgray", color="black", linewidth=1.5) +
        geom_text(aes(x=xmin[1]+0.25, y=ymax[1]-0.25), label="testlabel", 
                  size= base_size * scale_factor, hjust = 0, vjust = 1) +
        # left box 1
        geom_rect(aes(xmin = xmin[1]-4.5, xmax = xmin[1]-0.5, 
                      ymin = ymin[1]-1.5, ymax= ymin[1]), 
                  fill="lightgray", color="black", linewidth=1.5) +
        geom_text(aes(x=xmin[1]-4.25, y=ymin[1]-0.25), label="left box 1", 
                  size= base_size * scale_factor, hjust = 0, vjust = 1) +
        # left box 2
        geom_rect(aes(xmin = xmin[1]-4.5, xmax = xmin[1]-0.5, 
                      ymin = ymin[1]-3.25, ymax= ymin[1]-1.75), 
                  fill="lightgray", color="black", linewidth=1.5) +
        geom_text(aes(x=xmin[1]-4.25, y=ymin[1]-2), label="left box 2", 
                  size= base_size * scale_factor, hjust = 0, vjust = 1) +
        # theme stuff
        xlim(-10,10) + ylim(-10, 10) +
        xlab(NULL) +  
        ylab(NULL) +
        coord_fixed(ratio = 1)
    }
    b <- 7
    s <- 4
    # Plotting with different sizes
    X11(width = b, height = b)
    print(create_plot(scale_factor = 1))
    
    X11(width = s, height = s)
    print(create_plot(scale_factor = s/b))