Nevedha Ayyanar
Nevedha Ayyanar

Reputation: 865

Display the text label above the point in the line chart using plot_ly() in r

I am trying to visualize a line chart using ploy_ly() function. I need to display the label above the line chart. But the labels are displayed over the points and it looks messy.

The dataframe used is as follows:

  Quarter   average
1 2018 Q1     47.6
2 2018 Q2     46.4
3 2018 Q3     45.7
4 2018 Q4     45.5
5 2019 Q1     45.7
6 2019 Q2     46.3
7 2019 Q3     45.7

The code used for visualizing the line chart using plot_ly() is as follows:

plot_ly(teamAverageperweek, x = teamAverageperweek$Quarter,
                       text = teamAverageperweek$average,
                       hoverinfo = 'text',
                       hovertext = paste('Quarter of Date: ', teamAverageperweek$Quarter,
                                         '<br> Avg Hours Per Week: ',teamAverageperweek$average),
                       showlegend = FALSE)%>%
   add_trace(y = teamAverageperweek$average,
             type = 'scatter',
             mode = 'lines', 
             line = list(color = 'rgb(242,142,43)', 
                         width = 3)) %>%
   add_trace(x = teamAverageperweek$Quarter,
             y = teamAverageperweek$average,
             type = 'scatter',
             mode = 'markers', 
             marker = list(color = 'rgb(242,142,43)', 
                           size = 8)) %>%
   layout(
      yaxis = list(
       range = c(20,70),
       title = "Avg Hours Per Week"
     ) 
   ) %>%
  layout(hoverlabel = list(bgcolor= 'white')) %>%
  layout(showlegend = FALSE) %>%
  add_annotations(x = teamAverageperweek$Quarter,
                  y = teamAverageperweek$average,
                  text =  teamAverageperweek$average,
                  xref = "x",
                  yref = "y",
                  yanchor = 'center',
                  xanchor = 'top',
                  showarrow = FALSE)

Is it possible to display the text label above the points?

Thanks in advance!!

Upvotes: 1

Views: 4842

Answers (1)

vpz
vpz

Reputation: 1044

Yes it is possible, you can use textposition = "bottom center".

Here you can find the options for textposition.

Well, I modified yours code quite a bit, but I think It is more readable that way. I hope it can help you.

For example you can use:

  • teamAverageperweek %>% plot_ly(...) insted of plot_ly(data = teamAverageperweek)

  • x = ~Quarter insted of teamAverageperweek$Quarter

  • You can use only one trace and set mode = 'lines+markers+text' insted of add 2 traces and use annotations()

  • You also can set all parameters of layout(... , ...) separating it with commas.

Here is your code, but modified:

library(dplyr)
library(plotly)

# Yours data
teamAverageperweek = data.frame(
                        Quarter = c("2018 Q1","2018 Q2","2018 Q3","2018 Q4",
                                    "2019 Q1","2019 Q2","2019 Q3"),
                        average = c(47.6,46.4,45.7,45.5,
                                    45.7,46.3,45.7)
                        )
# The Plot
teamAverageperweek %>%
  plot_ly(x = ~Quarter,
        text = ~average,
        hoverinfo = 'text',
        hovertext = ~paste('Quarter of Date: ', Quarter,
                          '<br> Avg Hours Per Week: ',average),
        showlegend = FALSE)%>%
  add_trace(y = ~average,
            type = 'scatter',
            mode = 'lines+markers+text', 
            line = list(color = 'rgb(242,142,43)', 
                        width = 3),
            textposition = "bottom center", # here the text position
            marker = list(color = 'rgb(242,142,43)', 
                          size = 8)) %>%
  layout(
    yaxis = list(range = c(20,70),
                 title = "Avg Hours Per Week"
                 ),
    hoverlabel = list(bgcolor= 'white')
         )

Here the output:

plot

Upvotes: 4

Related Questions