#Visualizaciòn de Graficos Histogramas Barras Sectores-Cirulares

1: cargar data

#Histogramas de la variable ¿Cuando se usa un histigrama y cuando uno de barras?

edad<-c(20, 21, 21, 18, 17, 19, 20, 19, 24, 18, 22, 20, 19, 21, 20, 19, 20, 19)
 #el histograma grafica el rango
#cuando no esta el paquete

hist(edad)

hist
## function (x, ...) 
## UseMethod("hist")
## <bytecode: 0x0000018b7bf02378>
## <environment: namespace:graphics>

Como guardar una imagen

Para guardar la imagen tenemos que buscar en plots; dar click en exportar; luego save plot a image para elegir en que formato guardar dar click en directorio ; elegir carpeta.

para copiar la imagen le da click derecho en zoom y click en copiar y se pega en un word

Como calcular la frecuencia si no la tengo

Frecuencia_Absoluta <- table (edad)
print(Frecuencia_Absoluta)
## edad
## 17 18 19 20 21 22 24 
##  1  2  5  5  3  1  1

Diagrama de barras

al diagrama de barras si debo definir los ejes la barra grafia la frecuencia absoluta es decir la cantidad de datos que se repiteel dato la difeerenci de diagrama de barras a diagrama de tortas es para representar graficos de porcentajes

barplot(edad)

# Cargar data de url

url <- 'https://raw.githubusercontent.com/fhernanb/datos/master/babies.txt'

datos_url<- read.table(url, header = TRUE, sep = '\t')

#base de datos estudia la Natalidad de Bebes

bwt: peso de bebe al nacer en onza mas cercana

gestación: Duración del embarazo en días, calculado a partir del primer día del último período menstrual normal.

paridad: Indicador de si el bebé es el primogénito o desconocido.

edad: edad de la madre en el momento de la concepción, en años.

altura: Altura de la madre, en pulgadas.

peso: peso de la madre antes del embarazo, en libras.

fumar: estado de tabaquismo Indicador de si la madre fuma o no.

#Como contar variables

ncol(datos_url)
## [1] 7

#para ver solo los primeros 8 datos de la data

head(datos_url,n=8)
##   bwt gestation     parity age height weight smoke
## 1 120       284 First born  27     62    100   Not
## 2 113       282 First born  33     64    135   Not
## 3 128       279 First born  28     64    115   Yes
## 4 123        NA First born  36     69    190   Not
## 5 108       282 First born  23     67    125   Yes
## 6 136       286 First born  25     62     93   Not
## 7 138       244 First born  33     62    178   Not
## 8 132       245 First born  23     65    140   Not
summary(datos_url)
##       bwt          gestation        parity               age       
##  Min.   : 55.0   Min.   :148.0   Length:1236        Min.   :15.00  
##  1st Qu.:108.8   1st Qu.:272.0   Class :character   1st Qu.:23.00  
##  Median :120.0   Median :280.0   Mode  :character   Median :26.00  
##  Mean   :119.6   Mean   :279.3                      Mean   :27.26  
##  3rd Qu.:131.0   3rd Qu.:288.0                      3rd Qu.:31.00  
##  Max.   :176.0   Max.   :353.0                      Max.   :45.00  
##                  NA's   :13                         NA's   :2      
##      height          weight         smoke          
##  Min.   :53.00   Min.   : 87.0   Length:1236       
##  1st Qu.:62.00   1st Qu.:114.8   Class :character  
##  Median :64.00   Median :125.0   Mode  :character  
##  Mean   :64.05   Mean   :128.6                     
##  3rd Qu.:66.00   3rd Qu.:139.0                     
##  Max.   :72.00   Max.   :250.0                     
##  NA's   :22      NA's   :36
# como sacar los 4 ultimos datos de la baase

#como obtener resumen de la data

str(datos_url)
## 'data.frame':    1236 obs. of  7 variables:
##  $ bwt      : int  120 113 128 123 108 136 138 132 120 143 ...
##  $ gestation: int  284 282 279 NA 282 286 244 245 289 299 ...
##  $ parity   : chr  "First born" "First born" "First born" "First born" ...
##  $ age      : int  27 33 28 36 23 25 33 23 25 30 ...
##  $ height   : int  62 64 64 69 67 62 62 65 62 66 ...
##  $ weight   : int  100 135 115 190 125 93 178 140 125 136 ...
##  $ smoke    : chr  "Not" "Not" "Yes" "Not" ...

como hallar la meddiana

median(datos_url$bwt)
## [1] 120

#como hallar la moda para variable cuaitativa

mode(datos_url$fumar)
## [1] "NULL"

como hallar la varianza mide la dispersión de los datos en torno a la media

var(datos_url$bwt)
## [1] 332.5682

media

mean(datos_url$bwt)
## [1] 119.5769

es decir que el peso de los bebes tiene mayor dispercion

Desviación estandar

#summary un resumen solamente de la media mediana, moda min, mix, primer quartil y cuarto quartil

