- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试用循环中的数据绘制一些简单的 x,y 线图。代码将生成数百个这样的图,其想法是将这些图保存在缩略图大小的图像中(例如 200 x 200 像素,DPI 并不重要),并将其与数据一起嵌入到 Excel 文件中。当我通过 ggplot2 绘图时,它看起来非常好,但是当我想保存它时,我得到的要么是裁剪后的图像,仅显示图像的一部分,要么是非常狭窄的图,标签/文本的大小不匹配。如果我将比例设置为 2,那么它看起来是正确的,但不符合我的标准。
我的数据框如下所示。
Drug=c("A","A","A","A","A","A")
Con=c(10,100,1000,10,100,1000)
Treatment=c("Dox","Dox","Dox","Pac","Pac","Pac")
Value=c(-3.8,-4.5,-14.1,4,-4.6,3.5)
mat_tbl=data.frame(Drug,Con,Treatment,Value)
p=ggplot(data=mat_tbl,aes(x=Con,y=Value,colour=Treatment,group=Treatment))+geom_point(size = 4)+geom_line()+labs(title="Drug A",x="Concentration",y="Value") + scale_y_continuous(limits=c(-25,125),breaks=seq(-25,125,by=25)) + theme_bw()
ggsave(filename = "curve_drug.png",plot=p,width=2,height=2,units="in",scale=1)
关于我需要处理哪些参数有什么建议吗?
最佳答案
请看一下我为此编写的函数,它是 ggsave
的辅助函数:
plot.save <- function(plot,
width = 800,
height = 500,
text.factor = 1,
filename = paste0(
format(
Sys.time(),
format = '%Y%m%d-%H%M%S'), '-Rplot.png'
)
) {
dpi <- text.factor * 100
width.calc <- width / dpi
height.calc <- height / dpi
ggsave(filename = filename,
dpi = dpi,
width = width.calc,
height = height.calc,
units = 'in',
plot = plot)
}
如您所见,DPI 只不过是 R 中绘图的文本因子。
考虑一下:
plot.save(some_ggplot, width = 400, height = 400, text.factor = 1)
这个(将 text.factor
从 1 更改为 0.5):
plot.save(some_ggplot, width = 400, height = 400, text.factor = 0.5)
两张图片都是 400 x 400 像素,就像我在函数中设置的那样。
<小时/>也许也很有趣:plot.save
的第一个参数是实际的 ggplot。所以现在我使以下成为可能(使用dplyr
包),我经常使用它:
ggplot(...) %>%
plot.save(200, 200) # here I've set width and height in pixels
关于R ggsave 保存缩略图大小(200 x 200),缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26551359/
我想知道如何在 superheat 包中使用 ggsave。 ggsave 不起作用...... # install devtools install.packages("devtools") # u
有没有办法在批处理模式下使用ggsave?当我说批处理模式时,我的意思是我首先通过 ggplot 绘制所有图,然后将每个图保存在特定文件夹中。 Here是一个建议称为 的参数 plot=last_pl
有没有办法在批处理模式下使用 ggsave?当我说批处理模式时,我的意思是我首先通过 ggplot 绘制所有绘图,然后将每个绘图保存在特定文件夹中。 Here是一个建议的参数,称为 plot=last
我有下面的例子 library(ggplot2) library(ggthemes) ggplot(mtcars) + geom_point(aes(x = wt, y = mpg, colour
图书馆(ggplot2) > ggplot(mpg, aes(displ, hwy))+geom_point() > ggsave("try.jpg") 它抛出一个错误 "Saving 6.99 x
每当我使用 ggsave 和 scale 保存绘图时,绘图的大小都会增加,但文本的大小不会。 ggplot(economics, aes(date, unemploy)) + geom_line(
我已经更新了我的 ggplot2 库,现在我遇到了一个问题,在调用 ggsave 后,图形设备不再显示绘图。我无法解决该问题,这意味着我无法根据需要导出图。请帮忙。 在网上搜索发现一些提到 dpi 不
我在更改 ggplot2 的标题大小、X-Y 标签、X-Y 轴文本时遇到问题.我正在使用 ggsave将绘图保存为 jpg。 p <- ggplot() p + theme(axis.title =
我正在保存一个分面 ggplot2 图,它可以很好地以较小的尺寸保存,但当我想增加它时却失败了。 > ggsave("tst.png",height=6.75,width=9) # works fin
我可以在存储后使用 ggsave 保存绘图,但在管道中使用它时出现以下错误。我希望在同一个(管道)命令中进行绘制和保存。 no applicable method for 'grid.draw'
R问题。 我对宽度、高度、dpi 和单位感到非常困惑。 为什么下面两个尺寸不同? ggsave(filename = "foo.png",ggplot(mtcars, aes(x=wt, y=mpg)
不必在每次调用时重复“height= 4, width= 4, dpi= 72”。 。 . 我曾经尝试过库(默认)来解决这样的问题,但它的表现不佳。我在 R-help 上问了一个问题,但似乎没有人使用
我刚开始学习使用 python。我正在使用 anaconda python 3.5 和 Rodeo 来做一个简单的 ggplot。 from ggplot import * df=pd.DataFra
我一直在尝试清理我的项目工作流程,并且一直在使用 here 包,但对某些实用程序感到困惑。 我在文件夹 ~\ProjFolder 中设置了一个 Rstudio 项目。在此文件夹中,我添加了一个 Plo
我想在 ggplot2::ggsave() 中保存 stats::heatmap() 的输出,但输出不是绘图,并且我收到一个错误。 这是代码: data("iris") col<- colorRamp
我使用 ggsave 来保存使用 ggplot2 生成的图形,这就是我的做法 figure1<-last_plot() ggsave(figure1,file="/home/user1/figure1
如何去除ggsave中的白边距? 我的问题和Remove white space (i.e., margins) ggplot2 in R一模一样。然而,那里的答案对我来说并不理想。我不想对固定但未知
我使用 ggplot2::ggsave() 创建了一个 svg。我将 svg 嵌入到一个 html 文件中。但是,我发现 svg 周围有一个边框。如何删除此边框? tl;dr 版本:download
我已经将 ggplot 导入到 python 中并正在运行以下脚本,希望 ggsave() 实际上会将我的绘图保存在某个地方,但它似乎实际上并没有将任何内容写入 .png 文件以供以后使用。当我在我的
我正在尝试在我的本地计算机(12 核 Mac Pro 2009 或 Macbook Pro 2017)中运行 foreach 和 doParallel。一旦出现 plot 或 device-out,例
我是一名优秀的程序员,十分优秀!