0 引言
很多中文期刊,对投稿的图形,要求刻度线指向图形内部。ggplot2 怎么做到刻度线向内?其实很简单。
1 加载包
library(ggplot2)
library(svglite)
2 设置轴刻度向内
ggplot() +
geom_text(
aes(
x = 1,
y = 1,
label = "Axes ticks inside!"
),
size = 4
) +
theme_classic(base_size = 14) +
theme(
# 设置刻度线的长度为负数即可
axis.ticks.length = unit(-4, "pt")
)
3 更复杂的案例
轴名称的单位上下标的设置,参考此公众号之前的推送文章。
在公众号聊天对话框发送
上下标
,或在主页搜索上下标
即可找到。
FONT <- "serif" # Times New Roman
SIZE <- 12
figure1 <- ggplot(
data = mpg,
aes(x = drv, y = hwy)
) +
stat_summary(
geom = "col",
fun = mean,
color = "black",
fill = NA
) +
stat_summary(
geom = "errorbar",
fun.data = mean_se,
width = 0.2
) +
scale_y_continuous(
expand = expansion(mult = c(0, 0))
) +
labs(
x = "X 轴名称\nYour X title", # \n 表示换行
y = "Y 轴名称\nYour Y title"
) +
coord_cartesian(
ylim = c(0, 30),
clip = "off"
) +
theme_classic(
base_size = SIZE,
base_family = FONT,
base_line_size = 0.4
) +
theme(
axis.title = element_text(size = SIZE),
axis.text = element_text(size = SIZE, color = "black"),
axis.ticks = element_line(color = "black", lineend = "square"),
axis.ticks.length = unit(-4, "pt")
)
3.1 保存高分辨率图片
# PNG
ggsave(
"figure-1.png",
figure1,
width = 4,
height = 4,
dpi = 600
)
# TIFF
# 体积很大,动不动几十兆,其实不推荐
ggsave(
"figure-1.tiff",
figure1,
width = 4,
height = 4,
dpi = 600
)
3.2 保存为矢量图
保存为 SVG 格式,则可以在 PPT 里继续手动编辑,比如,手动添加字母显著性标记等。
PPT 里将 SVG 图解散组合,即可编辑。
# PDF
ggsave(
"figure-1.pdf",
figure1,
width = 4,
height = 4,
device = cairo_pdf
)
# SVG
ggsave(
"figure-1.svg",
figure1,
width = 4,
height = 4
)