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

Daniel L

I would like to add space/margin in-between each y-axis label, so the points do not overlap, but I have not been able to find a solution.

Screenshot of the graph

The code for generating the plot is here:

library(tidyverse)
devtools::install_github('danielludolf/FFplot')
library(FFplot)
library(plotly)

p <- data %>% 
ggplot(aes(x = `Race/Ethnicity`, y = fct_inorder(Measures))) + 
labs(x = "", y = "") +
geom_point(aes(size = Pixels,
             text = paste0(
               "Measure: ", str_wrap(Measures, 60), "<br>",
               "Disparity: ", str_wrap(`Race/Ethnicity`, 50), "<br>")),
         color = '#004c89', stroke = 1) + 
theme(legend.position = "none")

ff_ggplotly(p, source_name = "Add Source Here", width = 750, height = 550) %>% 
  layout(yaxis = list(gridcolor = '#d4d4d3'))

The dataset I am using is below:

structure(list(Measures = c("Zero Net Worth", "Zero Net Worth", 
"Uninsured", "Uninsured", "Unemployment", "Unemployment", "Poverty", 
"Poverty", "Median Employment Income", "Median Employment Income", 
"Liquid Asset Poverty", "Liquid Asset Poverty", "Income Insufficiency", 
"Income Insufficiency", "Housing Cost Burden (renters)", "Housing Cost Burden (renters)", 
"Housing Cost Burden (homeowners)", "Housing Cost Burden (homeowners)", 
"Houshold Underbanked Status", "Houshold Underbanked Status", 
"Household Banking Status", "Household Banking Status", "Homeownership", 
"Homeownership", "Employer-Provided Health Insurance", "Employer-Provided Health Insurance", 
"Educational Attainment", "Educational Attainment", "Business Value", 
"Business Value", "Asset Poverty", "Asset Poverty"), `Race/Ethnicity` = c("Black/AA, NH : 
White, NH", 
"Hispanic/Latino : White, NH", "Black/AA, NH : White, NH", "Hispanic/Latino : White, NH", 
"Black/AA, NH : White, NH", "Hispanic/Latino : White, NH", "Black/AA, NH : White, NH", 
"Hispanic/Latino : White, NH", "Black/AA, NH : White, NH", "Hispanic/Latino : White, NH", 
"Black/AA, NH : White, NH", "Hispanic/Latino : White, NH", "Black/AA, NH : White, NH", 
"Hispanic/Latino : White, NH", "Black/AA, NH : White, NH", "Hispanic/Latino : White, NH", 
"Black/AA, NH : White, NH", "Hispanic/Latino : White, NH", "Black/AA, NH : White, NH", 
"Hispanic/Latino : White, NH", "Black/AA, NH : White, NH", "Hispanic/Latino : White, NH", 
"Black/AA, NH : White, NH", "Hispanic/Latino : White, NH", "Black/AA, NH : White, NH", 
"Hispanic/Latino : White, NH", "Black/AA, NH : White, NH", "Hispanic/Latino : White, NH", 
"Black/AA, NH : White, NH", "Hispanic/Latino : White, NH", "Black/AA, NH : White, NH", 
"Hispanic/Latino : White, NH"), Pixels = c(35.4, 51.55, 91.4500000000001, 
79.1000000000001, 41.8517568812349, 19.0738225897106, 36.451355662209, 
84.5353032987158, 41.8689225795901, 52.8685324089523, 14.2245846952017, 
7.76867956641282, 13.775217283819, 9.77368755100193, 18.3689239165512, 
25.3197416180145, 11.9779708756455, 16.1033936963916, 34.8411564399252, 
22.7121030150228, 39.2441860465116, 28.75, 31.1957446808511, 
40.8170212765957, 75.4838709677419, 78.5483870967742, 32.075, 
42.8416666666667, 21.1139240506329, 26.0443037974683, 20.7666836410461, 
33.1868273779831)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-32L))
Quinten

You can use a margin in your layout function to create space between the y-axis labels. You can use the following code:

library(tidyverse)
devtools::install_github('danielludolf/FFplot')
library(FFplot)
library(plotly)

p <- data %>% 
  ggplot(aes(x = `Race/Ethnicity`, y = fct_inorder(Measures))) + 
  labs(x = "", y = "") +
  geom_point(aes(size = Pixels,
                 text = paste0(
                   "Measure: ", str_wrap(Measures, 60), "<br>",
                   "Disparity: ", str_wrap(`Race/Ethnicity`, 50), "<br>")),
             color = '#004c89', stroke = 1) + 
  theme(legend.position = "none")

ff_ggplotly(p, source_name = "Add Source Here", width = 750, height = 550) %>% 
  layout(margin = list(l=0, r=0, b=20, t=20, pad=0),
         yaxis = list(gridcolor = '#d4d4d3'))

Output:

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 make more space for y axis labels for gantt chart

Plotly: How to adjust the space between axis labels and plot area?

Plotly: How to adjust the space between axis labels and plot area?

how to add reactive x and y axis labels to shiny plotly graph?

How to reduce space between axis ticks and axis labels in R

How to put labels between axis values and add a second Y axis

Space between y-axis and y-axis labels d3js

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

Add whitespace / increase the space between the X and the Y axis in ggplot

R how to increase spacing between X and Y axis value labels

Colors on the x-axis labels in plotly R

Getting separate axis labels on R plotly subplots

How to change the x-axis and y-axis labels in plotly?

Increase the space between x axis labels ggplot?

How do I add space between the tick labels and the graph in plotly (python)?

Trying to add more space between labels in x-axis on pyplot histogram

Plotly/Dash: is it possible to hide tick labels of a secondary y axis?

y-axis labels don't get displayed properly with Plotly

Plotly horizontal bar - display ALL y axis labels

group plotly y-axis labels, but keep individual data lines

How to add space between bars using Plotly?

How do you increase the space between the axis labels and axis titles in R using functions ggplot() and plot_ly?

R: plotly graph with dual Y axis?

Second Y-Axis in a R plotly graph

Reduce space between y axis and cathegories in a ggplot

Space between categories on the y-axis

making space between next values on y axis

How to correctly place axis labels in Plotly (R) subplots with multiple axes?

Plotly R : Format axis tick labels to log10