如何让条形标签成为 Plotly for R 中的名称

How to have bar labels be names in Plotly for R

所以我正在尝试制作一个条形图来显示飞往芝加哥的最受欢迎的机场。出于某种原因,我发现用机场名称专门标记我的条形图非常困难。

我有一个名为 ty 的数据框

> ty
                                                     Name
1   Atlanta, GA: Hartsfield-Jackson Atlanta International
2                                 New York, NY: LaGuardia
3      Minneapolis, MN: Minneapolis-St Paul International
4              Los Angeles, CA: Los Angeles International
5                        Denver, CO: Denver International
6       Washington, DC: Ronald Reagan Washington National
7                      Orlando, FL: Orlando International
8           Phoenix, AZ: Phoenix Sky Harbor International
9                 Detroit, MI: Detroit Metro Wayne County
10                  Las Vegas, NV: McCarran International
11         San Francisco, CA: San Francisco International
12 Dallas/Fort Worth, TX: Dallas/Fort Worth International
13                        Boston, MA: Logan International
14           Philadelphia, PA: Philadelphia International
15               Newark, NJ: Newark Liberty International

我还有一个名为df的数据框

      id numArrivals
1  10397         964
2  12953         962
3  13487         883
4  12892         823
5  11292         776
6  11278         771
7  13204         725
8  14107         700
9  11433         672
10 12889         647
11 14771         611
12 11298         580
13 10721         569
14 14100         567
15 11618         488

id对应的机场名称10397Atlanta, GA: Hartsfield-Jackson Atlanta International,他们继续按这个顺序。

然而,当我 运行:

plotly::plot_ly(df,x=ty["Name"],y=df$numArrivals,type="bar",color=I("rgba(0,92,124,1)"))

我得到了this图表。

如何将我的酒吧标签变成机场的名称,而不仅仅是数字?

随意使用 ggplotly() 来创建您的绘图。我使用下面的代码创建了一个小示例。

example <- data.frame(airport = c("Atlanta, GA: Hartsfield-Jackson Atlanta International","New York, NY: LaGuardia","Minneapolis, MN: Minneapolis-St Paul International"),
              id = c(10397,12953,13487),
              numArrivals = c(964,962,883),stringsAsFactors = F)

library(ggplot2)
library(plotly)

a <- ggplot(example,aes(x=airport,y=numArrivals,fill=id)) + geom_bar(stat = "identity") + coord_flip()
ggplotly(a)

最后的结果是这样的。