In R, how can I get a contour plot overlayed on a graph without the axes being distorted?

Viewed 60

In R, I am able to perfectly create a contour plot and a separate graph of various functions. However, when I go to plot them together, because the contour plot has a key to the right of the graph that does not exist in the other graph, the function graph axes are distorted, and the lines go off the graph.

For example, I graph the line y=10000-x, and my x-axis goes from 0 to 10000. This graphs fine on its own, but when I add it to the contour plot graph, even though the contour plot graph has the same scale, the graph of y=10000-x goes off of the graph, because the location of the x=10000 area of the graph has shifted to the right to make room for the key.

I have attached my code a photo that better illustrates the problem.

x = seq(0, 10000,length.out=100)
y = seq(0, 10000,length.out=100)
z<- outer(x,y,function(x,y) (339-0.01*x-0.003*y)*x+(399-0.01*y-0.004*x)*y-(400000+195*x+225*y))
filled.contour(x, y, z,add=TRUE,col=hcl.colors(25, "Spectral"))


eq1 = function(x){(0*x)+8000} #equation goes in the brackets
curve(eq1, from=0, to=10000,add=TRUE,xlab="", ylab="",ylim=c(0,10000),col="purple",lwd=3)

eq2 = function(x){10000-x} #equation goes in the brackets
curve(eq2, from=0, to=10000,add=TRUE,ylim=c(0,10000),col="blue",lwd=3)



abline(v=5000,col='green',lwd=3)

abline(v=0,col='black',lwd=3)


eq1 = function(x){(0*x)} #equation goes in the brackets
curve(eq1, from=0, to=10000,add=TRUE,ylim=c(0,10000),col="black",lwd=3)

text(2000,4000,labels="feasible region")
title(main="Constraints of TV setup",xlab="Number of 19'' sets", ylab="Number of 21'' sets")

points(3846,6154,col="red",pch=16)
text(3846,6800,labels="max",col='red')
N = function(x,y){(339-0.01*x-0.003*y)*x+(399-0.01*y-0.004*x)*y-(400000+195*x+225*y)}
cat(N(3846,6154))
0 Answers
Related