EJERCICIO 4.1 PENA

data <- read.delim("~/eurosec.txt")
print(head(data,10))
     V1  V2   V3  V4   V5   V6  V7   V8  V9
1   3.3 0.9 27.6 0.9  8.2 19.1 6.2 26.6 7.2
2   9.2 0.1 21.8 0.6  8.3 14.6 6.5 32.2 7.1
3  10.8 0.8 27.5 0.9  8.9 16.8 6.0 22.6 5.7
4   6.7 1.3 35.8 0.9  7.3 14.4 5.0 22.3 6.1
5  23.2 1.0 20.7 1.3  7.5 16.8 2.8 20.8 6.1
6  15.9 0.6 27.6 0.5 10.0 18.1 1.6 20.1 5.7
7   7.7 3.1 30.8 0.8  9.2 18.5 4.6 19.2 6.2
8   6.3 0.1 22.5 1.0  9.9 18.0 6.8 28.5 6.8
9   2.7 1.4 30.2 1.4  6.9 16.9 5.7 28.3 6.4
10 12.7 1.1 30.2 1.4  9.0 16.8 4.9 16.8 7.0
# Crear la matriz de dispersión
pairs(data, 
      main = "Matriz de Dispersión para EUROSEC", 
      pch = 19,          # Tipo de punto (círculo relleno)
      col = "blue",      # Color de los puntos
      cex = 0.8)         # Tamaño de los puntos

# 2. Usando ggplot2 con GGally
library(GGally)
Warning: package 'GGally' was built under R version 4.4.2
Cargando paquete requerido: ggplot2
Registered S3 method overwritten by 'GGally':
  method from   
  +.gg   ggplot2
ggpairs(data,
        title = "Scatter Plot Matrix - GGally",
        upper = list(continuous = "points", combo = "box"),
        lower = list(continuous = "points", combo = "box")) +
  theme_minimal()

# 3. Usando lattice
library(lattice)
splom(data,
      main = "Scatter Plot Matrix - Lattice",
      pch = 16, col = "green")

# 4. Usando plotly (interactivo)
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
plot_ly(data = data, type = "splom",
        dimensions = lapply(names(data), function(x) list(label = x, values = data[[x]])),
        marker = list(color = "purple")) %>%
  layout(title = "Scatter Plot Matrix - Plotly")
library(echarts4r)
Warning: package 'echarts4r' was built under R version 4.4.2
# Scatter plot matrix básico
data %>%
  e_charts(V1) %>%
  e_scatter(V2, name = "V1 vs V2") %>%
  e_scatter(V3, name = "V1 vs V3") %>%
  e_title("Scatter Plot - echarts4r") %>%
  e_theme("dark") %>%
  e_legend()
library(ggvis)
Warning: package 'ggvis' was built under R version 4.4.2

Adjuntando el paquete: 'ggvis'
The following objects are masked from 'package:plotly':

    add_data, hide_legend
The following object is masked from 'package:ggplot2':

    resolution
# Scatter plot básico interactivo
data %>%
  ggvis(~V1, ~V2) %>%
  layer_points(fill := "blue", opacity := 0.6) %>%
  add_axis("x", title = "V1") %>%
  add_axis("y", title = "V2") %>%
  set_options(width = 600, height = 400)
# No tiene scatter plot matrix directo, pero puedes combinar manualmente
data %>%
  ggvis(~V1, ~V2) %>%
  layer_points(fill := "blue") %>%
  add_axis("x", title = "V1") %>%
  add_axis("y", title = "V2")
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Leer los datos desde el archivo eurosec.txt
data = pd.read_csv("eurosec.txt", sep="\t")

# Crear la matriz de dispersión con seaborn
sns.pairplot(data, 
             vars=["V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9"], 
             plot_kws={"s": 50, "color": "blue", "marker": "o"},  # Tamaño y color de los puntos
             diag_kind="hist")  # Histogramas en la diagonal

# Ajustar el título
plt.suptitle("Matriz de Dispersión para EUROSEC", y=1.02)

# Mostrar la gráfica
plt.show()

import altair as alt
import pandas as pd


