0

As I'm learning R programming, I was trying to change font type in scatterplot.

For e.g., let say I want to do the following:

Q) For the Scatter plot with cars, change the font type, font size and color of the labels main, lab, and axis

I tried doing as below but could not complete it:

plot(cars$speed, cars$distance, font="New times Roman", cex.lab=3,
cex.axis=3, cex.main=5, col="brown", main="Title", xlab="speed", ylab="distance", font=10)

Please help

UseR10085
  • 7,120
  • 3
  • 24
  • 54
Johnny
  • 1
  • 2
  • Were you trying for "Times New Roman"? As far as I know, "New times Roman" is not a font. Take a look at https://www.statmethods.net/advgraphs/parameters.html – mhovd Jul 17 '20 at 13:26
  • Sorry...I tired 'Times New Roman', however I couldn't get it. Please help – Johnny Jul 17 '20 at 13:40
  • 1
    Does this answer your question? [Changing fonts in ggplot2](https://stackoverflow.com/questions/34522732/changing-fonts-in-ggplot2) – UseR10085 Jul 17 '20 at 14:34

2 Answers2

0

It's possible that it's only not working because you specified the font as "New times Roman" instead of "Times New Roman".

I'm not super familiar with base r plotting, but have you checked out ggplot2? It's a much better library for plotting in r. I'd also recommend grabbing tidyverse, another library that makes working with data in r a lot easier.

With ggplot and tidyverse, your plot might look like:

library(tidyverse)
cars %>% ggplot(aes(x=speed, y=dist)) + 
  geom_point() + 
  theme_minimal() + 
  theme(text=element_text(family="Times New Roman"))

enter image description here

G. Shand
  • 388
  • 3
  • 12
  • I wanted this in a simple scatter plot. Is it possible – Johnny Jul 17 '20 at 13:41
  • Do you mean without the grid in the background and axis labels? Change theme_minimal() to theme_void(). Here's a resource for changing ggplot theme components: https://ggplot2.tidyverse.org/reference/theme.html – G. Shand Jul 17 '20 at 13:53
0

You can define the font family in par():

par(family="Times New Roman")
plot(cars$speed, cars$distance, cex.lab=3, cex.axis=3, cex.main=5,col="brown",main="Title", xlab="speed", ylab="distance", font=10)

Edit:

If you do not want to pre-define the graphics parameters using par(), you could do this:

plot(cars$speed, cars$distance, cex.axis=2,
     xlab="", ylab="", main="")
title(main="Title", col.main="brown", cex.main=5, family="Times New Roman",
      xlab="speed", cex.lab=3, ylab="distance", col.lab="brown" )

Created on 2020-07-17 by the reprex package (v0.3.0)

user12728748
  • 8,106
  • 2
  • 9
  • 14