Graphing equations involving absolute values
How would you draw in R the graph of the equation
|y| = 开发者_如何转开发1 - |x|
fun1 <- function(x) 1-abs(x)
fun2 <- function(x) -(1-abs(x))
plot (fun1, -1, 1,xlim=c(-2,2),ylim=c(-2,2),pty="s",col=2);abline(h=0,v=0)
plot (fun2, -1, 1,col=2,add=TRUE)
Here's an approach using ggplot
.
x = seq(-1, 1, by = 0.01)
y = 1 - abs(x)
p1 = qplot(x, geom = 'blank') +
geom_point(aes(y = y), colour = 'blue') +
geom_point(aes(y = -y), colour = 'red')
x <- (-1):1
y <- 1 - abs(x)
plot(x, y, type="l")
lines(x, -y)
精彩评论