matrix = alt.Chart(data).mark_point().encode(
    x=alt.X(alt.repeat("column"), type='quantitative'),
    y=alt.Y(alt.repeat("row"), type='quantitative'),
    color=alt.value('purple')
).properties(
    width=50,
    height=50
).repeat(
    row=['V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9'],
    column=['V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9']
).interactive()
matrix.display()
alt.RepeatChart(...)
from plotnine import *
import pandas as pd



# Scatter plot básico
p = (ggplot(data, aes(x='V1', y='V2'))
     + geom_point(color='blue', size=2, alpha=0.5)
     + ggtitle("Scatter Plot - Plotnine")
     + theme_minimal())
p.draw()
# Scatter plot matrix (manual con facet_grid)
p_matrix = (ggplot(data.melt(id_vars=['V1']), aes(x='V1', y='value'))
            + geom_point(color='purple', size=1, alpha=0.5)
            + facet_grid('variable ~ .')
            + ggtitle("Partial Scatter Matrix - Plotnine")
            + theme_minimal())
p_matrix.draw()
import pandas as pd
from pandas.plotting import scatter_matrix



# Crear la matriz de dispersión
scatter_matrix(data, 
               alpha=0.5,        # Transparencia
               figsize=(12, 12), # Tamaño del gráfico
               diagonal='hist',  # Histogramas en la diagonal
               c='purple',       # Color de los puntos
               s=10)             # Tamaño de los puntos
array([[<Axes: xlabel='V1', ylabel='V1'>,
        <Axes: xlabel='V2', ylabel='V1'>,
        <Axes: xlabel='V3', ylabel='V1'>,
        <Axes: xlabel='V4', ylabel='V1'>,
        <Axes: xlabel='V5', ylabel='V1'>,
        <Axes: xlabel='V6', ylabel='V1'>,
        <Axes: xlabel='V7', ylabel='V1'>,
        <Axes: xlabel='V8', ylabel='V1'>,
        <Axes: xlabel='V9', ylabel='V1'>],
       [<Axes: xlabel='V1', ylabel='V2'>,
        <Axes: xlabel='V2', ylabel='V2'>,
        <Axes: xlabel='V3', ylabel='V2'>,
        <Axes: xlabel='V4', ylabel='V2'>,
        <Axes: xlabel='V5', ylabel='V2'>,
        <Axes: xlabel='V6', ylabel='V2'>,
        <Axes: xlabel='V7', ylabel='V2'>,
        <Axes: xlabel='V8', ylabel='V2'>,
        <Axes: xlabel='V9', ylabel='V2'>],
       [<Axes: xlabel='V1', ylabel='V3'>,
        <Axes: xlabel='V2', ylabel='V3'>,
        <Axes: xlabel='V3', ylabel='V3'>,
        <Axes: xlabel='V4', ylabel='V3'>,
        <Axes: xlabel='V5', ylabel='V3'>,
        <Axes: xlabel='V6', ylabel='V3'>,
        <Axes: xlabel='V7', ylabel='V3'>,
        <Axes: xlabel='V8', ylabel='V3'>,
        <Axes: xlabel='V9', ylabel='V3'>],
       [<Axes: xlabel='V1', ylabel='V4'>,
        <Axes: xlabel='V2', ylabel='V4'>,
        <Axes: xlabel='V3', ylabel='V4'>,
        <Axes: xlabel='V4', ylabel='V4'>,
        <Axes: xlabel='V5', ylabel='V4'>,
        <Axes: xlabel='V6', ylabel='V4'>,
        <Axes: xlabel='V7', ylabel='V4'>,
        <Axes: xlabel='V8', ylabel='V4'>,
        <Axes: xlabel='V9', ylabel='V4'>],
       [<Axes: xlabel='V1', ylabel='V5'>,
        <Axes: xlabel='V2', ylabel='V5'>,
        <Axes: xlabel='V3', ylabel='V5'>,
        <Axes: xlabel='V4', ylabel='V5'>,
        <Axes: xlabel='V5', ylabel='V5'>,
        <Axes: xlabel='V6', ylabel='V5'>,
        <Axes: xlabel='V7', ylabel='V5'>,
        <Axes: xlabel='V8', ylabel='V5'>,
        <Axes: xlabel='V9', ylabel='V5'>],
       [<Axes: xlabel='V1', ylabel='V6'>,
        <Axes: xlabel='V2', ylabel='V6'>,
        <Axes: xlabel='V3', ylabel='V6'>,
        <Axes: xlabel='V4', ylabel='V6'>,
        <Axes: xlabel='V5', ylabel='V6'>,
        <Axes: xlabel='V6', ylabel='V6'>,
        <Axes: xlabel='V7', ylabel='V6'>,
        <Axes: xlabel='V8', ylabel='V6'>,
        <Axes: xlabel='V9', ylabel='V6'>],
       [<Axes: xlabel='V1', ylabel='V7'>,
        <Axes: xlabel='V2', ylabel='V7'>,
        <Axes: xlabel='V3', ylabel='V7'>,
        <Axes: xlabel='V4', ylabel='V7'>,
        <Axes: xlabel='V5', ylabel='V7'>,
        <Axes: xlabel='V6', ylabel='V7'>,
        <Axes: xlabel='V7', ylabel='V7'>,
        <Axes: xlabel='V8', ylabel='V7'>,
        <Axes: xlabel='V9', ylabel='V7'>],
       [<Axes: xlabel='V1', ylabel='V8'>,
        <Axes: xlabel='V2', ylabel='V8'>,
        <Axes: xlabel='V3', ylabel='V8'>,
        <Axes: xlabel='V4', ylabel='V8'>,
        <Axes: xlabel='V5', ylabel='V8'>,
        <Axes: xlabel='V6', ylabel='V8'>,
        <Axes: xlabel='V7', ylabel='V8'>,
        <Axes: xlabel='V8', ylabel='V8'>,
        <Axes: xlabel='V9', ylabel='V8'>],
       [<Axes: xlabel='V1', ylabel='V9'>,
        <Axes: xlabel='V2', ylabel='V9'>,
        <Axes: xlabel='V3', ylabel='V9'>,
        <Axes: xlabel='V4', ylabel='V9'>,
        <Axes: xlabel='V5', ylabel='V9'>,
        <Axes: xlabel='V6', ylabel='V9'>,
        <Axes: xlabel='V7', ylabel='V9'>,
        <Axes: xlabel='V8', ylabel='V9'>,
        <Axes: xlabel='V9', ylabel='V9'>]], dtype=object)
