-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGEOMS.R
149 lines (119 loc) · 4.46 KB
/
GEOMS.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 06-Octubre-2024
# Uso de Geometrias en ggplot
# Prof:Aline Pingarroni
# Taller Introduccion al lenguaje R
# Cargar librerías necesarias
library(ggplot2)
library(dplyr)
library(ggridges)
library(tidyr)
#1 Cargar base de datos
data <- read.csv("data_domingo.csv", na.strings = c("NA"))
dir()
summary(data)
dim(data)
names(data)
#2 Agregar columnas log-transformadas
data$logBody_mass_kg <- log(data$Body_mass_kg)
data$logHome_Range_km2 <- log(data$Home_Range_km2)
#3 Gráfico de densidad para una variable continua
ggplot(data=data, aes(x=logHome_Range_km2)) +
geom_density()
#3.1 Histograma de una variable continua
ggplot(data=data, aes(x=logHome_Range_km2)) +
geom_histogram()
# Guardar el gráfico en un objeto y añadir histogramas
E <- ggplot(data=data, aes(x=logHome_Range_km2))
E
E + geom_histogram()
#3.2 Gráfico de dispersion para dos variables continuas
ggplot(data=data, aes(x=logBody_mass_kg, y=logHome_Range_km2)) +
geom_point()
#3.3 Gráfico de área con más variables
ggplot(data=data, aes(x=precip_annual, y=solar_rad_mean, fill=Sex)) +
geom_area()
# Eliminar filas con NA en las columnas usadas en el gráfico
data_filtered <- drop_na(data, precip_annual, solar_rad_mean, Sex)
ggplot(data=data_filtered, aes(x=precip_annual, y=solar_rad_mean, fill=Sex)) +
geom_area()
# Filtrar observaciones con datos no faltantes y seleccionar 50 observaciones aleatorias
data_filtered <- data %>%
filter(!is.na(logBody_mass_kg) & !is.na(logHome_Range_km2)) %>%
sample_n(50)
# Gráfico de etiquetas mostrando las especies de las 50 observaciones seleccionadas
ggplot(data=data_filtered, aes(x=logBody_mass_kg, y=logHome_Range_km2)) +
geom_label(aes(label=Species))
# Gráfico de texto
ggplot(data=data_filtered, aes(x=logBody_mass_kg, y=logHome_Range_km2)) +
geom_text(aes(label=Species))
# Gráfico de hexágonos
ggplot(data=data, aes(x=logBody_mass_kg, y=logHome_Range_km2)) +
geom_hex()
# Gráfico de barras para una variable categórica
summary(data)
unique(data$Life_Stage)
data_filtered <- drop_na(data, Life_Stage)
ggplot(data=data_filtered, aes(x= Life_Stage)) +
geom_bar()
# Gráfico de columna para una variable categórica y una continua
ggplot(data=data_filtered, aes(x=Sex, y=Body_mass_kg )) +
geom_col()
# Boxplot para una variable categórica y una continua
# Filtrar observaciones con datos no faltantes y seleccionar 50 observaciones aleatorias
data_filtered <- data %>%
filter(!is.na(logBody_mass_kg) & !is.na(logHome_Range_km2)) %>%
sample_n(50)
ggplot(data=data_filtered, aes(x=Sex, y=logHome_Range_km2)) +
geom_boxplot(fill="red", color="yellow") +
geom_jitter()
# Violin plot
ggplot(data=data_filtered, aes(x=Sex, y=logHome_Range_km2)) +
geom_violin(fill="red", color="yellow") +
geom_jitter()
# Ajustes de posición para gráfico de barras
summary(data)
ggplot(data=data, aes(x=Sex, fill=Locomotion)) +
geom_bar()
ggplot(data=data, aes(x=Sex, fill=Locomotion)) +
geom_bar(position="dodge")
ggplot(data=data, aes(x=Sex, fill=Locomotion)) +
geom_bar(position="fill")
ggplot(data=data, aes(x=Sex, fill=Locomotion)) +
geom_bar(position="stack")
# Sistema de coordenadas: coord_flip() para invertir ejes
ggplot(data=data, aes(x=Sex, fill=Locomotion)) +
geom_bar() +
coord_flip()
# coord_polar() para convertir el gráfico de barras en un gráfico de pastel
ggplot(data=data, aes(x=Sex, fill=Locomotion)) +
geom_bar() +
coord_polar()
# Gráfico de puntos con límites ajustados
ggplot(data=data, aes(x=logBody_mass_kg, y=logHome_Range_km2)) +
geom_point()
ggplot(data=data, aes(x=logBody_mass_kg, y=logHome_Range_km2)) +
geom_point() +
coord_cartesian(ylim=c(0, 1), xlim=c(-5, -2.5))
## Contar la frecuencia de cada sexo
sex_counts <- data %>%
count(Sex) %>%
mutate(percentage = n / sum(n) * 100)
# Crear el gráfico de pastel basado en la variable 'Sex'
ggplot(sex_counts, aes(x = "", y = percentage, fill = Sex)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y") + # Transformar en un gráfico de pastel
theme_void() + # Eliminar ejes y fondo
geom_text(aes(label = paste0(round(percentage, 1), "%")),
position = position_stack(vjust = 0.5))
##########ETIQUETADO #########################
o<- ggplot(data=data, aes(x=Sex, fill=Locomotion)) +
geom_bar()
o
# Modificar los títulos y etiquetas
o+labs(
title = "Frecuencia del sexo",
subtitle = "Mamiferos",
x = "Sexo", # Etiqueta del eje X
y = "Frecuencia", # Etiqueta del eje Y
caption = "Fuente: HomeRange: A global database of mammalian home ranges"
)