簡體   English   中英

Plotly R:在 y 軸標簽之間添加空間

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

我想在每個 y 軸 label 之間添加空間/邊距,這樣點就不會重疊,但我一直找不到解決方案。

圖表截圖

生成 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'))

我正在使用的數據集如下:

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

您可以在layout function 中使用margin在 y 軸標簽之間創建空間。 您可以使用以下代碼:

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:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM