简体   繁体   English

Plotly R:在 y 轴标签之间添加空间

[英]Plotly R: Add space in-between y-axis labels

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.我想在每个 y 轴 label 之间添加空间/边距,这样点就不会重叠,但我一直找不到解决方案。

图表截图

The code for generating the plot is here:生成 plot 的代码在这里:

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))

You can use a margin in your layout function to create space between the y-axis labels.您可以在layout function 中使用margin在 y 轴标签之间创建空间。 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: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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