简体   繁体   中英

R - How to display only one hover text on a multiple lines chart with plotly?

I use the code below to plot a multiple lines chart and having hover informations with ggplotly:

data <- data %>%
    mutate(text = paste("Epoch : ", epoch, "\nTrain Loss : ", loss, "\nTest Loss : ", test_loss))
g <- ggplot(
        data,
        aes(x = epoch + 1, text = text, group = 1)
    ) +
    geom_line(
        aes(y = loss, color = 'train set loss'),
        size = 0.8
    ) +
    geom_line(
        aes(y = test_loss, color = 'test set loss'),
        size = 0.8
    ) +
    labs(x = 'epoch', y = 'loss') +
    scale_color_manual(values = c('train set loss' = 'blue', 'test set loss' = 'red')) +
    theme_bw() +
    theme(
        legend.title = element_blank(),
        legend.text = element_text(size = 15),
        axis.text.x = element_text(face = "bold", size = 12),
        axis.text.y = element_text(face = "bold", size = 12),
        axis.title.x = element_text(face = "bold", size = 15),
        axis.title.y = element_text(face = "bold", size = 15)
    )
g <- ggplotly(g, tooltip = 'text') %>%
    config(displayModeBar = F)
g <- layout(g, legend = list(x = 0.75, y = 0.99), hovermode = 'x unified')
g

The corresponding result is the image below: 在此处输入图像描述

The problem is that I woul like to have only one hover text at the top or the bottom and not two (because as you can see, they are the same right now). I thought that with hovermode = 'x unified' would do the job, but it seems that it is not.

Thanks in advance guys.

Edit

The code below, with adding 2 columns text1 and text2 instead of just text and adding the text parameter into the aesthtitics of the two geom_line works and does the image below:

data <- data %>%
    mutate(text1 = paste("Epoch : ", epoch + 1, "\nLoss : ", loss)) %>%
    mutate(text2 = paste("Epoch : ", epoch + 1, "\nLoss : ", test_loss))
g <- ggplot(
        data,
        aes(x = epoch + 1)
    ) +
    geom_line(
        aes(y = loss, color = 'train set loss', text = text1, group = 1),
        size = 0.8
    ) +
    geom_line(
        aes(y = test_loss, color = 'test set loss', text = text2, group = 1),
        size = 0.8
    ) +
    labs(x = 'epoch', y = 'loss') +
    scale_color_manual(values = c('train set loss' = 'blue', 'test set loss' = 'red')) +
    theme_bw() +
    theme(
        legend.title = element_blank(),
        legend.text = element_text(size = 15),
        axis.text.x = element_text(face = "bold", size = 12),
        axis.text.y = element_text(face = "bold", size = 12),
        axis.title.x = element_text(face = "bold", size = 15),
        axis.title.y = element_text(face = "bold", size = 15)
    )
g <- ggplotly(g, tooltip = 'text') %>%
    config(displayModeBar = F)
g <- layout(g, legend = list(x = 0.75, y = 0.99), hovermode = 'x unified')
g

在此处输入图像描述

However, this solution brings the following message: Warning: Ignoring unknown aesthetics: text

Perhaps you should try tooltip = c("x","y") . See example below

# create some data
x <- c(1:10); y <- x*x ; yy <- x+x
df1 <- data.frame(x,y,yy)

p1 <- ggplot(df1,aes(x=x)) +
  geom_line(aes(y=y, color = 'train set loss')) +
  geom_line(aes(y=yy, color = 'train set loss2')) 

p2 <- ggplotly(p1, tooltip = c("x","y")) %>%
  config(displayModeBar = F)
p2 <- layout(p2, legend = list(x = 0.75, y = 0.99), hovermode = 'x unified')
p2
  

输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM