Dev P
Dev P

Reputation: 449

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 but their 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, which I do not want. Even though x1 and y1 are plotted, I need to have the actual values being shown (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))

Upvotes: 0

Views: 59

Answers (1)

Majid
Majid

Reputation: 1854

There might be many ways to this but after formatting the dataframe then you can use tooltip="text" to show the desired tooltip here.

df1 <- df  
df1$x <- NULL
df1$y <- NULL
dfx <- melt(df1,id=c("sd"))


df2 <- df  
df2$x1 <- NULL
df2$y1 <- NULL
dfy <- melt(df2,id=c("sd"))
names(dfy) <- c("sd1", "variable1", "value1")

df <- cbind(dfx,dfy)

ggplotly(ggplot(data = df,aes(x=sd, y=value, color=variable, text=value1))+
           geom_line(size=0.2), tooltip = "text")

Upvotes: 2

Related Questions