Keeping the resolution of a plot but condensing the size in R












0















I have a plot that I am generating in R that needs to be in .pdf format. The current code that I am using is the following:



x1 = seq(0,1,.001)
x2 = seq(0,1,.001)

f = function(x1,x2){
ans = x1 + x2
return(ans)
}

h = function(x1,x2){
ans1 = 1.5-x1-2*x2-.5*sin(2*pi*(x1^2-2*x2))
ans2 = x1^2+x2^2-1.5
ans1 = sapply(ans1,function(x){max(x,0)})
ans2 = sapply(ans2,function(x){max(x,0)})
ans = ans1 + ans2
return(ans)
}

z = outer(x1,x2,f)
w = outer(x1,x2,h)

image(x1,x2,z,xlab=expression(x[1]),ylab=expression(x[2]))
contour(x1,x2,z,add=TRUE)

X = cbind(expand.grid(x1,x2),c(w))
points(X[X[,3]!=0,1],X[X[,3]!=0,2],pch=19,col="lightgrey")


Now the plot looks exactly as I would like for it to be, however, when I go to save the plot as a .pdf the size of the file is around 60 MB which is far to large for me to place into a document (let alone it takes quite sometime to even open). Is there a way to modify my code to keep the plot looking the same, but having the file size be much smaller?



I understand that the problem is because I plot so many points, however, I am not sure how to generate the nice "smooth" grey area without doing so. Perhaps there is a simple command to use that I am unaware of.










share|improve this question























  • Do you need vector graphics or would a PNG do instead?

    – Roland
    Nov 22 '18 at 6:20











  • Unfortunately vector.

    – John Smith
    Nov 22 '18 at 6:25











  • I recently ran into the same problem. There's no way to compress a vector graphic, if I'm right. Consider Roland's suggestion, or consider a jpeg which may look smoother. Another way would be to blur your variances a little to an amount that's sufficient for printing, e.g. rather 0.000 0.005 0.010 0.015... than 0.000 0.001 0.002 0.003....

    – jay.sf
    Nov 22 '18 at 7:19













  • I'd expect a journal to accept a PNG (possibly embedded in a PDF) of sufficient resolution. Vector graphics really is not the right tool for this.

    – Roland
    Nov 22 '18 at 8:52











  • What about options for rewriting the code? Say, instead of using points are there other commands that I could use to get the same graphic that would take up less space? For example, say, using polygon (not sure how that would work in this context) instead of points?

    – John Smith
    Nov 22 '18 at 10:05
















0















I have a plot that I am generating in R that needs to be in .pdf format. The current code that I am using is the following:



x1 = seq(0,1,.001)
x2 = seq(0,1,.001)

f = function(x1,x2){
ans = x1 + x2
return(ans)
}

h = function(x1,x2){
ans1 = 1.5-x1-2*x2-.5*sin(2*pi*(x1^2-2*x2))
ans2 = x1^2+x2^2-1.5
ans1 = sapply(ans1,function(x){max(x,0)})
ans2 = sapply(ans2,function(x){max(x,0)})
ans = ans1 + ans2
return(ans)
}

z = outer(x1,x2,f)
w = outer(x1,x2,h)

image(x1,x2,z,xlab=expression(x[1]),ylab=expression(x[2]))
contour(x1,x2,z,add=TRUE)

X = cbind(expand.grid(x1,x2),c(w))
points(X[X[,3]!=0,1],X[X[,3]!=0,2],pch=19,col="lightgrey")


Now the plot looks exactly as I would like for it to be, however, when I go to save the plot as a .pdf the size of the file is around 60 MB which is far to large for me to place into a document (let alone it takes quite sometime to even open). Is there a way to modify my code to keep the plot looking the same, but having the file size be much smaller?



I understand that the problem is because I plot so many points, however, I am not sure how to generate the nice "smooth" grey area without doing so. Perhaps there is a simple command to use that I am unaware of.










