字体系列在 ggplot 中不会改变 [英] Font family won't change in ggplot

查看:18
本文介绍了字体系列在 ggplot 中不会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情节有效,但我无法更改字体系列.它始终保持默认设置,即使我可以更改其他内容,例如颜色、大小和对齐方式.

这是我的代码:

ggplot(data = SeattleJuly17Data,aes(x = 价格,y = 满意度评分,col = RoomType)) +geom_point() +xlim(0,500) +geom_smooth() +ggtitle("按价格和房型划分的满意度趋势") +主题(plot.title = element_text(family = "Calibri",大小=15,颜色=红色",hjust = 0.5)) +xlab("每晚价格") +ylab("客人满意度评分")

解决方案

您可以使用

My plot works, except I'm not able to get the font family to change. It always stays with the default, even when I can other things to change like color, size, and justification.

Here's my code:

ggplot(data = SeattleJuly17Data,
       aes(x = Price, y = SatisfactionScore, col = RoomType)) +
    geom_point() +
    xlim(0,500) +
    geom_smooth() +
    ggtitle("Satisfaction Trends by Price and Room Type") +
    theme(plot.title = element_text(family = "Calibri",
                                    size=15,
                                    color="Red",
                                    hjust = 0.5)) +
    xlab("Price per Night") +
    ylab("Guest Satisfaction Score")

解决方案

You can use the extrafont package to pick the font that you want.

library(ggplot2)
library(ggpmisc)

### Use more updated dev version on Github
# install.packages("remotes")
# remotes::install_github("wch/extrafont")
library(extrafont)

### Run this one only ONCE to import all fonts to R
# font_import(prompt = FALSE)

# or import only specific font
font_import(pattern = "DejaVu", prompt = FALSE)

# if the font is not in default search path e.g. `C:/Windows/Fonts/`
myfontPath <- "C:/Users/xxx/Downloads/Fonts/"
font_import(pattern = "DejaVu",
            paths = myfontPath,
            recursive = TRUE,
            prompt = FALSE)

### Load fonts
# Options: "all", "pdf", "postscript", or "win"
loadfonts(device = "all")

### Choose the desired font
myFont <- "DejaVu Sans Mono"
# Can also override the default sans, mono or serif fonts
windowsFonts(mono  = myFont)
windowsFonts()

### Plot
set.seed(666)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3*df$x + rnorm(100, sd = 40)

formula1 <- y ~ x

ggplot(data = df, aes(x = x, y = y)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, formula = formula1) +
  stat_poly_eq(aes(label = paste(..eq.label.., sep = "~~~")), 
               family = myFont, # specify font
               label.x.npc = "right", label.y.npc = 0.15,
               eq.with.lhs = "italic(hat(y))~`=`~",
               eq.x.rhs = "~italic(x)",
               formula = formula1, parse = TRUE, size = 6) +
  stat_poly_eq(aes(label = paste(..rr.label.., sep = "~~~")), 
               family = myFont,
               label.x.npc = "right", label.y.npc = "bottom",
               formula = formula1, parse = TRUE, size = 6) +
  theme_bw(base_size = 20, 
           base_family = myFont) # specify font

这篇关于字体系列在 ggplot 中不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