对于最终文章提交,我被要求更新我的数据,以便它们符合以下规范:
我尝试了什么:
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')
返回以下两个图:
我可以弄清楚如何处理这里的一些问题(例如传奇位置),但我对以下内容有困难:
ggsave
是否自行缩放?更新对于我目前的任务,我将其导出为svg文件并在Inkscape中进行编辑。花了几个小时,但比让ggplot扭曲到规格更容易。
但是,知道将来如何在ggplot2中以编程方式执行此操作会很有帮助。
答案 0 :(得分:1)
回答问题: 1)Henrik在评论中说:
对于问题1(如何在顶部和右侧轴上打勾?),请参阅ggplot 2.2.0中scale_中的新sec.axis参数。尝试例如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')