Modifying fonts in ggplot2

前端 未结 7 1314
再見小時候
再見小時候 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条回答
  • 2020-11-29 17:22

    Have a look at the family argument of theme_text()

    dummy <- data.frame(A = rnorm(10), B = rnorm(10))
    ggplot(dummy, aes(x = A, y = B)) + geom_point()
    #helvetica = default
    ggplot(dummy, aes(x = A, y = B)) + geom_point() + opts(axis.title.x = theme_text(family = "sans", face = "bold"))
    #times
    ggplot(dummy, aes(x = A, y = B)) + geom_point() + opts(axis.title.x = theme_text(family = "serif", face = "bold"))
    #courier 
    ggplot(dummy, aes(x = A, y = B)) + geom_point() + opts(axis.title.x = theme_text(family = "mono", face = "bold"))
    
    0 讨论(0)
  • 2020-11-29 17:25

    You can set the font of the labels produced by geom_text with grid.gedit:

    grid.gedit("GRID.text",gp=gpar(fontfamily="mono"))
    

    Call this after you have produced your original plot.

    0 讨论(0)
  • 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
    

    0 讨论(0)
  • 2020-11-29 17:36

    Also check out the Cairo package, which has support for totally switching out all of the fonts with those of your choosing. http://rforge.net/doc/packages/Cairo/00Index.html

    0 讨论(0)
  • 2020-11-29 17:43

    I think your answer is fine but you can do it more simply:

    install.packages("extrafont");library(extrafont)
    font_import("Trebuchet MS")
    library(ggplot2)
    qplot(1:10)+theme(text=element_text(family="Trebuchet MS"))
    
    0 讨论(0)
  • 2020-11-29 17:44

    Inspired by a post on kohske's blog I came up with this:

    theme_set( theme_bw( base_family= "serif"))
    
    theme_update( panel.grid.minor= theme_blank(),
                 panel.grid.major= theme_blank(),
                 panel.background= theme_blank(),
                 axis.title.x= theme_blank(),
                 axis.text.x= theme_text( family= "serif",
                   angle= 90, hjust= 1 ),
                 axis.text.x= theme_text( family= "serif"),
                 axis.title.y= theme_blank())
    
    theme_map <- theme_get()
    
    theme_set( theme_bw())
    

    Now when I want to use that particular theme:

    last_plot() + theme_map
    

    YMMV.

    BTW, if I had the power I would vote down the preferred answer:

    > grid.gedit("GRID.text",gp=gpar(fontfamily="mono"))
    Error in editDLfromGPath(gPath, specs, strict, grep, global, redraw) :
      'gPath' (GRID.text) not found
    

    Not sure what this means. Nor was I offered a link to comment on that answer; maybe something has changed on the site.

    0 讨论(0)
提交回复
热议问题