Mostrando entradas con la etiqueta curve. Mostrar todas las entradas
Mostrando entradas con la etiqueta curve. Mostrar todas las entradas

2021-03-18

Cómo representar funciones en R

Problema

Queremos representar funciones en R.

Solución

En nuestro ejemplo queremos representar la siguiente ecuación de segundo grado: 2x2 + 20x + 3 = 0

  • Opción 1: base package
  • f <- function(x) {
      x ^ 2 + 20 * x + 3
    }
    curve(expr = f, from = -100, to = 100)
    
  • Opción 2: ggplot2
  • library(ggplot2)
    ggplot(data.frame(x = c(-100, 100)), aes(x)) +
      stat_function(
        fun = function(x) {
          x ^ 2 + 20 * x + 3
        }
      ) +
      geom_hline(yintercept = 0) +
      geom_vline(xintercept = 0)
    
     

Notas

Si queremos añadir más funciones:

  • Opción 1: Base package
  • f1 <- function(x) {
      x ^ 2 + 20 * x + 3
    }
    f2 <- function(x) {
      x ^ 2 + 50 * x + 100
    }
    curve(expr = f1, from = -100, to = 100)
    curve(expr = f2, from = -100, to = 100, col  = 2, add = TRUE)
    
  • Opción 2: ggplot2
  • ggplot(data.frame(x = c(-100, 100)), aes(x)) +
      stat_function(
        fun = function(x) {
          x ^ 2 + 20 * x + 3
        }
      ) +
      stat_function(
        fun = function(x) {
          x ^ 2 + 50 * x + 100
        },
        colour = "red"
      ) +
      geom_hline(yintercept = 0) +
      geom_vline(xintercept = 0)
     

Entradas relacionadas

Referencias

2021-03-16

How to plot functions in R

Problem

We would like to plot functions in R.

Solution

In our example we will plot the following quadratic equation: 2x2 + 20x + 3 = 0

  • Option 1: base package
  • f <- function(x) {
      x ^ 2 + 20 * x + 3
    }
    curve(expr = f, from = -100, to = 100)
    
  • Option 2: ggplot2
  • library(ggplot2)
    ggplot(data.frame(x = c(-100, 100)), aes(x)) +
      stat_function(
        fun = function(x) {
          x ^ 2 + 20 * x + 3
        }
      ) +
      geom_hline(yintercept = 0) +
      geom_vline(xintercept = 0)
    
     

Notes

If we want to add more functions:

  • Option 1: Base package
  • f1 <- function(x) {
      x ^ 2 + 20 * x + 3
    }
    f2 <- function(x) {
      x ^ 2 + 50 * x + 100
    }
    curve(expr = f1, from = -100, to = 100)
    curve(expr = f2, from = -100, to = 100, col  = 2, add = TRUE)
    
  • Option 2: ggplot2
  • ggplot(data.frame(x = c(-100, 100)), aes(x)) +
      stat_function(
        fun = function(x) {
          x ^ 2 + 20 * x + 3
        }
      ) +
      stat_function(
        fun = function(x) {
          x ^ 2 + 50 * x + 100
        },
        colour = "red"
      ) +
      geom_hline(yintercept = 0) +
      geom_vline(xintercept = 0)
     

References

2019-08-10

Cómo representar una curva logarítmica en R

Problema

Queremos ajustar una curva logarítmica a un diagrama de dispersión.

library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
  geom_point()

Solución

Paquete base

plot(hwy ~ displ, mpg)
logEstimate <- lm(hwy ~ log(displ), data = mpg)
curve(coef(logEstimate)[1] + coef(logEstimate)[2] * log(x), add = TRUE)
ggplot2

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  stat_smooth(method = "lm", formula = y ~ log(x))

Referencias

How to plot a log curve in R

Problem

We want to fit a logarithmic curve to a scatterplot.

library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
  geom_point()

Solution

Base package

plot(hwy ~ displ, mpg)
logEstimate <- lm(hwy ~ log(displ), data = mpg)
curve(coef(logEstimate)[1] + coef(logEstimate)[2] * log(x), add = TRUE)
ggplot2

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  stat_smooth(method = "lm", formula = y ~ log(x))

References

Nube de datos