【问题标题】:How to have bar labels be names in Plotly for R如何在 Plotly for R 中将条形标签命名为名称
【发布时间】:2018-08-22 10:25:23
【问题描述】:

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

我有一个名为 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 图表。

如何将我的酒吧标签添加到机场名称中,而不仅仅是数字?

【问题讨论】:

    标签: r r-plotly


    【解决方案1】:

    随意使用 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)
    

    最终的结果是这样的。

    【讨论】:

      猜你喜欢
      • 2016-11-14
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 2019-10-28
      • 2021-10-27
      • 2021-01-29
      • 2021-06-28
      • 2021-12-12
      相关资源
      最近更新 更多