Modifying fonts in ggplot2

前端 未结 7 1346
再見小時候
再見小時候 2020-11-29 16:43

I am looking for a way to modify font types in ggplot. At the moment I would be happy enough to simply change fonts to the \'courier\' font family, but ultimately my goal is

7条回答
  •  猫巷女王i
    2020-11-29 17:34

    This seems like the simplest solution, for my money.

    Some play data in df, and made into a simple graph, "p", with nice long x and y labels, so we can see the font change:

    df <- data.frame(A = rnorm(10), B = rnorm(10))
    p = ggplot(data = df, aes(x = A, y = B)) + geom_point()
    p = p + xlab("A long x-string so we can see the effect of the font switch")
    p = p + ylab("Likewise up the ordinate")
    

    And we view the default plot in whatever that font is:

    p 
    

    Now we switch to Optima, adding some nice title and subtitle to bask in the glory of Optima:

    label = "Now we switch to Optima"
    subtitle = "Optima is a nice font: https://en.wikipedia.org/wiki/Optima#Usages"
    

    And after all that, we print in the new font

    # the only line you need to read:
    p + theme(text = element_text(family = "Optima", , face = "bold"))
    p = p + ggtitle(label = label, subtitle = subtitle)
    p
    

提交回复
热议问题