A continuacion se mostrarán diferentes graficos con R usando la libreria ggplot2.
# library: cargar la libreria
library(ggplot2)
# The iris dataset is provided natively by R
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
# basic scatterplot
#<function>(data_set, elementos_Esteticos(eje_x = , eje_y = ))+
#geometria
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point()
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
geom_point()
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species, shape = Species)) +
geom_point()
Quiero saber cuales son las flores con el sepalo mas grande.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Sepal.Width, shape = Species)) +
geom_point()
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Sepal.Width, shape = Species)) +
geom_point() +
facet_wrap(~Species)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Sepal.Width, shape = Species)) +
geom_point() +
facet_wrap(~Species) +
theme_bw()
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Sepal.Width, shape = Species)) +
geom_point() +
facet_wrap(~Species) +
geom_smooth(color = "red", se = FALSE) +
theme_bw()
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Sepal.Width, shape = Species)) +
geom_point() +
facet_wrap(~Species) +
geom_smooth(color = "red", se = FALSE, method = "lm") +
theme_bw()
grafico_completo <-
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Sepal.Width, shape = Species)) +
geom_point() +
facet_wrap(~Species) +
geom_smooth(color = "red", se = FALSE) +
labs(x = "Longitud del sepalo (cm)", y = "Ancho del sepalo (cm)",
title = "Ancho vs largo de sepalos de flores",
caption = "Est. UCEVA 9no semestre") +
theme_bw()
grafico_completo
ggsave("imagenes/grafico_completo.png", plot = grafico_completo, units = "in", dpi = 300, height = 6, width = 9)
library(plotly)
# convirtiendo el grafico en interactivo
ggplotly(grafico_completo)
ggplot(iris, aes(x = Species, y = Petal.Length, fill = Species)) +
geom_boxplot()
cajas <-
ggplot(iris, aes(x = Species, y = Petal.Length, fill = Species)) +
geom_boxplot() +
geom_jitter(width = 0.1) +
labs(title = "Cajas de longitud petalo de Iris", y = "Longitud de petalo (cm)",
x = NULL, caption = "Estudiantes de 9no semestre - UCEVA") +
theme_dark()
ggplotly(cajas)
ggsave("imagenes/cajas.png", plot = cajas, units = "in", dpi = 300, height = 6, width = 9)