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

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

2019-01-31

Cómo controlar la dispersión de puntos dentro de un diagrama de violín con ggplot2

Problem

Queremos controlar la dispersión de puntos dentro de un diagrama de violín con ggplot2.

library(tidyverse)
p <- ggplot(mpg, aes(class, hwy))
p + geom_violin() + geom_jitter()

Solución

  • Opción 1
  • Ampliamos la anchura del diagrama de violín (width = 1.3), y jugamos con la transparencia y la variación horizontal de geom_ jitter con (width = .02). No es una opción enteramente satisfactoria. Al restringir la variación horizontal de geom_ jitter, limitamos la propia finalidad de la función que es evitar la superposición de puntos.

    p + geom_violin(width = 1.3) + geom_jitter(alpha = 0.2, width = .02)
    
  • Opción 2
  • Empleamos la función geom_quasirandom del paquete geom_beeswarm:

    The quasirandom geom is a convenient means to offset points within categories to reduce overplotting. Uses the vipor package

    library(ggbeeswarm)
    p + geom_violin(width = 1.3) + geom_quasirandom(alpha = 0.2, width = 0.2)
    

Entradas relacionadas

Referencias

How to restrain scattered jitter points within a violin plot using ggplot2

Problem

We would like to restrain the scattered jitter points within a violin plot using ggplot2.

library(tidyverse)
p <- ggplot(mpg, aes(class, hwy))
p + geom_violin() + geom_jitter()

Solution

  • Option 1
  • Not a completely satisfactory option, because by restricting the horizontal jitter we defeat the purpose of handling overplotting. But we can enlarge the width of the violin plots (width = 1.3), and play with alpha for transparency and limit the horizontal jitter (width = .02).

    p + geom_violin(width = 1.3) + geom_jitter(alpha = 0.2, width = .02)
    
  • Option 2
  • Using the function geom_quasirandom from package geom_beeswarm:

    The quasirandom geom is a convenient means to offset points within categories to reduce overplotting. Uses the vipor package

    library(ggbeeswarm)
    p + geom_violin(width = 1.3) + geom_quasirandom(alpha = 0.2, width = 0.2)
    

Related posts

References

Nube de datos