IS there way to change the labeling name in ggplotly

I have a challenge solving the below issue. Request experts here to help me in this. Before I put the proper reprex. Let me explain the problem here,

df
   sd   x      y        x1         y1
1  1    12    1019    0.3867382  1.3484106
2  2    20    1016   1.7618076  0.8860984
3  3    7      1006   -0.4726801 -0.6549423
4  4    3      1009   -1.1602147 -0.1926301

Well, I have to plot x and y here. But if you observe both there scales are different. So what I have done is I have normalized the data and came up with x1 and y1. Normalizing formula is
((x - mean(x)) / sd(x)

Now I need to plot x1 and y1. So have made some data wrangling and below is the reprex of that. I am getting the output. But the main issue here is that, since x1 and y1 are getting plotted, when i move the cursor over it shows x1 and y1 values. I do not need that. Even though x1 and y1 are plotted, I need to have actual values being shown there (x and y values). Is there a way to achieve this?

library(ggplot2)
library(plotly)
require(reshape)
df <- structure(list(sd = c(1, 2, 3, 4), x = c(12, 20, 7, 3), y = c(1019, 
                                                              1016, 1006, 1009), x1 = c(0.386738249968166, 1.76180758318831, 
                                                                                        -0.472680083294425, -1.1602147499045), y1 = c(1.34841060188091, 
                                                                                                                                      0.886098395521742, -0.654942292342157, -0.192630085982987)), row.names = c(NA, 
                                                                                                                                                                                                                 -4L), class = "data.frame")                                                                                                                                                                                        
df$x <- NULL
df$y <- NULL
df1 <- melt(df,id=c("sd"))
ggplotly(ggplot(data = df1,aes(x=sd,y=value,color=variable))+geom_line(size=0.2)

Is this what you are looking for?

library(ggplot2)
library(plotly)

df <- data.frame(sd = c(1, 2, 3, 4, 1,2,3,4), 
                 variable = c(rep("x1", 4), rep("y1", 4)),
                 value = c(12, 20, 7, 3,1019, 1016, 1006, 1009), 
                scaledValue = c(0.386738249968166, 1.76180758318831,
                            -0.472680083294425, -1.1602147499045, 1.34841060188091, 
                            0.886098395521742, -0.654942292342157, -0.192630085982987))
df

#  sd variable value scaledValue
#1  1       x1    12   0.3867382
#2  2       x1    20   1.7618076
#3  3       x1     7  -0.4726801
#4  4       x1     3  -1.1602147
#5  1       y1  1019   1.3484106
#6  2       y1  1016   0.8860984
#7  3       y1  1006  -0.6549423
#8  4       y1  1009  -0.1926301

ggplotly(ggplot(data = df ,aes(x=sd,y=scaledValue,
                               color=variable, 
                               hoverinfo = value)) + 
           geom_line(size=0.2))
1 Like

Here is a second version.

library(ggplot2)
library(plotly)

df <- data.frame(sd = c(1, 2, 3, 4, 1,2,3,4), 
                 variable = c(rep("x1", 4), rep("y1", 4)),
                 value = c(12, 20, 7, 3,1019, 1016, 1006, 1009), 
                scaledValue = c(0.386738249968166, 1.76180758318831,
                            -0.472680083294425, -1.1602147499045, 1.34841060188091, 
                            0.886098395521742, -0.654942292342157, -0.192630085982987))

plt <- ggplot(data = df, aes(x=sd, y=scaledValue, color=variable, text = value)) + 
           geom_line(size=0.2)
ggplotly(plt, tooltip = "text")
1 Like

Perfect and thanks a lot. Both are working

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.