3

This may seem like a simple question, but I couldn't get any information on the sites I looked at.

In ggplot2 I use text size 7. For some reason I needed to use R's native graphics, but I don't know how to scale the text size to match ggplot2.

The text size of my axes is:

theme(axis.title = element_text(face="bold", size = 7)

Within plot(), what value of cex.axis should I put?

Daniel Valencia C.
  • 2,159
  • 2
  • 19
  • 38
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. How exactly are you saving/combing plots to compare? – MrFlick May 03 '23 at 13:19

2 Answers2

2

We could get the default ggplot using (theme_bw()$text$size) -> 11

Font size 7 is then 64% smaller then the default -> 7/11 (in my first answer I used 12).

To scale the font size to match your ggplot2 font size of 7, we could a scaling factor of 0.64

dev.off()
barplot(data$mpg, names.arg = data$cyl,
        col = "grey", border = "black",
        main = "Title", xlab = "X Axis", ylab = "Y Axis", 
        cex.names = 0.64,
        cex.lab = 0.64,
        cex.main = 0.64,
        yaxt="n",
        xaxt="n")
axis(2, cex.axis = 0.64)
axis(1, cex.axis = 0.64)

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Interesting, how did you come up with 0.58 to match with ggplot text size 7? And par should be called before barplot? – zx8754 May 03 '23 at 13:29
  • Please see my update. Now it should be clear. The default was not 12, it was 11. But I think it depends on the theme we choose. – TarJae May 03 '23 at 13:40
1

I thought that it would be in specifying the parameters for the base plot, e.g. specifying "par(ps=7)" before the plot, where ps is "integer; the point size of text (but not symbols)."

Alternatively, I know when you go to save the plot using something like "png()" there is an option of specifying pointsize. So when I try the following and scale the plots to be the same, the axis font looks similar. You could mess around with margins etc.. to avoid having to scale.

foobar <- data.frame(x=rnorm(100), y=rnorm(100))

p_gg <- ggplot() + theme_light() + 
  geom_point(data=foobar, aes(x=x, y=y)) +
  theme(axis.title = element_text(face="bold", size = 7))
ggsave(p_gg, file="p_gg.png", width = 4, height = 4, units ="in", dpi = 600)

png(filename = "p_df.png",
    width = 4, height = 4, units = "in", pointsize = 7,
    bg = "white", res = 600, restoreConsole = TRUE)
par(new=TRUE, ps=7)
plot(foobar$x, foobar$y)
dev.off()

enter image description here

myt
  • 61
  • 3