share|improve this question























  • Do you need vector graphics or would a PNG do instead?

    – Roland
    Nov 22 '18 at 6:20











  • Unfortunately vector.

    – John Smith
    Nov 22 '18 at 6:25











  • I recently ran into the same problem. There's no way to compress a vector graphic, if I'm right. Consider Roland's suggestion, or consider a jpeg which may look smoother. Another way would be to blur your variances a little to an amount that's sufficient for printing, e.g. rather 0.000 0.005 0.010 0.015... than 0.000 0.001 0.002 0.003....

    – jay.sf
    Nov 22 '18 at 7:19













  • I'd expect a journal to accept a PNG (possibly embedded in a PDF) of sufficient resolution. Vector graphics really is not the right tool for this.

    – Roland
    Nov 22 '18 at 8:52











  • What about options for rewriting the code? Say, instead of using points are there other commands that I could use to get the same graphic that would take up less space? For example, say, using polygon (not sure how that would work in this context) instead of points?

    – John Smith
    Nov 22 '18 at 10:05














0












0








0








I have a plot that I am generating in R that needs to be in .pdf format. The current code that I am using is the following:



x1 = seq(0,1,.001)
x2 = seq(0,1,.001)

f = function(x1,x2){
ans = x1 + x2
return(ans)
}

h = function(x1,x2){
ans1 = 1.5-x1-2*x2-.5*sin(2*pi*(x1^2-2*x2))
ans2 = x1^2+x2^2-1.5
ans1 = sapply(ans1,function(x){max(x,0)})
ans2 = sapply(ans2,function(x){max(x,0)})
ans = ans1 + ans2
return(ans)
}

z = outer(x1,x2,f)
w = outer(x1,x2,h)

image(x1,x2,z,xlab=expression(x[1]),ylab=expression(x[2]))
contour(x1,x2,z,add=TRUE)

X = cbind(expand.grid(x1,x2),c(w))
points(X[X[,3]!=0,1],X[X[,3]!=0,2],pch=19,col="lightgrey")


Now the plot looks exactly as I would like for it to be, however, when I go to save the plot as a .pdf the size of the file is around 60 MB which is far to large for me to place into a document (let alone it takes quite sometime to even open). Is there a way to modify my code to keep the plot looking the same, but having the file size be much smaller?



I understand that the problem is because I plot so many points, however, I am not sure how to generate the nice "smooth" grey area without doing so. Perhaps there is a simple command to use that I am unaware of.










share|improve this question














I have a plot that I am generating in R that needs to be in .pdf format. The current code that I am using is the following:



x1 = seq(0,1,.001)
x2 = seq(0,1,.001)

f = function(x1,x2){
ans = x1 + x2
return(ans)
}

h = function(x1,x2){
ans1 = 1.5-x1-2*x2-.5*sin(2*pi*(x1^2-2*x2))
ans2 = x1^2+x2^2-1.5
ans1 = sapply(ans1,function(x){max(x,0)})
ans2 = sapply(ans2,function(x){max(x,0)})
ans = ans1 + ans2
return(ans)
}

z = outer(x1,x2,f)
w = outer(x1,x2,h)

image(x1,x2,z,xlab=expression(x[1]),ylab=expression(x[2]))
contour(x1,x2,z,add=TRUE)

X = cbind(expand.grid(x1,x2),c(w))
points(X[X[,3]!=0,1],X[X[,3]!=0,2],pch=19,col="lightgrey")


Now the plot looks exactly as I would like for it to be, however, when I go to save the plot as a .pdf the size of the file is around 60 MB which is far to large for me to place into a document (let alone it takes quite sometime to even open). Is there a way to modify my code to keep the plot looking the same, but having the file size be much smaller?



I understand that the problem is because I plot so many points, however, I am not sure how to generate the nice "smooth" grey area without doing so. Perhaps there is a simple command to use that I am unaware of.







r plot






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 5:50









John SmithJohn Smith

1




