r - 在 R 中为 tiff() 输出调整和选择适当大小的提示和技巧;保存时图像与文本经常不成比例
问题描述
我一直在努力tiff() output
为轴、刻度和标签上的特定文本和相应文本选择大小、单位等之间的平衡。我经常发现图像大小和轴文本/标签之间的比例在保存时很容易变得不平衡或比例不当。我认为我错过了保存图像的基本提示和技巧。
这个线程的目的是收集关于如何很好地平衡图形和文本的技巧和窍门tiff() output
。我已经制作了一个问题所在的示例。
请w
在下面找到数据和脚本示例。
我用 dobbel y 轴制作了这个图:
这张图片是直接从 R 截取的。看起来不错,所以我想把它保存tiff() file
为我的手稿。
因此,我写道:
tiff("plot.tiff", width = 320, height = 180, units = 'mm', res = 300)
... (script below) ...
dev.off()
不幸的是,当我tiff() file
在我的工作目录中找到我的时候,它看起来“太大”并且不像 R Studio 中的“绘图窗口”,如上所示。我当然必须“放大”几次才能在 R 绘图窗口中产生相同的质量,但是,绘图因此尺寸变得太大。
理想情况下,此特定图是复合图形的一部分,如下所示:
如您所见,它看起来不太好。
所以,显然,我已经尝试改变width
,height
和res
units
tiff()
tiff("plot.tiff", width = xx, height = xx units = 'mm', res = 300)
... (script below) ...
dev.off()
但是,这通常会影响轴文本和标签。像这样:
所以,我的问题是,关于如何调整大小tiff()
和文本以使其保持比例,是否有任何解决方案、提示、技巧等?
脚本
w$WHO <- as.factor(w$WHO)
yaks <- c(0,2,4,6,8,10,12,14,16,18)
ggplot() + theme_bw() +
geom_bar(mapping = aes(x = w$WHO[w$response==1]), stat = "count", alpha=0.2, colour="#1C73C2", fill="#ECF0F9") +
scale_y_continuous(name = "Number of progressions", breaks=yaks, sec.axis = sec_axis(~ . * 1 , name = "Progression rate per 100 person-yrs", breaks=yaks, labels=c("0","40","80","120","160","200","240","280","320","360"))) +
scale_x_discrete(name="", drop = FALSE, labels =c("WHO-I\nn=32","WHO-II\nn=23","WHO-III\nn=16", "Unknown\nn=5")) +
coord_cartesian(ylim=c(0, 18)) +
geom_point(mapping = aes(x = 1, y = 0.2677376), size=10, alpha=0.7, shape=18, colour="red") +
geom_point(mapping = aes(x = 2, y = 3.2870709), size=10, shape=18,alpha=0.7, colour="red") +
geom_point(mapping = aes(x = 3, y = 16.98), size=10, shape=18,alpha=0.7, colour="red") +
annotate("text", x = c(1,2,3, 4) , y = c(-0.5, 2.2870709, 15.98, 0.5), label = c("5.4","65.7","339.6", "0 events"), col="red", fontface=2, cex=4)+
theme(axis.text.y.right = element_text(color = "red", size = 11),
axis.title.y=element_text(size=14,face="bold", margin = margin(t = 0, r = 20, b = 0, l = 0)),
axis.text.y = element_text(color = "#1C73C2", size = 11),
axis.title.y.right=element_text(size=14,face="bold", margin = margin(t = 0, r = 0, b = 0, l = 20)),
axis.text.x = element_text(color = "grey20", size = 11))
数据
w <- structure(list(WHO = c(1L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 1L, 2L,
3L, 3L, 3L, 1L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 1L, 3L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 4L, 4L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 3L, 4L,
3L, 3L), response = c(0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L,
0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L,
1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L,
1L, 1L), rfs = c(547.875, 60.875, 395.6875, 517.4375, 669.625,
60.875, 182.625, 152.1875, 365.25, 243.5, 91.3125, 60.875, 30.4375,
639.1875, 60.875, 1308.8125, 121.75, 60.875, 121.75, 152.1875,
3.04375, 4169.9375, 121.75, 2648.0625, 517.4375, 730.5, 2191.5,
578.3125, 426.125, 2526.3125, 2069.75, 1704.5, 1734.9375, 547.875,
426.125, 474.825, 270.89375, 456.5625, 499.175, 234.36875, 2298.03125,
91.3125, 1667.975, 675.7125, 365.25, 435.25625, 182.625, 365.25,
663.5375, 182.625, 91.3125, 365.25, 91.3125, 182.625, 91.3125,
91.3125, 365.25, 295.24375, 91.3125, 91.3125, 365.25, 91.3125,
182.625, 91.3125, 182.625, 121.75, 1521.875, 639.1875, 913.125,
152.1875, 334.8125, 365.25, 121.75, 547.875, 182.625, 91.3125
)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 70L, 71L,
72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L,
85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L,
98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L,
109L, 111L), class = "data.frame", na.action = structure(c(`36` = 36L,
`37` = 37L, `38` = 38L, `39` = 39L, `40` = 40L, `41` = 41L, `42` = 42L,
`43` = 43L, `44` = 44L, `45` = 45L, `46` = 46L, `47` = 47L, `48` = 48L,
`49` = 49L, `50` = 50L, `51` = 51L, `52` = 52L, `53` = 53L, `54` = 54L,
`55` = 55L, `56` = 56L, `57` = 57L, `58` = 58L, `59` = 59L, `60` = 60L,
`61` = 61L, `62` = 62L, `63` = 63L, `64` = 64L, `65` = 65L, `66` = 66L,
`67` = 67L, `68` = 68L, `69` = 69L, `110` = 110L), class = "omit"))
解决方案
推荐阅读
- javascript - 向nginx反向代理传递参数
- stm32 - 在 STM32L4R5 中使用 USART
- linux - 从某一行搜索文件中的字符串 no
- c# - 如何使用 Moq 对 Azure Key Vault 进行单元测试
- python - 如何使用“%matplotlib inline”运行 Python 代码?
- reactjs - 如何获取两个日期之间的天数?
- python - 在哪里提交 keras 的拉取请求?
- javascript - JavaScript 控制台样式的整个日志
- excel - 仅在 excel 文件中查找子脚本和超级脚本并用相同的 unicode 或符号替换它们
- javascript - 在 Javascript 中格式化 json