Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为曲线饼图的hoverinfo添加附加值_R_Plotly - Fatal编程技术网

为曲线饼图的hoverinfo添加附加值

为曲线饼图的hoverinfo添加附加值,r,plotly,R,Plotly,我有一个数据集df_tidy,有三列职业,职务,和n df_tidy <- structure(list(vocation = c("AC - Accounting", "AC - Accounting", "AD - Advertising/Public Relations", "AD - Advertising/Public Relations", "AF - Arts - Fine"

我有一个数据集
df_tidy
,有三列
职业
职务
,和
n

df_tidy <- structure(list(vocation = c("AC - Accounting", "AC - Accounting", 
"AD - Advertising/Public Relations", "AD - Advertising/Public Relations", 
"AF - Arts - Fine", "AF - Arts - Fine", "AF - Arts - Fine", "AF - Arts - Fine", 
"BA - Business - Administrative/Management", "BE - Business - Entrepreneur/Owner", 
"BE - Business - Entrepreneur/Owner", "BK - Banking", "BK - Banking", 
"BK - Banking", "CN - Consulting", "CO - Communications", "CO - Communications", 
"CP - Computing/Information Systems", "CP - Computing/Information Systems", 
"EA - Education - Higher Ed Administration", "EA - Education - Higher Ed Administration", 
"EA - Education - Higher Ed Administration", "EA - Education - Higher Ed Administration", 
"EA - Education - Higher Ed Administration", "EA - Education - Higher Ed Administration", 
"EA - Education - Higher Ed Administration", "ED - Education, Sec/Pri Ed Teaching (K-12)", 
"EM - Entertainment", "EN - Engineering", "EO - Education - Other", 
"ES - Education, Sec/Pri Ed Admin (K-12)", "ET - Education - Higher Ed Teaching", 
"EV - Environmental Sciences/Services", "FB - Fashion/Beauty", 
"FB - Fashion/Beauty", "FB - Fashion/Beauty", "FO - Fund Raising/Foundation", 
"FV - Finance/Venture Capital", "FV - Finance/Venture Capital", 
"GO - Government - Other", "HC - Health Care Administration", 
"IN - Insurance", "JW - Journalism/Writing", "LB - Human Resources/Labor", 
"LB - Human Resources/Labor", "LB - Human Resources/Labor", "LO - Law - Other", 
"LO - Law - Other", "LS - Library Science", "LW - Law - Lawyer", 
"LW - Law - Lawyer", "LW - Law - Lawyer", "MC - Museum/Cultural Centers", 
"MC - Museum/Cultural Centers", "MD - Media - Radio, TV, Film", 
"MD - Media - Radio, TV, Film", "MD - Media - Radio, TV, Film", 
"MS - Marketing/Sales", "MS - Marketing/Sales", "MS - Marketing/Sales", 
"MS - Marketing/Sales", "NP - Not-for-Profit", "NP - Not-for-Profit", 
"RC - Restaurant/Catering/Food Service", "RC - Restaurant/Catering/Food Service", 
"RC - Restaurant/Catering/Food Service", "RD - Research/Development", 
"TC - Telecommunications"), job_title = c("Agent", "Assurance Associate", 
"IT Recruiting Specialist/Admin Manager", "Senior Associate", 
"Assistant Registrar", "Associate Director, Arts in Education", 
"AVP, Head of Sale Managment", "Senior Researcher", "Assistant Director, Energy", 
"Co-Founder and Chief Executive Officer", "Founder & CEO", "Relationship Liaison II", 
"Senior Fund Accountant", "Vice President, Equity Analyst", "Verifying Supervisor", 
"Project Manager", "Senior Security Leader/Paranoids", "Founder/President", 
"VFX Coordinator", "Academic Program Assistant", "Assistant Director", 
"Assistant University Archivist", "Grad Research Assistant, Chief of Staff", 
"Instructor, Graduate Programs", "Library Clerk 2", "Program Associate", 
"Teacher", "Supervising Field Producer", "General Counsel", "Director of College Counseling", 
"Assistant High School Principal", "Teaching Assistant", "Director of Philanthropy", 
"Manager", "Sourcing Manager", "Visual Stylist", "Executive Director", 
"Chief Marketing Officer", "Financial Representative", "Executive Assistant", 
"Client Mgr., Global Health Benefits", "Programmer", "Writer", 
"Employment Specialist", "Human Resources Director", "Human Resources Specialist", 
"Director of Research", "Senior Health Policy Analyst", "Readers' Services Librarian", 
"Associate", "Partner", "Senior Assistant District Attorney", 
"Chief Curator", "Co-Founder and Partner", "Head of Creative/Executive Producer", 
"Head of Production", "Head of Technologies", "Digital Media Sales Operations Mgr", 
"Director of Sales and Marketing", "Regional Mktg. & Communications Dir.", 
"Sr. Production Designer", "Director of Media Relations", "Program Manager", 
"Bartender", "Guest Relations Coordinator", "Shift Supervisor", 
"Lab Manager", "Client Solution Exec. 3 - National Bus."), n = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L)), row.names = c(NA, -68L), groups = structure(list(
    vocation = c("AC - Accounting", "AD - Advertising/Public Relations", 
    "AF - Arts - Fine", "BA - Business - Administrative/Management", 
    "BE - Business - Entrepreneur/Owner", "BK - Banking", "CN - Consulting", 
    "CO - Communications", "CP - Computing/Information Systems", 
    "EA - Education - Higher Ed Administration", "ED - Education, Sec/Pri Ed Teaching (K-12)", 
    "EM - Entertainment", "EN - Engineering", "EO - Education - Other", 
    "ES - Education, Sec/Pri Ed Admin (K-12)", "ET - Education - Higher Ed Teaching", 
    "EV - Environmental Sciences/Services", "FB - Fashion/Beauty", 
    "FO - Fund Raising/Foundation", "FV - Finance/Venture Capital", 
    "GO - Government - Other", "HC - Health Care Administration", 
    "IN - Insurance", "JW - Journalism/Writing", "LB - Human Resources/Labor", 
    "LO - Law - Other", "LS - Library Science", "LW - Law - Lawyer", 
    "MC - Museum/Cultural Centers", "MD - Media - Radio, TV, Film", 
    "MS - Marketing/Sales", "NP - Not-for-Profit", "RC - Restaurant/Catering/Food Service", 
    "RD - Research/Development", "TC - Telecommunications"), 
    .rows = structure(list(1:2, 3:4, 5:8, 9L, 10:11, 12:14, 15L, 
        16:17, 18:19, 20:26, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 
        34:36, 37L, 38:39, 40L, 41L, 42L, 43L, 44:46, 47:48, 
        49L, 50:52, 53:54, 55:57, 58:61, 62:63, 64:66, 67L, 68L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 35L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))
问题

每个
职业的
悬停信息
仅显示每个
职业的1个
职务
。我希望
hoverinfo
包含每个特定
职业的每个
工作
。例如,
EA-教育-高等教育管理
的悬停信息应为:

Job Titles:

Academic Program Assistant
Assistant Director
Assistant University Archivist
Grad Research Assistant, Chief of Staff
Instructor, Graduate Programs
Library Clerk 2
Program Associate

Percent of Total:

10.3%

我能让它工作的唯一方法是,我将数据聚合到每个职业的一行,并将名称连接起来。我已经使用了
data.table
,但希望能为您提供要点

我在这里按照最后两个R示例进行格式化

并将
部分添加到工具提示中,因为它将返回一个额外的
跟踪0
,如下所示:

您可以使用此命令替换基本的
plot\u ly
命令,添加的
layout
可以很好地使用它


library(data.table)
dt <- as.data.table(df_tidy)
# summing the n for the volumes, and concatenate the jobs together with \n as a line break
dt_agg <- dt[, .(n=sum(n),
                 jobs=paste(job_title, collapse="\n")), by=vocation]

pie_chart_vocation_plotly <- plot_ly(dt_agg, 
                                     labels = ~vocation, 
                                     values = ~n, 
                                     type = 'pie', 
                                     text = ~jobs,
                                     textposition = 'outside', 
                                     textinfo = 'label',
                                     hovertemplate = 
                                       "<b>Job Titles</b>:<br><br>%{text}<br><br><b>Percent of total</b>:<br>%{percent}<extra></extra>"
                                     ) 

库(数据表)
dt

library(data.table)
dt <- as.data.table(df_tidy)
# summing the n for the volumes, and concatenate the jobs together with \n as a line break
dt_agg <- dt[, .(n=sum(n),
                 jobs=paste(job_title, collapse="\n")), by=vocation]

pie_chart_vocation_plotly <- plot_ly(dt_agg, 
                                     labels = ~vocation, 
                                     values = ~n, 
                                     type = 'pie', 
                                     text = ~jobs,
                                     textposition = 'outside', 
                                     textinfo = 'label',
                                     hovertemplate = 
                                       "<b>Job Titles</b>:<br><br>%{text}<br><br><b>Percent of total</b>:<br>%{percent}<extra></extra>"
                                     )