library(dplyr)
##
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
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
#install.packages("MASS")
#install.packages("UsingR")
library(UsingR)
## Cargando paquete requerido: MASS
##
## Adjuntando el paquete: 'MASS'
## The following object is masked from 'package:plotly':
##
## select
## The following object is masked from 'package:dplyr':
##
## select
## Cargando paquete requerido: HistData
## Cargando paquete requerido: Hmisc
##
## Adjuntando el paquete: 'Hmisc'
## The following object is masked from 'package:plotly':
##
## subplot
## The following objects are masked from 'package:dplyr':
##
## src, summarize
## The following objects are masked from 'package:base':
##
## format.pval, units
library(MASS)
data(UScereal)
data(brightness)
head(UScereal)
## mfr calories protein fat sodium fibre
## 100% Bran N 212.1212 12.121212 3.030303 393.9394 30.303030
## All-Bran K 212.1212 12.121212 3.030303 787.8788 27.272727
## All-Bran with Extra Fiber K 100.0000 8.000000 0.000000 280.0000 28.000000
## Apple Cinnamon Cheerios G 146.6667 2.666667 2.666667 240.0000 2.000000
## Apple Jacks K 110.0000 2.000000 0.000000 125.0000 1.000000
## Basic 4 G 173.3333 4.000000 2.666667 280.0000 2.666667
## carbo sugars shelf potassium vitamins
## 100% Bran 15.15152 18.18182 3 848.48485 enriched
## All-Bran 21.21212 15.15151 3 969.69697 enriched
## All-Bran with Extra Fiber 16.00000 0.00000 3 660.00000 enriched
## Apple Cinnamon Cheerios 14.00000 13.33333 1 93.33333 enriched
## Apple Jacks 11.00000 14.00000 2 30.00000 enriched
## Basic 4 24.00000 10.66667 3 133.33333 enriched
names(UScereal)
## [1] "mfr" "calories" "protein" "fat" "sodium" "fibre"
## [7] "carbo" "sugars" "shelf" "potassium" "vitamins"
data("UScereal")
cereal_data <- UScereal
p<-ggplot(cereal_data, aes(x = shelf, fill = mfr)) +
geom_bar(position = "dodge") +
labs(title = "Number of Cereals per Shelf by Mfr",
x = "Shelf",
y = "mfr") +
theme_minimal()
p_interactive<- ggplotly(p)
p_interactive
x<-ggplot(UScereal, aes(x = fat, y = vitamins)) +
geom_point() +
labs(title = "Fat vs. Vitamins",
x = "Fat (g)",
y = "Vitamins") +
theme_minimal()
x_interactive<- ggplotly(x)
x_interactive
a<-ggplot(UScereal, aes(x = shelf, y = fat)) +
geom_boxplot() +
labs(title = "Fat Content by Shelf",
x = "Shelf",
y = "Fat (g)") +
theme_minimal()
a_interactive<- ggplotly(a)
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?
a_interactive
s<-ggplot(UScereal, aes(x = carbo, y = sugars)) +
geom_point(color="red") +
labs(title = "Carbohydrates vs. Sugars",
x = "Carbohydrates (g)",
y = "Sugars (g)") +
theme_minimal()
s_interactive<- ggplotly(s)
s_interactive
w<-ggplot(UScereal, aes(x = mfr, y = fibre)) +
geom_boxplot() +
labs(title = "Fibre Content by Manufacturer",
x = "Manufacturer",
y = "Fibre (g)") +
theme_minimal()
w_interactive<- ggplotly(w)
w_interactive
v<-ggplot(UScereal, aes(x = sodium, y = sugars)) +
geom_point(color="blue",size=2) +
labs(title = "Sodium vs. Sugars",
x = "Sodium (mg)",
y = "Sugars (g)") +
theme_minimal()
v_interactive<- ggplotly(v)
v_interactive
data("mammals")
# Visualización de la relación original con ggplot
p1 <- ggplot(mammals, aes(x = body, y = brain)) +
geom_point(color = "blue") +
labs(title = "Relationship between Body Weight and Brain Weight",
x = "Body Weight (kg)",
y = "Brain Weight (g)") +
theme_minimal()
# Hacer el gráfico interactivo
p1_interactive <- ggplotly(p1)
# Mostrar el gráfico interactivo
p1_interactive
# Transformar los datos utilizando log
mammals$log_body <- log(mammals$body)
mammals$log_brain <- log(mammals$brain)
# Visualización de la relación transformada con ggplot
p2 <- ggplot(mammals, aes(x = log_body, y = log_brain)) +
geom_point(color = "red") +
labs(title = "Log-Transformed Relationship between Body Weight and Brain Weight",
x = "Log of Body Weight (log(kg))",
y = "Log of Brain Weight (log(g))") +
theme_minimal()
# Hacer el gráfico interactivo
p2_interactive <- ggplotly(p2)
# Mostrar el gráfico interactivo
p2_interactive
##El gráfico de dispersión muestra cómo el peso del cerebro de los mamíferos varía con respecto al peso corporal. En general, parece haber una tendencia a que los mamíferos con cuerpos más grandes también tengan cerebros más grandes. Sin embargo, la relación no parece ser perfectamente lineal a simple vista.
data(anorexia)
head(anorexia)
## Treat Prewt Postwt
## 1 Cont 80.7 80.2
## 2 Cont 89.4 80.1
## 3 Cont 91.8 86.4
## 4 Cont 74.0 86.3
## 5 Cont 78.1 76.1
## 6 Cont 88.3 78.1
# Calcular el cambio de peso
anorexia <- anorexia %>%
mutate(weight_change = Postwt - Prewt)
# Resumir el cambio de peso por tipo de tratamiento
summary_by_treatment <- anorexia %>%
group_by(Treat) %>%
summarise(
avg_weight_change = mean(weight_change),
patients_gained = sum(weight_change > 0),
patients_lost = sum(weight_change <= 0)
)
summary_by_treatment
## # A tibble: 3 × 4
## Treat avg_weight_change patients_gained patients_lost
## <fct> <dbl> <int> <int>
## 1 CBT 3.01 18 11
## 2 Cont -0.450 11 15
## 3 FT 7.26 13 4
# Gráfico de caja para visualizar el cambio de peso por tratamiento
s<-ggplot(anorexia, aes(x = Treat, y = weight_change, fill = Treat)) +
geom_boxplot() +
labs(title = "Cambio de Peso por Tipo de Tratamiento",
x = "Tipo de Tratamiento",
y = "Cambio de Peso (Postwt - Prewt)") +
theme_minimal()
s_interactive<- ggplotly(s)
s_interactive
##BASE DE DATOS BRIGHTNESS
j<-ggplot(data.frame(brightness), aes(x = brightness)) +
geom_histogram(aes(y = ..density..), binwidth = 2, fill = "blue", color = "black") +
geom_density(color = "red", size = 1) +
labs(title = "Histogram and Density Plot of Star Brightness",
x = "Brightness",
y = "Density") +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
j_interactive<- ggplotly(j)
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## ℹ The deprecated feature was likely used in the ggplot2 package.
## Please report the issue at <https://github.com/tidyverse/ggplot2/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
j_interactive
# Boxplot de los datos de brillo
f<-ggplot(data.frame(brightness), aes(y = brightness)) +
geom_boxplot(fill = "lightblue") +
labs(title = "Boxplot of Star Brightness",
y = "Brightness") +
theme_minimal()
f_interactive<- ggplotly(f)
f_interactive
Asimetría: La distribución es ligeramente sesgada hacia la derecha.