##logaritmos    ver más en https://www.datanovia.com/en/blog/ggplot-log-scale-transformation/ 

library(ggplot2)
p <- ggplot(cars, aes(x = speed, y = dist)) + 
  geom_point()
p
  
# log base 2 escala
p + scale_x_continuous(trans = 'log2')+
  scale_y_continuous(trans = 'log2')

# Log base 10 escala + log ticks (abajo y a la izquierda)
p + scale_x_continuous(trans = 'log10') + 
  scale_y_continuous(trans = 'log10')+
  annotation_logticks(sides="lb") #abajo y a la izquierda

# levantar paquetes
require(MASS) # to access Animals data sets
require(scales) # to access break formatting functions
data(Animals) # load data

 # x and y axis son transformados y formateados 
p2 <- ggplot(Animals, aes(x = body, y = brain)) + geom_point() +
     scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
              labels = trans_format("log10", math_format(10^.x))) +
     scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
              labels = trans_format("log10", math_format(10^.x))) +
     theme_bw() 
p2

######múltiples dimensiones
## x, y cuantitativas
ggplot(data = mpg,aes(x = displ, y = hwy)) + 
geom_point( aes(color = manufacturer, shape=trans))  +
facet_grid(cyl~ drv);

##x cuantitativa, y nominal
ggplot(mpg, aes(y=fl, x=year, color = hwy, shape=trans)) +
    scale_shape_manual(values=1:10) +
    scale_color_continuous(type = "viridis") + 
    geom_jitter(size=4) +
facet_grid(cyl~ drv); 


##### marcas    http://www.sthda.com/english/wiki/ggplot2-axis-ticks-a-guide-to-customize-tick-marks-and-labels 
# Convierte la columna dose de variable numeric a factor
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)

library(ggplot2)
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()
p

# Cambia la apariencia de las etiquetas de los ejes
p + theme(axis.text.x = element_text(face="bold", color="#993333", 
                           size=14, angle=45),
          axis.text.y = element_text(face="bold", color="#993333", 
                           size=14, angle=45))
                           
# Oculta las etiquetas de los ejes x e y 
p + theme(
  axis.text.x = element_blank(),
  axis.text.y = element_blank())

# Remueve los ticks de los ejes x e y las las etiquetas
p + theme(
  axis.text.x = element_blank(),
  axis.text.y = element_blank(),
  axis.ticks = element_blank())

## Cambiar las etiquetas de las marcas
# Solucion 1
p + scale_x_discrete(breaks=c("0.5","1","2"),
        labels=c("Dose 0.5", "Dose 1", "Dose 2"))
        
# Solucion 2 : se dibuja igual que la solución 1
p + scale_x_discrete(labels=c("0.5" = "Dose 0.5", "1" = "Dose 1",
                              "2" = "Dose 2"))



## Configurar los ejes para variables continuas
# scatter plot
sp<-ggplot(cars, aes(x = speed, y = dist)) + geom_point()
sp
# Cambiar las etiquetas de los ejes x e y, y los límites
sp + scale_x_continuous(name="Speed of cars", limits=c(0, 30)) +
  scale_y_continuous(name="Stopping distance", limits=c(0, 150))

## Configurar marcas específicas
# Define las marcas tick en el eje y
# una marca es realizada cada 5
p + scale_y_continuous(breaks=seq(0,40,5))

# las marcas son puestas a gusto del usuario
p + scale_y_continuous(breaks=c(5,7.5, 20, 25))
                     
# remueve los ticks y las lineas de grilla
p + scale_y_continuous(breaks=NULL)  ## + scale_x_discrete(breaks=NULL)

## Parallel Coordinates
library(plotly)

df <- read.csv("https://raw.githubusercontent.com/bcdunbar/datasets/master/parcoords_data.csv")

fig <- df %>%
  plot_ly(width = 1000, height = 600) 
fig <- fig %>% add_trace(type = 'parcoords',
          line = list(color = ~colorVal,
                      colorscale = 'Jet',
                      showscale = TRUE,
                      reversescale = TRUE,
                      cmin = -4000,
                      cmax = -100),
          dimensions = list(
            list(range = c(~min(blockHeight),~max(blockHeight)),
                 constraintrange = c(100000,150000),
                 label = 'Block Height', values = ~blockHeight),
            list(range = c(~min(blockWidth),~max(blockWidth)),
                 label = 'Block Width', values = ~blockWidth),
            list(tickvals = c(0,0.5,1,2,3),
                 ticktext = c('A','AB','B','Y','Z'),
                 label = 'Cyclinder Material', values = ~cycMaterial),
            list(range = c(-1,4),
                 tickvals = c(0,1,2,3),
                 label = 'Block Material', values = ~blockMaterial),
            list(range = c(~min(totalWeight),~max(totalWeight)),
                 visible = TRUE,
                 label = 'Total Weight', values = ~totalWeight),
            list(range = c(~min(assemblyPW),~max(assemblyPW)),
                 label = 'Assembly Penalty Weight', values = ~assemblyPW),
            list(range = c(~min(HstW),~max(HstW)),
                 label = 'Height st Width', values = ~HstW),
            list(range = c(~min(minHW),~max(minHW)),
                 label = 'Min Height Width', values = ~minHW),
            list(range = c(~min(minWD),~max(minWD)),
                 label = 'Min Width Diameter', values = ~minWD),
            list(range = c(~min(rfBlock),~max(rfBlock)),
                 label = 'RF Block', values = ~rfBlock)
            )
          )


fig

