gpt4 book ai didi

r - 如何在 ggplot2 中获得准确的字体、线条、点和图形大小?

转载 作者:行者123 更新时间:2023-12-02 15:02:59 31 4
gpt4 key购买 nike

对于提交的最终文章,我被要求更新我的数据,以便它们符合以下规范:

  1. 轴线为 0.25 毫米
  2. 轴线周围,刻度线朝内
  3. 数据线为 0.5 毫米
  4. 字体为10pt
  5. 人物宽度应为 80 或 169 毫米
  6. 必须为 300 dpi

我尝试过的:

library(ggplot2)
library(cowplot)
theme_set(theme_bw())

x <- rnorm(100)
mydata <- data.frame(x = x,
y = x^2 + runif(100),
z = rep(letters[1:4], 25))

p <- ggplot(data = mydata, aes(x, y)) +
geom_point(aes(color = z)) +
geom_smooth(color = 'black', se = FALSE, size = 0.5) +
theme(text = element_text(family = 'Times', size = 10, color = 'black'),
axis.ticks.length = unit(-0.1, 'cm'),
axis.text.x = element_text(margin = margin(t = 4, unit = 'mm')),
axis.text.y = element_text(margin = margin(r = 4, unit = 'mm')),
panel.grid = element_blank(),
axis.line = element_line(size = 0.25),
legend.position = c(0.5, 0.75))

p
ggsave(plot = p,
filename = 'myplot.png',
width = 80, height = 50, dpi = 300, units = 'mm')

p2 <- cowplot::plot_grid(plotlist = list(p, p, p, p), nrow = 1)
ggsave(plot = p2,
filename = 'mymultipleplot.png',
width = 169, height = 50, dpi = 300, units = 'mm')

返回以下两个图:

enter image description here

enter image description here

我可以弄清楚如何处理这里的一些问题(例如图例位置),但在以下方面遇到困难:

  1. 如何获取上轴和右轴周围的刻度?
  2. 如何获得正确的尺寸...
    • 这些看起来比 10 pt 大得多。 (下载它们或在新窗口中打开以查看未缩放的版本)
    • 尽管在主题(字体、线条)中指定了尺寸,但两个图中的尺寸并未保持不变。
    • 我不知道如何确认线条尺寸正确(以磅或毫米为单位)...ggsave 是否会自行缩放?
<小时/>

更新 对于我当前的任务,我导出为 svg 文件并在 Inkscape 中对其进行编辑。这花了几个小时,但比让 ggplot 扭曲规范更容易。

但是,了解将来如何在 ggplot2 中以编程方式执行此操作将会很有帮助。

最佳答案

问题答案:1)正如 Henrik 在评论中所说:

For question 1 (How can I get ticks around top and right axes?), see the new sec.axis argument in scale_ in ggplot 2.2.0. Try e.g. ggplot(mpg, aes(displ, hwy)) + geom_point() + scale_x_continuous(sec.axis = dup_axis()) + scale_y_continuous(sec.axis = dup_axis())

2) 这里的问题是你有相同的分辨率但不同的尺寸。由于两个图形的高度相同,您可以通过手动将字体大小乘以宽度的比例来解决缩放字体大小的问题:例如

theme(text = element_text(family = 'Times', size = 10*(80/169), color = 'black')

整个代码应该如下所示:

library(ggplot2)
library(cowplot)
theme_set(theme_bw())

x <- rnorm(100)
mydata <- data.frame(x = x,
y = x^2 + runif(100),
z = rep(letters[1:4], 25))

p1 <- ggplot(data = mydata, aes(x, y)) +
geom_point(aes(color = z)) + scale_x_continuous(sec.axis = dup_axis()) +
scale_y_continuous(sec.axis = dup_axis()) +
geom_smooth(color = 'black', se = FALSE, size = 0.5) +
theme(text = element_text(family = 'Times', size = 10*(80/169), color = 'black'),
axis.ticks.length = unit(-0.1, 'cm'),
axis.text.x = element_text(margin = margin(t = 4, unit = 'mm')),


axis.text.y = element_text(margin = margin(r = 4, unit = 'mm')),

panel.grid = element_blank(),
axis.line = element_line(size = 0.25),
legend.position = c(0.5, 0.75))

p2 <- ggplot(data = mydata, aes(x, y)) +
geom_point(aes(color = z)) + scale_x_continuous(sec.axis = dup_axis()) +
scale_y_continuous(sec.axis = dup_axis()) +
geom_smooth(color = 'black', se = FALSE, size = 0.5) +
theme(text = element_text(family = 'Times', size = 10, color = 'black'),
axis.ticks.length = unit(-0.1, 'cm'),
axis.text.x = element_text(margin = margin(t = 4, unit = 'mm')),


axis.text.y = element_text(margin = margin(r = 4, unit = 'mm')),

panel.grid = element_blank(),
axis.line = element_line(size = 0.25),
legend.position = c(0.5, 0.75))


p1
ggsave(plot = p1,
filename = 'myplot.png',
width = 80, height = 50, dpi = 300, units = 'mm')

p2multi <- cowplot::plot_grid(plotlist = list(p2, p2, p2, p2), nrow = 1)

ggsave(plot = p2multi ,
filename = 'mymultipleplot.png',
width = 169, height = 50, dpi = 300, units = 'mm')

关于r - 如何在 ggplot2 中获得准确的字体、线条、点和图形大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40594885/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com