Pruebas GGplot

Objetivo

Realizar algunos gráficos con ggplot2

Cargar librerías

library(ggplot2)

Funcion matematica a graficar

\[ f(x)=1-\frac {x}{2} \]

Simular algunos datos

datos=data.frame(x=c(-2 ,-1 ,0 ,1 ,2 ,3), y=c(0 ,0 ,1 ,0.5 ,0 ,0))
datos
   x   y
1 -2 0.0
2 -1 0.0
3  0 1.0
4  1 0.5
5  2 0.0
6  3 0.0

Crear una grafica con ggplot

g=ggplot()
g

Agregar los datos

g=ggplot(data = datos,mapping = aes(x = x,y = y))
g

Incorporar puntos al grafico

g=g+geom_point(color="red")
g

Hacer un poligono uniendo los puntos

g=g+geom_line(color="blue")
g

Delimitar la funcion entre 0 y 2