plt.suptitle('Scatter Plot Matrix - Pandas', y=1.02)
plt.show()

import pandas as pd

# Leer los datos

# Crear el scatter plot
data.plot.scatter(x='V1', y='V2', 
                 c='purple',    # Color
                 s=50,          # Tamaño de los puntos
                 alpha=0.5,     # Transparencia
                 figsize=(8, 6),# Tamaño del gráfico
                 title='Scatter Plot - Pandas')
plt.grid(True, linestyle='--', alpha=0.7)  # Opcional: rejilla
plt.show()

import pandas as pd
from pandas.plotting import scatter_matrix

# Leer los datos


# Crear la matriz de dispersión
scatter_matrix(data, 
               alpha=0.5,        # Transparencia
               figsize=(12, 12), # Tamaño del gráfico
               diagonal='hist',  # Histogramas en la diagonal
               c='purple',       # Color de los puntos
               s=10)             # Tamaño de los puntos
array([[<Axes: xlabel='V1', ylabel='V1'>,
        <Axes: xlabel='V2', ylabel='V1'>,
        <Axes: xlabel='V3', ylabel='V1'>,
        <Axes: xlabel='V4', ylabel='V1'>,
        <Axes: xlabel='V5', ylabel='V1'>,
        <Axes: xlabel='V6', ylabel='V1'>,
        <Axes: xlabel='V7', ylabel='V1'>,
        <Axes: xlabel='V8', ylabel='V1'>,
        <Axes: xlabel='V9', ylabel='V1'>],
       [<Axes: xlabel='V1', ylabel='V2'>,
        <Axes: xlabel='V2', ylabel='V2'>,
        <Axes: xlabel='V3', ylabel='V2'>,
        <Axes: xlabel='V4', ylabel='V2'>,
        <Axes: xlabel='V5', ylabel='V2'>,
        <Axes: xlabel='V6', ylabel='V2'>,
        <Axes: xlabel='V7', ylabel='V2'>,
        <Axes: xlabel='V8', ylabel='V2'>,
        <Axes: xlabel='V9', ylabel='V2'>],
       [<Axes: xlabel='V1', ylabel='V3'>,
        <Axes: xlabel='V2', ylabel='V3'>,
        <Axes: xlabel='V3', ylabel='V3'>,
        <Axes: xlabel='V4', ylabel='V3'>,
        <Axes: xlabel='V5', ylabel='V3'>,
        <Axes: xlabel='V6', ylabel='V3'>,
        <Axes: xlabel='V7', ylabel='V3'>,
        <Axes: xlabel='V8', ylabel='V3'>,
        <Axes: xlabel='V9', ylabel='V3'>],
       [<Axes: xlabel='V1', ylabel='V4'>,
        <Axes: xlabel='V2', ylabel='V4'>,
        <Axes: xlabel='V3', ylabel='V4'>,
        <Axes: xlabel='V4', ylabel='V4'>,
        <Axes: xlabel='V5', ylabel='V4'>,
        <Axes: xlabel='V6', ylabel='V4'>,
        <Axes: xlabel='V7', ylabel='V4'>,
        <Axes: xlabel='V8', ylabel='V4'>,
        <Axes: xlabel='V9', ylabel='V4'>],
       [<Axes: xlabel='V1', ylabel='V5'>,
        <Axes: xlabel='V2', ylabel='V5'>,
        <Axes: xlabel='V3', ylabel='V5'>,
        <Axes: xlabel='V4', ylabel='V5'>,
        <Axes: xlabel='V5', ylabel='V5'>,
        <Axes: xlabel='V6', ylabel='V5'>,
        <Axes: xlabel='V7', ylabel='V5'>,
        <Axes: xlabel='V8', ylabel='V5'>,
        <Axes: xlabel='V9', ylabel='V5'>],
       [<Axes: xlabel='V1', ylabel='V6'>,
        <Axes: xlabel='V2', ylabel='V6'>,
        <Axes: xlabel='V3', ylabel='V6'>,
        <Axes: xlabel='V4', ylabel='V6'>,
        <Axes: xlabel='V5', ylabel='V6'>,
        <Axes: xlabel='V6', ylabel='V6'>,
        <Axes: xlabel='V7', ylabel='V6'>,
        <Axes: xlabel='V8', ylabel='V6'>,
        <Axes: xlabel='V9', ylabel='V6'>],
       [<Axes: xlabel='V1', ylabel='V7'>,
        <Axes: xlabel='V2', ylabel='V7'>,
        <Axes: xlabel='V3', ylabel='V7'>,
        <Axes: xlabel='V4', ylabel='V7'>,
        <Axes: xlabel='V5', ylabel='V7'>,
        <Axes: xlabel='V6', ylabel='V7'>,
        <Axes: xlabel='V7', ylabel='V7'>,
        <Axes: xlabel='V8', ylabel='V7'>,
        <Axes: xlabel='V9', ylabel='V7'>],
       [<Axes: xlabel='V1', ylabel='V8'>,
        <Axes: xlabel='V2', ylabel='V8'>,
        <Axes: xlabel='V3', ylabel='V8'>,
        <Axes: xlabel='V4', ylabel='V8'>,
        <Axes: xlabel='V5', ylabel='V8'>,
        <Axes: xlabel='V6', ylabel='V8'>,
        <Axes: xlabel='V7', ylabel='V8'>,
        <Axes: xlabel='V8', ylabel='V8'>,
        <Axes: xlabel='V9', ylabel='V8'>],
       [<Axes: xlabel='V1', ylabel='V9'>,
        <Axes: xlabel='V2', ylabel='V9'>,
        <Axes: xlabel='V3', ylabel='V9'>,
        <Axes: xlabel='V4', ylabel='V9'>,
        <Axes: xlabel='V5', ylabel='V9'>,
        <Axes: xlabel='V6', ylabel='V9'>,
        <Axes: xlabel='V7', ylabel='V9'>,
        <Axes: xlabel='V8', ylabel='V9'>,
        <Axes: xlabel='V9', ylabel='V9'>]], dtype=object)
plt.suptitle('Scatter Plot Matrix - Pandas', y=1.02)
plt.show()