【问题标题】:R plot values using dice font?R使用骰子字体绘制值?
【发布时间】:2021-03-02 20:39:11
【问题描述】:

我想在 R 中使用值 1 到 6 的 dice 字体创建一个绘图。可以下载 windows 的 Dice 字体here - 将下载的 true type 字体复制到 C:\Windows\Fonts 目录,它可以可用于 Excel 或 Word 等 Windows 应用程序。

我在question 中发现了用于向 R 添加字体的 showtext 包,这似乎是使用下面的 R 脚本添加骰子字体。

require(showtext)
font_add(family = "dice",regular = "C:/Users/Mark/AppData/Local/Microsoft/Windows/Fonts/dice.ttf")

出于某种原因,我不明白我必须设置 AppData/Local 目录的路径才能让 font_add 不报告无法找到错误。下一个挑战是做一个简单的测试图

plot(c(1,2),c(2,1),pch = 0, col = "blue", type = "p", cex =5)
text(c(1,2),c(2,1),c(2,1),col = "red")

但是,当我尝试使用文本功能使用骰子字体时,我得到一个错误...

在 Windows 字体数据库中找不到字体系列

text(c(1,2),c(2,1),c(2,1),col = "red", family = "dice")

问题:

  1. 有没有办法显示 R 可访问的字体,以便我确定骰子字体是否可用?
  2. 如果 dice 字体可用,那么如何让字体与文本功能一起使用?
  3. 是否有替代或更好的方法来实现这个想法?

【问题讨论】:

标签: r


【解决方案1】:

在探索了 Andrew Brēza 建议的示例后,我设法使用 ggplot2 获得了结果 - 我找不到基本 R 的解决方案。关于我在原帖中列出的三个问题

  1. showtext有两个函数可以分别列出字体家族和可用的字体

    font_families() 字体文件()

我还发现我需要使用 Windows 右键单击​​选项“为所有用户安装”来安装 dice 字体,否则字体文件安装在用户 AppData/Local 目录下,而不是 C:\Windows\Font 目录下

2.showtext 似乎无法与基本 R 文本函数一起使用,即使字体被列为可用 - 因此我探索了 ggplot

3 ggplot with showtext 是唯一可行的解​​决方案 - 下面的脚本会产生我正在寻找的结果 - 我只需要花一点时间让它看起来像一个基本的 R 图

# Plot numbers using dice font
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# https://stackoverflow.com/questions/34522732/changing-fonts-in-ggplot2

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
require(showtext)

# Note install the dice font using right-click install for all users
# Otherwise the font file is install in user profile nto C:\Windows\fonts

# Add the dice font to R
font_add(family = "dice",regular = "C:/Windows/Fonts/dice.ttf")

# Checks to show the dice font available in the fonts available to R

font_families()
font_files()

# Automatically use show text for newplots - do I need this?

showtext_auto()

# Load ggplot2
require(ggplot2)

# Create some data
df <- data.frame(rbind(c(1,2,2),c(2,1,4)))


# Plot the dice values
ggplot(df, aes(x = X1, y = X2)) +
        geom_point(size = 0.1, shape = 0) +
        annotate("text",df[1,1],df[1,2], label = df[1,3], family = "dice", size = 10) +
        annotate("text",df[2,1],df[2,2], label = df[2,3], family = "dice", size = 10) 

【讨论】:

    猜你喜欢
    • 2021-12-06
    • 1970-01-01
    • 2018-09-08
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2022-01-05
    • 2015-01-30
    相关资源
    最近更新 更多