Is there a way to add text to the `y2` axis of a plotly chart in R?

Thomas

I would like to add text to the data points in a plotly chart. See the example below:

df <- data.frame(
  category_value = c("A", "B", "C"), 
  bar_value = c(1000, 2000, 15000), 
  line_value = c(0.8, 0.9, 0.7)
)


plot_ly(df) %>%
  add_trace(x = ~category_value, y = ~bar_value, type = "bar", name = "bar_value") %>%
  add_trace(x = ~category_value, y = ~line_value, yaxis = "y2", type = "scatter", name = "line_value", mode = "lines+markers") %>%
  layout(xaxis = list(title = var), yaxis2 = list(overlaying = "y", side = "right")) %>% 
  add_text(x = ~category_value, y = ~bar_value, text = ~bar_value, textposition = "top right") %>% 
  add_text(x = ~category_value, y = ~line_value, text = ~line_value, textposition = "top right")

The problem is that the text for the line chart is too low in the y-dimension. Something like add_text(x = ~category_value, y2 = ~line_value, text = ~line_value, textposition = "top right") would be helpful, but this does not work.

Kat

Yes, for sure. If you wanted the text labels for the bars and lines, you don't need four traces, you only need two (Plotly will make the rest.) Instead of "lines+markers" you can use "lines+markers+text", for the bar chart, you simply need to add the argument text.

I moved the legend over as well. The legend was overlapping the axis ticks for y2.

plot_ly(df) %>%
  add_trace(x = ~category_value, y = ~bar_value, type = "bar", name = "bar_value",
            text = ~bar_value, hoverinfo = "x+y", textposition = "top") %>%
  add_trace(x = ~category_value, y = ~line_value, yaxis = "y2", text = ~line_value,
            type = "scatter", name = "line_value", mode = "lines+markers+text",
            textposition = "top") %>%
  layout(xaxis = list(title = var), 
         yaxis2 = list(overlaying = "y", side = "right"),
         legend = list(x = 1.05))

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Plotly - How to add count of rows to Y axis on line chart?

How to add text label to R plotly aggregate chart?

Plotly R: Add space in-between y-axis labels

How can I add 2 100% stacked bars (Y Axis) to each element in the X Axis of my chart in plotly?

How to add Thousand separator in both axis of a 2-y axis chart in R using ggplot2

Is there a way to reverse the order of the y axis in heatmap of plotly

Hover text for plotly r candlestick chart

Add specific legend to R plotly chart

Is there an easy way to add a secondary y-axis to a Plotly plot without the need of providing a second array?

How to Arrange the x axis on a Plotly Bar Chart with R

Easy way to add a break to Y-axis in barchart() of lattice in R?

R: plotly graph with dual Y axis?

Second Y-Axis in a R plotly graph

python plotly area chart with Y-axis in percent

Plotly: How to make more space for y axis labels for gantt chart

Plotly Radar Chart: arranging y-axis labelled with string by levels

Python Plotly Line Chart with a Dataframe: Wrong scale on the y-axis

Offline Plotly: Generate bar chart of 2 columns on (y axis) & 1 column on ( x axis)

Custom hoverinfo text on plotly R chart that is different to the 'text' parameter?

Add buffer to Y axis with chart js

Multiple y-axes chart with Plotly in R

Plotly (python) : How to add more then one text label in a bar chart

Add horizontal line to hoverinfo text in plotly in R

R plotly: Add text in polar scatter plot

Wrapping text and increasing the space between factors on the y-axis in a ggplot bar chart in R?

R - creating a bar and line on same chart, how to add a second y axis

Set text size within marker in r plotly bubble chart

R plotly add_trace to a chart with color groups

How to add bordered data labels to R plotly bar chart?