Problems with plot
Hi!
I was studying the lpSolveExamples.R and the plot graph show some errors in the x and y axis. Do you know how can I fix it?
It is important to mention that I have just used LP example called "lpSolveAPI example 1 from book".
Tks, Rplot.pdf``
// set up problem: maximize //20x1 + 60x2 subject to //30x1 + 20x2 <= 2700 //5x1 + 10x2 <= 850 //x1 + x2 >= 95 // x1 >= 0 // x2 >= 0
library(lpSolve)
#defining parameters
obj.fun <- c(20, 60) constr <- matrix(c(30, 20, 5, 10, 1, 1), ncol = 2, byrow=TRUE) constr.dir <- c("<=", "<=", ">=") rhs <- c(2700, 850, 95)
#solving model
prod.sol <- lp("max", obj.fun, constr, constr.dir, rhs, compute.sens = TRUE)
#accessing to R output
prod.sol$obj.val prod.sol$solution prod.sol$duals #includes duals of constraints and reduced costs of variables prod.sol$duals.from prod.sol$duals.to prod.sol$sens.coef.from prod.sol$sens.coef.to
The problem I see is with the tick marks of the axis labels. Please can you send me the code you have used to create the plot?
Not sure if this would be helpful but here is my version of the plot. Still new to R so the ggplot function was leverage from somewhere else. But hope this helps. Also, the shaded area of pdf, should it not be the triangle form by where the lines interest. Note both would product the optimal solution.(20,75)
intPoint <- data.frame(x=c(20,50,80), y=c(75,60,15))
ggplot(data.frame(x=c(0,170)),aes(x)) + stat_function(fun=function(x) -1.5x+135, geom="line", aes(col='y=-1.5x+135')) + stat_function(fun=function(x) -.5x + 85, geom="line", aes(col='y=-.5x+85')) + stat_function(fun=function(x)-1x+95, geom="line", aes(col='y=-1x+95')) + geom_vline(xintercept=0, aes(col= 'x=0')) + geom_hline(yintercept= 0, aes(col='y=0')) + theme_bw() + labs(title = 'Optimization Model') + geom_point(data=intPoint, aes(x,y)) + annotate('text', x = 20, y = 75, label="(20, 75)", size=3 ) + annotate('text', x = 50, y = 60, label="(50, 60)", size=3 ) + annotate('text', x = 80, y = 15, label="(80, 15)", size=3 )
S1 <- (2020)+(7560) S2 <- (5020)+(6060) S3 <- (8020)+(1560)
items <- c("Solution 1", "Solution 2", "Solution 3") Product_A <- c(20,50,80) Product_B <- c(75,60,15) Price_A <- rep(20,3) Price_B <- rep(60,3) Total <- c(S1,S2,S3) Answer <- data.frame(items,Product_A,Price_A,Product_B,Price_B,Total)
Let me know if either of you come up with a better way of calling the x within the data frame in ggplot. There has to be but my knowledge is limited.