【发布时间】:2021-12-22 06:02:14
【问题描述】:
我发现 R 的默认图的别名很差。作为解决方案,我将 Cairo 设置为图形设备,现在绘图看起来好多了。
不幸的是,使用 Cairo 产生了另一个问题,即由于某种原因,我无法应用在绘图窗口中显示图表时使用的字体(在上面的左图中,Cambria已使用,但右图未能应用此字体)。
这是我的代码:
library(readxl)
library(scales)
library(ggplot2)
library(dplyr)
library('Cairo')
windowsFonts(Cam = windowsFont("Cambria"))
dataset <- read_excel('CW Data.xlsx')
colnames(dataset)[4] <- "Broadband Subs (%)"
options(scipen = 1000)
# Scatter plot FDI~GDP with regression line
CairoWin()
ggplot(dataset, aes(x=`2019 GDP ($bn)`, y=`2019 FDI ($m)`)) +
geom_point(size=3, shape=1) +
geom_smooth(method='lm',formula=y~x, se=FALSE, color='black') +
scale_x_continuous(label = comma) + scale_y_continuous(label=comma) +
theme(panel.background = element_rect(fill="peachpuff"),
plot.background = element_rect(fill="peachpuff")) +
theme(panel.grid.major = element_line(colour = "gray72"),
panel.grid.minor = element_line(colour = "gray72")) +
theme(text = element_text(family = "Cam"))
ggsave("FDI~GDP.png", device="png", type = "cairo")
这是我正在使用的 Excel 数据的 OneDrive 链接
【问题讨论】:
-
Ryan,而不是链接到一个不稳定的驱动器数据,可能需要登录,并且将来可能不再工作,您是否愿意创建一个像 teunbrand 在他的“答案”中建议的示例“?
-
这是我从 OneDrive 链接得到的:
This item might not exist or is no longer available This item might have been deleted, expired, or you might not have permission to view it. -
添加了 [windows] 标签,因为在将
x11Fonts换成windowsFonts调用并在下面的非回答中使用 MCVE 后,我无法在 Linux 机器上重现。我假设 Cambria 应该产生一种看起来像老式打字机输出的字体(细、衬线、等宽)。