Search code examples
rplotlogarithm

How to plot logarithm graph with R-plot?


I am trying to make the following graph with R-plot:

enter image description here

I have the following code in R:

x = 0:8
f = log2(x)
plot(x, f, main="Função Logarítmica", xlab="X - Abscissas", ylab="Y - Ordenadas", t='l', ylim=c(-3,3), xlim=c(0,8), col=4, axes=F) 
axis(1, pos=0, at=seq(from=0, to=8, by=0.2))
axis(2, pos=0)   
# Inclui linhas de grade
abline(h=seq(-3,3,0.5),v=seq(-3,8,0.5),lty=3,col="gray", lwd=2)

The graph generated with the R-plot:

enter image description here

How should I make my R graph look like the figure?


Solution

  • I'm not sure what it is you want to change in the figure, but here's a reasonable replica:

    x = seq(0.1, 8, 0.1)
    f = log2(x)
    plot(x, f, main = "Função Exponencial", 
         xlab = "X - Abscissas", ylab = "Y - Ordenadas", t = 'n', 
         ylim= c(-3, 3), xlim = c(0, 8), col = 4, axes = FALSE) 
    
    polygon(x = c(0, 8, 8, 0), y = c(-3, -3, 3, 3), col= "#e3e9ff")
    abline(h = seq(-3, 3, 0.1), v = seq(-3, 8, 0.1), col = "white", lwd = 2)
    lines(x, f, lwd = 2, col = "red3")
    axis(1, pos = 0, at = 0:8)
    axis(2, pos = 0) 
    text(label = c("\u215b", "\u00bc", "\u00bd"),
         c(0.125, 0.25, 0.5), c(0.2, -0.2, -0.2), cex = 1.3)
    
    for(i in seq(-3, 3)) {
      lines(c(0, 2^i, 2^i), c(i, i, 0), lty = 2)
      points(2^i, i, pch = 16, cex = 1.5)
    }
    

    enter image description here