1













  • Do you need vector graphics or would a PNG do instead?

    – Roland
    Nov 22 '18 at 6:20











  • Unfortunately vector.

    – John Smith
    Nov 22 '18 at 6:25











  • I recently ran into the same problem. There's no way to compress a vector graphic, if I'm right. Consider Roland's suggestion, or consider a jpeg which may look smoother. Another way would be to blur your variances a little to an amount that's sufficient for printing, e.g. rather 0.000 0.005 0.010 0.015... than 0.000 0.001 0.002 0.003....

    – jay.sf
    Nov 22 '18 at 7:19













  • I'd expect a journal to accept a PNG (possibly embedded in a PDF) of sufficient resolution. Vector graphics really is not the right tool for this.

    – Roland
    Nov 22 '18 at 8:52











  • What about options for rewriting the code? Say, instead of using points are there other commands that I could use to get the same graphic that would take up less space? For example, say, using polygon (not sure how that would work in this context) instead of points?

    – John Smith
    Nov 22 '18 at 10:05



















  • Do you need vector graphics or would a PNG do instead?

    – Roland
    Nov 22 '18 at 6:20











  • Unfortunately vector.

    – John Smith
    Nov 22 '18 at 6:25











  • I recently ran into the same problem. There's no way to compress a vector graphic, if I'm right. Consider Roland's suggestion, or consider a jpeg which may look smoother. Another way would be to blur your variances a little to an amount that's sufficient for printing, e.g. rather 0.000 0.005 0.010 0.015... than 0.000 0.001 0.002 0.003....

    – jay.sf
    Nov 22 '18 at 7:19













  • I'd expect a journal to accept a PNG (possibly embedded in a PDF) of sufficient resolution. Vector graphics really is not the right tool for this.

    – Roland
    Nov 22 '18 at 8:52











  • What about options for rewriting the code? Say, instead of using points are there other commands that I could use to get the same graphic that would take up less space? For example, say, using polygon (not sure how that would work in this context) instead of points?

    – John Smith
    Nov 22 '18 at 10:05

















Do you need vector graphics or would a PNG do instead?

– Roland
Nov 22 '18 at 6:20





Do you need vector graphics or would a PNG do instead?

– Roland
Nov 22 '18 at 6:20













Unfortunately vector.

– John Smith
Nov 22 '18 at 6:25





Unfortunately vector.

– John Smith
Nov 22 '18 at 6:25













I recently ran into the same problem. There's no way to compress a vector graphic, if I'm right. Consider Roland's suggestion, or consider a jpeg which may look smoother. Another way would be to blur your variances a little to an amount that's sufficient for printing, e.g. rather 0.000 0.005 0.010 0.015... than 0.000 0.001 0.002 0.003....

– jay.sf
Nov 22 '18 at 7:19







I recently ran into the same problem. There's no way to compress a vector graphic, if I'm right. Consider Roland's suggestion, or consider a jpeg which may look smoother. Another way would be to blur your variances a little to an amount that's sufficient for printing, e.g. rather 0.000 0.005 0.010 0.015... than 0.000 0.001 0.002 0.003....

– jay.sf
Nov 22 '18 at 7:19















I'd expect a journal to accept a PNG (possibly embedded in a PDF) of sufficient resolution. Vector graphics really is not the right tool for this.

– Roland
Nov 22 '18 at 8:52





I'd expect a journal to accept a PNG (possibly embedded in a PDF) of sufficient resolution. Vector graphics really is not the right tool for this.

– Roland
Nov 22 '18 at 8:52













What about options for rewriting the code? Say, instead of using points are there other commands that I could use to get the same graphic that would take up less space? For example, say, using polygon (not sure how that would work in this context) instead of points?

– John Smith
Nov 22 '18 at 10:05





What about options for rewriting the code? Say, instead of using points are there other commands that I could use to get the same graphic that would take up less space? For example, say, using polygon (not sure how that would work in this context) instead of points?

– John Smith
Nov 22 '18 at 10:05












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424623%2fkeeping-the-resolution-of-a-plot-but-condensing-the-size-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424623%2fkeeping-the-resolution-of-a-plot-but-condensing-the-size-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory