library("ggplot2")
library("plotly")
##
## Adjuntando el paquete: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
#ggplot
data(mtcars)
ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point()
#Personalizacion ##se pude agregar mas capas y personalizar el
gráfico
ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_point(color = "blue", size = 3) + # Cambiar color y tamaño de los puntos
labs(title = "Peso vs. Consumo de Combustible", # Título del gráfico
x = "Peso (1000 lbs)", # Etiqueta del eje x
y = "Millas por Galón") # Etiqueta del eje y
#Facetado ##para dividir el grafico en subgraficos basados en la
variable, usa facet_wrap o facet_grip:
ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_point() +
facet_wrap(~ cyl) # Crear subgráficos por número de cilindros
#Temas ##sirve para ajustar la apriencia del grafico
ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme_minimal() # Usar un tema minimalista
g<-ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_point(aes(color = factor(cyl)), size = 3) + # Color por número de cilindros
labs(title = "Peso vs. Consumo de Combustible",
x = "Peso (1000 lbs)",
y = "Millas por Galón",
color = "Cilindros") + # Leyenda de colores
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5)) # Centrar el título
g
#Grafico interactivo
interactive_plot <- ggplotly(g)
interactive_plot