Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
library('ggplot2')
f_circulo <- function()
{
# Generar 500 puntos de 0pi radianes a 2pi radianes,
# lo cuales equivalen a 0 y 360 grados respectivamente.
t <- seq( from = 0, to = 2 * pi, length.out = 1000 )
# Generar un circulo con ecuaciones parametricas
# para un circulo de radio 1 y centro ( 0, 0 ):
#
# x(t) = r cos(t) + j
# y(t) = r sin(t) + k
r <- 1
x <- r * cos(t)
y <- r * sin(t)
data.frame( x = x, y = y )
}
f_cuadrado <- function()
{
# Basado en distancia Chebyshev:
# d = max( | x0 - x1 |, | y0 - y1 | )
#
# Debido x0 y y0 son 0, entonces:
# d = max( | x1 | , | y1 | )
# Asimismo, d debe ser = 1, por lo cual
# se deduce que todo punto (1,y), (x,1)
# compone al cuadrado.
x = c( 1, -1, -1, 1, 1 )
y = c( 1, 1, -1, -1, 1 )
data.frame( x, y )
}
d_circulo <- f_circulo()
d_cuadrado <- f_cuadrado()
# Especificaciones para lineas, flechas y texto
d_specs <- data.frame( t_x = c( 0.5, 0 )
, t_y = c( 0.1, 1.2 )
, s_x = c( 0, 1 )
, s_y = c( 0, 1.1 )
, s_xend = c( 1, -1 )
, s_yend = c( 0, 1.1 )
, t_lab = c( 'r', 'l' )
, fig = c( 'Cuadrado', 'Circulo' )
)
# Crear grafica
( ggplot()
+ geom_path( data = d_cuadrado
, aes( x = x
, y = y
)
, col = '#F4D03F'
, size = 1
)
+ geom_path( data = d_circulo
, aes( x = x
, y = y
)
, col = '#C0392B'
, size = 0.8
)
+ geom_segment( data = d_specs
, aes( x = s_x
, y = s_y
, xend = s_xend
, yend = s_yend
, color = fig
)
, arrow = arrow( type = 'closed'
, ends = 'both'
, length = unit( x = 0.25, units = 'cm' )
)
)
+ geom_text( data = d_specs
, aes( x = t_x
, y = t_y
, label = t_lab
, color = fig
)
, size = 4
)
+ labs( x = 'X'
, y = 'Y'
)
+ theme_bw()
+ coord_equal()
+ lims( x = c( -1.3, 1.3 )
, y = c( -1.3, 1.3 )
)
+ scale_color_manual( values = c('#F4D03F','#C0392B') )
+ theme( text = element_text( size = 8 )
, legend.position = 'none'
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment