4

Rで、plot.lyの「テキスト」をこのような金額(またはその他のもの)にフォーマットするにはどうすればよいですか-$ 4,000,000(コンマ付き)-

私の「実際の」データはすでに $ としてフォーマットされていますが、 add_trace 関数で hoverinfo='x+text' を実行すると、フォーマットが失われます。plotly のレイアウト関数の Hoverformat = "$,f" も役に立ちませんでした。

積み上げ領域プロットの R での再現可能な例を次に示します。

library(plotly)

season <- c('Winter', 'Spring', 'Summer', 'Fall')
y1_real<- c(40, 80, 95, 10)
y2_real<- c(20, 55, 10, 10)
y3_real<- c(40, 10, 60, 180)

df= data.frame(season, y1_real, y2_real, y3_real)

#changing the format of the real data to $

class(df$y1_real)<- c("money", class(df$y1_real) )
class(df$y2_real)<- c("money", class(df$y2_real))
class(df$y3_real)<- c("money", class(df$y3_real))

df$y3_stck<- df$y3_real + df$y2_real + df$y1_real
df$y2_stck<- df$y2_real + df$y1_real
df$y1_stck<- df$y1_real

p1<- plot_ly(df, x=season, y=y1_stck, text=df$y1_real, hoverinfo='x+text+name', name="Blue", fill="tonexty")
p2<- add_trace(p1, x=season, y=y2_stck, text=df$y2_real, hoverinfo='x+text+name', name="Orange", fill="tonexty")
p3<- add_trace(p2, x=season, y=y3_stck, text=df$y3_real, hoverinfo='x+text+name', name="Green", fill="tonexty")

p4<- layout(yaxis=list(showgrid=FALSE, showline=FALSE, hoverformat="$,f"))
p4

ありがとう!

4

1 に答える 1