summary(datos_url)
##       bwt          gestation        parity               age       
##  Min.   : 55.0   Min.   :148.0   Length:1236        Min.   :15.00  
##  1st Qu.:108.8   1st Qu.:272.0   Class :character   1st Qu.:23.00  
##  Median :120.0   Median :280.0   Mode  :character   Median :26.00  
##  Mean   :119.6   Mean   :279.3                      Mean   :27.26  
##  3rd Qu.:131.0   3rd Qu.:288.0                      3rd Qu.:31.00  
##  Max.   :176.0   Max.   :353.0                      Max.   :45.00  
##                  NA's   :13                         NA's   :2      
##      height          weight         smoke          
##  Min.   :53.00   Min.   : 87.0   Length:1236       
##  1st Qu.:62.00   1st Qu.:114.8   Class :character  
##  Median :64.00   Median :125.0   Mode  :character  
##  Mean   :64.05   Mean   :128.6                     
##  3rd Qu.:66.00   3rd Qu.:139.0                     
##  Max.   :72.00   Max.   :250.0                     
##  NA's   :22      NA's   :36

#quantiles dividido en 5

quantile(datos_url$bwt)
##     0%    25%    50%    75%   100% 
##  55.00 108.75 120.00 131.00 176.00
#si cartegorizo la muestra se tiene
quantile(datos_url$bwt, probs=c(0.25,0.40,0.90))
##    25%    40%    90% 
## 108.75 116.00 142.00
library (dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
## 
## 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
# permite manejar base de datos de manera mas organizada sirve para escribir ggplot que permite hacer graficos en 3d con diferentes colores ademas de hacer tablas bien estructuradas

#para obtener informacion solo de una variable y extraer solo una variable uso %>% pull(bwt)

datos_url %>% pull(bwt) %>% mean
## [1] 119.5769
datos_url %>% pull(gestation) %>% median
## [1] NA

#como cambiar los valores perdidos de la base is.na quita los valores perdidos es decir limpia la data

datos_url$gestation[is.na(datos_url$gestation)] <- mean(datos_url$gestation, na.rm = TRUE)

Hallar promedio edad de las mujeres

datos_url %>%pull(age) %>% mean
## [1] NA

tiene valores perdidos por lo que se va a limpiar la data para esto utilizamos is.na que quita los valores perdidos

datos_url$age[is.na(datos_url$age)]<-mean(datos_url$age, na.rm= )

Para visualizar todos los datos

para visualizar el dato de la persona 6

datos_url[,6]
##    [1] 100 135 115 190 125  93 178 140 125 136 120 124 128  99 154 130 125 125
##   [19] 170 142  93 175 145 182 122 112 106 175 125 132 105 146 123 105 120 130
##   [37] 115  92 101  NA 160 177  NA 119 130 110 150  90 147 119 130 148 110 126
##   [55] 132 130 145 140 116 110  96 118 125 130 125 115 150 137 170 170 130 118
##   [73] 125 120 149 110 107 130 126 103 116 104 135 148 145  NA 117 143 196 113
##   [91] 117 112 104 130 103 100 100 162 110 137 121 120  NA 117 108 140 143 150
##  [109] 125 154  NA 125 136  NA 145 114 109 215 145 170 103 133 130 155 150 150
##  [127] 110 120 154 148 103 110 125 100 147 120 135 145 104 165 107 123 110 127
##  [145] 115 155 125 175 140 250 148 128  NA 132  NA 152 135 104  NA 121 111 138
##  [163] 190 123 125 160 123 105 123 110 109 118 115 105 131 155 170 123 125 120
##  [181] 175 192 116 220 150  NA 117  93  97 135 136 110 124  NA 155 150 168 147
##  [199] 117 110 140 132  97 112  NA 105 110 150 125 150 138 150  99 115 125 130
##  [217] 145 130 125 130 135 121 180 145 136 142 128 120 106 106  NA 117 116 127
##  [235] 135 135 160 120 155 110 190 140 105 125 117 125 128 124 169 125 110 105
##  [253] 110 125 185 142 118 139 160 128 135 148 105 122 140 104 125 132 102 140
##  [271] 103 119 165 115 156 150 120 135 160 140 142 140 118 103 146 126 135 189
##  [289] 157 108 135 122 148 152 140 100 105 105 110 122 125 125 102 125 135 130
##  [307] 130 112 145 117 176  NA 130 112 100 110 135 120 135 123 129 130 136 145
##  [325] 120 120 120 106 135 130 132 110 110 135 127 130 107  NA 103 157 144 130
##  [343] 112 130 130 130 103 110 117 122 110 113  NA 128 132 104 159 115 115 112
##  [361] 135 110 130  NA 145 147 130 112 170 122 125 122 136 143 135 130 121 108
##  [379] 128 105 125 109 100 145 137 150 115 155 112 130 145 124 145 118 100 140
##  [397] 125 134 115  NA 133 105 104 105 117 135 129 120 140 139 116 180 124 143
##  [415] 137 104 132 147 107 110 130 130 135 155 132 114 210 142 138 102 145 102
##  [433] 115 150 110 140 130 120 148  NA 135 108 111  NA 138 130 120 140 127 140
##  [451] 130 115 135 127 124 118 125 165 137 118 140 150 149 127 138  93 125 123
##  [469] 111 133 147  99 125 154 141 115 135  NA  NA 135 155  NA 135 110 130 144
##  [487] 135 135 123 155 130 128 122 118 123 125 120 125 122 162 103 110 111 130
##  [505] 117  95 129 126  NA 175 147 140 180 102 116 110 150 115 120 147 145 110
##  [523] 200 140 130  NA 117 125 180 135 120 109 113 132 110 124 160 160 103 130
##  [541] 128  96 127 120 106 122  89 108 138 125  95 145 140 107 125 135 155 105
##  [559] 102 114 124 137 170 197 171 145 130 165 146 135 134 135 132 105 110 133
##  [577] 132 112 159 135 150 125 105 125 113  98 130 135 119 130 150 127 120 140
##  [595] 129 110 114 130 115 107 155 118 126 115 110 137 115 139 215 140 100 160
##  [613] 107 108 132 116 165 109  98 110 110 115 202 135 112 108 108 118 125 126
##  [631] 112 130 180 145 130  90 100 118 120 145 118 135 129 112 128 155 124 125
##  [649]  NA 165 132 124 135  NA 112 101 117 115 115 135 133 134 119 125 115 110
##  [667] 108  98 105 145 120 140 130 110 107 132 115 118 124  95 116  94 145 125
##  [685] 115 110 102 140 120  94 160 105 120 117 126 175 174 112  NA 144 125 169
##  [703] 135 120 128 135 116 125 140 140 138 128 118 113 105 150 145 153 115 122
##  [721] 127 160 130 198 118 125 110 165 157 120 140 148 126 190 130 100 114 136
##  [739] 117 120 118 100 137 126 120  96 110 182 122 105 130 140 125  NA  98 135
##  [757] 105 125 127 125 120 130  NA 142 105 118 147 106 127 110 104 130 118 111
##  [775] 105 134 140 121 111 120 110 110 160 124 113 145 120  96 130 119 150 124
##  [793] 130 127 100 164 135 155 149 139 130 134 127 110 140 108 124 127 112 120
##  [811] 155 129 132 132 145 124 130 110 108 112 118 135 162 162 130 112 118  91
##  [829] 103 107 100 112 125 134 115 141 118 123 148 125 120 122 126 118 110 108
##  [847] 107 126 120 215 138 170 110 120 150 130 117 107 191 185 150 112 127 110
##  [865] 140 210 111 124 130 133 160 125 143 125  NA 150 100 129 150 110 135 109
##  [883]  NA 125 118 110 132 125 181 123 117 135 130 110 110 140 120 155 114 159
##  [901] 112 116 110 128 136 137 153 130  99 115 125 175 156 107 160 128 130 135
##  [919] 137 125 175 108 137 155 217 126 130 135 156 115 109 105 137 143 123 178
##  [937] 120 134 121 102 115 116 170 160 109 112 120 133 110 130 122 122 121 125
##  [955] 105 112 130 145 145 124 113 120 135 118 118 133 122 125 126 112 137 135
##  [973] 115 135 131 111 110 160 140 149 110 134 155 150 129 122 112 130 128 120
##  [991] 108  98 127 130 145 130 113 156 110 132 140 159 164 114 130 140 127 115
## [1009] 185 124 114 116 112  NA 133 115 115 145 140 127 120 185 130 135 145 130
## [1027] 150 109 110 142 125 132 110 110 120 130 116  90 130 125 112 127 132 128
## [1045]  NA 115 116 145 119 135 117 125 117 120 130 120 125 129 144 100 145 104
## [1063] 110 145 132 125 108 119 118 130 128  97 115 135 142 131 165 122 114 137
## [1081] 107 113 145 110 126 135 111 135 105 113 110 120 121 130 122 127 122 115
## [1099] 104 163 146 113 120 142 124 127 135 122 127 125 135 150 155 120 115 136
## [1117] 115 108 118 115 125 120 140 123  98 118 105 154 118 122 117 150 100 115
## [1135] 118 118 102 127 104  99 107 124 136 142 132 125 106 120 200 113 112 114
## [1153] 117  99 177 145 124 125 134 123 140 130 110 119 104 103 135 180 110 145
## [1171] 150 128 115  95 145 130 103  NA 126 101 113 130 137 112 127 140 110 145
## [1189] 140  89  NA 135 228 160 158 145 127 135 150 170 107 145 130 115 120 113
## [1207] 135 125 156 140 130 103 120 151 103 119 109 145 150 180  95 120 116 136
## [1225] 135 145 102  87 116 121 126 100 120 150 110 129