簡體   English   中英

如何將 ggplot2 上的 label 更改為 ZE1E1D3D40573127E6EE0480CAF128 中的 hover?

[英]How to change label on ggplot2 to hover in R?

所以我目前的數據看起來像這樣。

structure(list(Reportable = c("Non-Reportable Injury", "Reportable Injury", 
"Non-Reportable Injury", "Reportable Injury", "Non-Reportable Injury", 
"Reportable Injury", "Non-Reportable Injury", "Reportable Injury", 
"Non-Reportable Injury", "Reportable Injury", "Non-Reportable Injury", 
"Reportable Injury", "Non-Reportable Injury", "Reportable Injury", 
"Non-Reportable Injury", "Reportable Injury", "Non-Reportable Injury", 
"Reportable Injury", "Non-Reportable Injury", "Reportable Injury", 
"Non-Reportable Injury", "Reportable Injury"), Result_Description = c("OTHER", 
"OTHER", "GROUND", "GROUND", "DOOR", 
"DOOR", "STAIR STEP", "STAIR STEP", "FLOOR", "FLOOR", "CHAIR/SEAT", 
"CHAIR/SEAT", "LOCOMOTIVE, OTHER", "LOCOMOTIVE, OTHER", "BALLAST, STONES, ETC.", 
"BALLAST, STONES, ETC.", "DOOR, TRAP - PASSENGER TRAIN", "DOOR, TRAP - PASSENGER TRAIN", 
"PLATFORM", "PLATFORM", "TRACK (RAIL)", "TRACK (RAIL)"), count = c(69L, 
235L, 27L, 91L, 29L, 44L, 19L, 39L, 14L, 38L, 15L, 31L, 21L, 
25L, 16L, 27L, 15L, 22L, 8L, 25L, 4L, 26L), pct = c("23%", "77%", 
"23%", "77%", "40%", "60%", "33%", "67%", "27%", "73%", "32%", 
"66%", "45.7%", "54.3%", "37%", "63%", "41%", "59%", "24%", "76%", 
"13%", "87%"), total = c("69 (23%)", "235 (77%)", "27 (23%)", 
"91 (77%)", "29 (40%)", "44 (60%)", "19 (33%)", "39 (67%)", "14 (27%)", 
"38 (73%)", "15 (32%)", "31 (66%)", "21 (45.7%)", "25 (54.3%)", 
"16 (37%)", "27 (63%)", "15 (41%)", "22 (59%)", "8 (24%)", "25 (76%)", 
"4 (13%)", "26 (87%)"), total1 = c("69\n(23%)", "235\n(77%)", 
"27\n(23%)", "91\n(77%)", "29\n(40%)", "44\n(60%)", "19\n(33%)", 
"39\n(67%)", "14\n(27%)", "38\n(73%)", "15\n(32%)", "31\n(66%)", 
"21\n(45.7%)", "25\n(54.3%)", "16\n(37%)", "27\n(63%)", "15\n(41%)", 
"22\n(59%)", "8\n(24%)", "25\n(76%)", "4\n(13%)", "26\n(87%)"
)), row.names = c(NA, -22L), class = c("tbl_df", "tbl", "data.frame"
))

而我的 plot 如下:

ggplot(Result_counts, aes(fill=Reportable, y=count, x=as.factor(Result_Description), label = total)) +
  geom_bar(position="stack", stat="identity")+
  aes(stringr::str_wrap(as.factor(Result_Description), 40), count) +
  labs(x = "", y = "Injury Count", fill = "")+
 geom_text(size = 2, position = position_stack(vjust = 0.5)) +
  ggtitle("Injury Plot")+
  scale_fill_manual(values = c("darkorange", "cornflowerblue") ) +
  theme_hc() +
  coord_flip()

酷吧?

我一直在搞亂plotly更重要的是ggplotly所以刪除我的條內標簽,而是讓標簽出現在 hover 上。

那么如何對我的 ggplot 代碼進行此更改。

一旦完成,我仍然希望在每個條的右側有一個總數,作為 label,它表示每個完整條的計數總和和百分比,與Reportable無關。

所以基本上兩個目標

  1. 將現有標簽更改為懸停
  2. 添加新的 label 以獲取每個條形的實際總計數和整體百分比。

將上面的初始 dataframe 視為df 然后下面的代碼

b <- aggregate(df$count, by=list(a=df$Result_Description), FUN=sum)
  names(b) <- c("Result_Description","fullcount")
  Result_counts <- left_join(df,b,by="Result_Description")
  
  p <- ggplot(Result_counts, 
              aes(fill=Reportable, y=count, x=as.factor(Result_Description), 
                  text=paste(Reportable, ": ", total) )) +
    geom_bar(position="stack", stat="identity")+
    aes(stringr::str_wrap(as.factor(Result_Description), 40), count) +
    labs(x = "", y = "Injury Count", fill = "")+
    geom_text(aes(label = fullcount, y = fullcount, vjust = 0.5)) +
    ggtitle("Injury Plot")+
    scale_fill_manual(values = c("darkorange", "cornflowerblue") ) +
    theme_hc() +
    coord_flip()
  
  pp <- ggplotly(p, tooltip=c("text"))
  pp

給出這個 output:

輸出

暫無
暫無

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

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