0

I have created the following dataframe

library(data.table)
require(wrapr)
library(readxl)
library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly)

df <- data.frame("X_Frequency" = c(5, 10, 55, 180, 300, 360, 1000, 2000) , 
                 "X_Axis" = c(0.009185742, 0.207822221, 0.067542222, 0.002597778, 
                              0.002597778, 0.001454756, 0.001454756 , 0.001454756))

Next we create a chart using ggplot. We also transform the axes so that we get log values

B <- ggplot(data = df, 
            mapping = aes(x = df$X_Frequency, y = df$X_Axis)) +  
  geom_line() +
  labs(x = "Frequency(Hz)", y="Axis")

B <- ggplotly(B, dynamicTicks = TRUE)
B <- layout(B, yaxis = list(type = "log"))
B <- layout(B, xaxis = list(type = "log"))

B

The resultant graph shows the various points separated by straight lines. I have added interpolations between the points as follows

Next we create a interpolations between the available points

df$X_Slope2 <- 0### Initiate slope column
for(i in 2:nrow(df)){
  df$X_Slope2[i] = (df$X_Axis[i] - df$X_Axis[i-1]) / 
    (df$X_Frequency[i] - df$X_Frequency[i - 1])
}

df_new <- bind_cols(
  df %>%
    select(X_Frequency, X_Axis, X_Slope2) %>%
    complete(., expand(., X_Frequency = 5:2000))
)

next I have created a new data frame with all interpolated values

df241 <- df_new %>% 
  left_join(df) %>% 
  fill(c(X_Slope2), .direction = 'up') ##fill up slopes

for(i in 1: nrow(df241)){
  if(is.na(df241$X_Axis[i]) == T){
    df241$X_Axis[i] = df241$X_Slope2[i] * 
      (df241$X_Frequency[i] - df241$X_Frequency[i-1]) +
      df241$X_Axis[i-1]
  } else {
     df241$X_Axis[i] = df241$X_Axis[i]
  }
}

We now create the same plot

C <- ggplot(data = df241, 
            mapping = aes(x = df241$X_Frequency, y = df241$X_Axis)) +  
  geom_line() +
  labs(x = "Frequency(Hz)", y="Axis")
C <- ggplotly(C, dynamicTicks = TRUE)
C <- layout(C, yaxis = list(type = "log"))
C <- layout(C, xaxis = list(type = "log"))

C

While without log transformation both graphs are identical, with log transformation, the second graph (with the interpolated values) shows curves instead of straight lines in contrast to the first graph without the interpolation.

Is it possible to generate the lines with interpolations without the curved nature. The line should be identical to graph B but the interpolations should be visible on hovering the mouse over the graph. requesting someone to guide me.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Raghavan vmvs
  • 1,213
  • 1
  • 10
  • 29
  • Your second graph does show straight lines: straight lines ***between interpolated values***. Try plotting a simple example (e.g. one straight line between two points, with one interpolated value in between) & check what the plot looks like after log transformation. – Z.Lin Feb 12 '19 at 10:49
  • I would like the straight lines between original values and the interpolated points to appear when I hover the mouse over the graph. Can this be accomplished? – Raghavan vmvs Feb 12 '19 at 10:55

0 Answers0