如何在ggplot中的条形图顶部取消条形图和显示百分比?

时间:2018-02-26 00:09:11

标签: r ggplot2 geom-bar

我的数据集有17个分类变量。但是,我对两个变量name(包含33个不同的酒店名称)和NPS_Type(' Promoter和Detractor)感兴趣。

我使用下面的代码生成条形图,但由于某种原因,百分比计算似乎是相反的。即barplot显示Detractor条上的启动子百分比,反之亦然。所以,我认为可能会解散barplot可能会有所帮助。有关如何解决此问题的任何建议吗?

library(ggplot2)
ggplot(few_var5, aes(x='Hotel Name-Long_PL', y = NPS_Type, fill= NPS_Type)) +
  geom_bar(aes(y=(..count..)/sum(..count..))) +
  geom_text(aes(y = ((..count..)/sum(..count..)),
                label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = 0.25)

可能有用的示例:

Casino_PL Postal Code_PL Fitness Center_PL Property Longitude_PL Mini-Bar_PL Spa_PL  NPS_Type
105802          N          94523                 Y             -122.0611           N      N  Promoter
105803          N          94523                 Y             -122.0611           N      N  Promoter
105805          N          94523                 Y             -122.0611           N      N  Promoter
105814          N          94523                 Y             -122.0611           N      N  Promoter
105819          N          94523                 Y             -122.0611           N      N  Promoter
105848          N          94523                 Y             -122.0611           N      N  Promoter
105849          N          94523                 Y             -122.0611           N      N  Promoter
105908          N          94523                 Y             -122.0611           N      N  Promoter
105917          N          94523                 Y             -122.0611           N      N  Promoter
105920          N          94523                 Y             -122.0611           N      N  Promoter
105922          N          94523                 Y             -122.0611           N      N Detractor
105930          N          94523                 Y             -122.0611           N      N  Promoter
105935          N          94523                 Y             -122.0611           N      N  Promoter
105961          N          94523                 Y             -122.0611           N      N Detractor
105983          N          94523                 Y             -122.0611           N      N  Promoter
106003          N          94523                 Y             -122.0611           N      N  Promoter
106007          N          94523                 Y             -122.0611           N      N  Promoter
106031          N          94523                 Y             -122.0611           N      N  Promoter
106033          N          94523                 Y             -122.0611           N      N  Promoter
106040          N          94523                 Y             -122.0611           N      N  Promoter
106043          N          94523                 Y             -122.0611           N      N  Promoter
106051          N          94523                 Y             -122.0611           N      N  Promoter
106058          N          94523                 Y             -122.0611           N      N  Promoter
106065          N          94523                 Y             -122.0611           N      N  Promoter
106083          N          94523                 Y             -122.0611           N      N  Promoter
106104          N          94523                 Y             -122.0611           N      N  Promoter
106116          N          94523                 Y             -122.0611           N      N  Promoter
106161          N          94523                 Y             -122.0611           N      N  Promoter
106180          N          94523                 Y             -122.0611           N      N  Promoter
106182          N          94523                 Y             -122.0611           N      N  Promoter
106202          N          94523                 Y             -122.0611           N      N  Promoter
106206          N          94523                 Y             -122.0611           N      N Detractor
106216          N          94523                 Y             -122.0611           N      N  Promoter
106218          N          94523                 Y             -122.0611           N      N  Promoter
106253          N          94523                 Y             -122.0611           N      N  Promoter
106278          N          94523                 Y             -122.0611           N      N  Promoter
106305          N          94523                 Y             -122.0611           N      N  Promoter
106326          N          94523                 Y             -122.0611           N      N  Promoter
106328          N          94523                 Y             -122.0611           N      N  Promoter
106331          N          94523                 Y             -122.0611           N      N  Promoter
106359          N          94523                 Y             -122.0611           N      N  Promoter
106389          N          94523                 Y             -122.0611           N      N  Promoter
106418          N          94523                 Y             -122.0611           N      N  Promoter
106434          N          94523                 Y             -122.0611           N      N  Promoter
106441          N          94523                 Y             -122.0611           N      N  Promoter
106464          N          94523                 Y             -122.0611           N      N  Promoter
106468          N          94523                 Y             -122.0611           N      N  Promoter
106474          N          94523                 Y             -122.0611           N      N  Promoter
106526          N          94523                 Y             -122.0611           N      N  Promoter
106530          N          94523                 Y             -122.0611           N      N  Promoter
134812          N          94002                 Y             -122.2710           N      N  Promoter
134815          N          94002                 Y             -122.2710           N      N  Promoter
134820          N          94002                 Y             -122.2710           N      N  Promoter
134822          N          94002                 Y             -122.2710           N      N  Promoter
134825          N          94002                 Y             -122.2710           N      N  Promoter
134826          N          94002                 Y             -122.2710           N      N  Promoter
134827          N          94002                 Y             -122.2710           N      N  Promoter
134829          N          94002                 Y             -122.2710           N      N  Promoter
                                           POV_H Property Latitude_PL Pool-Indoor_PL   State_PL
105802                                   Leisure             37.94289              N California
105803                                   Leisure             37.94289              N California
105805                                   Leisure             37.94289              N California
105814                                   Leisure             37.94289              N California
105819                                   Leisure             37.94289              N California
105848                                   Leisure             37.94289              N California
105849                                   Leisure             37.94289              N California
105908                                  Business             37.94289              N California
105917                                  Business             37.94289              N California
105920                                   Leisure             37.94289              N California
105922                                  Business             37.94289              N California
105930                                  Business             37.94289              N California
105935                                   Leisure             37.94289              N California
105961                                   Leisure             37.94289              N California
105983                                   Leisure             37.94289              N California
106003  Combination of both business and leisure             37.94289              N California
106007                      Prefer not to answer             37.94289              N California
106031                                  Business             37.94289              N California
106033                                   Leisure             37.94289              N California
106040  Combination of both business and leisure             37.94289              N California
106043                                   Leisure             37.94289              N California
106051                                   Leisure             37.94289              N California
106058                                   Leisure             37.94289              N California
106065                                   Leisure             37.94289              N California
106083                                  Business             37.94289              N California
106104                                   Leisure             37.94289              N California
106116                                   Leisure             37.94289              N California
106161                                   Leisure             37.94289              N California
106180                                   Leisure             37.94289              N California
106182                                   Leisure             37.94289              N California
106202                                   Leisure             37.94289              N California
106206                                  Business             37.94289              N California
106216                                   Leisure             37.94289              N California
106218                                  Business             37.94289              N California
106253                                   Leisure             37.94289              N California
106278                                   Leisure             37.94289              N California
106305                                   Leisure             37.94289              N California
106326                                  Business             37.94289              N California
106328                                  Business             37.94289              N California
106331                                  Business             37.94289              N California
106359                                   Leisure             37.94289              N California
106389                                  Business             37.94289              N California
106418                      Prefer not to answer             37.94289              N California
106434                                   Leisure             37.94289              N California
106441                                  Business             37.94289              N California
106464                                   Leisure             37.94289              N California
106468  Combination of both business and leisure             37.94289              N California
106474                                  Business             37.94289              N California
106526                                   Leisure             37.94289              N California
106530                                   Leisure             37.94289              N California
134812                                  Business             37.53244              N California
134815                                  Business             37.53244              N California
134820  Combination of both business and leisure             37.53244              N California
134822  Combination of both business and leisure             37.53244              N California
134825                                  Business             37.53244              N California
134826  Combination of both business and leisure             37.53244              N California
134827                                  Business             37.53244              N California
134829                                  Business             37.53244              N California
        LENGTH_OF_STAY_C Condition_Hotel_H          City_PL Pool-Outdoor_PL
105802                 3                10    Pleasant Hill               Y
105803                 2                10    Pleasant Hill               Y
105805                 2                10    Pleasant Hill               Y
105814                 4                10    Pleasant Hill               Y
105819                 9                 9    Pleasant Hill               Y
105848                 1                 8    Pleasant Hill               Y
105849                 2                10    Pleasant Hill               Y
105908                 5                10    Pleasant Hill               Y
105917                 4                10    Pleasant Hill               Y
105920                 2                10    Pleasant Hill               Y
105922                 2                 5    Pleasant Hill               Y
105930                35                10    Pleasant Hill               Y
105935                 3                 9    Pleasant Hill               Y
105961                 1                 9    Pleasant Hill               Y
105983                 2                 9    Pleasant Hill               Y
106003                 1                10    Pleasant Hill               Y
106007                49                 8    Pleasant Hill               Y
106031                 4                10    Pleasant Hill               Y
106033                10                10    Pleasant Hill               Y
106040                 5                10    Pleasant Hill               Y
106043                 1                 9    Pleasant Hill               Y
106051                 2                10    Pleasant Hill               Y
106058                 1                10    Pleasant Hill               Y
106065                 1                10    Pleasant Hill               Y
106083                12                10    Pleasant Hill               Y
106104                 4                10    Pleasant Hill               Y
106116                 2                10    Pleasant Hill               Y
106161                 1                10    Pleasant Hill               Y
106180                 2                 9    Pleasant Hill               Y
106182                 2                10    Pleasant Hill               Y
106202                 1                10    Pleasant Hill               Y
106206                 1                 6    Pleasant Hill               Y
106216                 2                10    Pleasant Hill               Y
106218                79                 9    Pleasant Hill               Y
106253                 1                 9    Pleasant Hill               Y
106278                 3                 8    Pleasant Hill               Y
106305                 1                10    Pleasant Hill               Y
106326                 1                 9    Pleasant Hill               Y
106328                32                 8    Pleasant Hill               Y
106331                 1                10    Pleasant Hill               Y
106359                 2                10    Pleasant Hill               Y
106389                 1                10    Pleasant Hill               Y
106418                11                10    Pleasant Hill               Y
106434                 1                 9    Pleasant Hill               Y
106441                 3                10    Pleasant Hill               Y
106464                 1                 9    Pleasant Hill               Y
106468                 1                10    Pleasant Hill               Y
106474                 3                10    Pleasant Hill               Y
106526                 1                10    Pleasant Hill               Y
106530                 2                10    Pleasant Hill               Y
134812                 2                10          Belmont               Y
134815                 2                10          Belmont               Y
134820                 6                10          Belmont               Y
134822                 1                10          Belmont               Y
134825                 6                 9          Belmont               Y
134826                 2                 8          Belmont               Y
134827                 7                10          Belmont               Y
134829                11                10          Belmont               Y
                                   Hotel Name-Long_PL Shuttle Service_PL
105802                      HYATT house Pleasant Hill                  Y
105803                      HYATT house Pleasant Hill                  Y
105805                      HYATT house Pleasant Hill                  Y
105814                      HYATT house Pleasant Hill                  Y
105819                      HYATT house Pleasant Hill                  Y
105848                      HYATT house Pleasant Hill                  Y
105849                      HYATT house Pleasant Hill                  Y
105908                      HYATT house Pleasant Hill                  Y
105917                      HYATT house Pleasant Hill                  Y
105920                      HYATT house Pleasant Hill                  Y
105922                      HYATT house Pleasant Hill                  Y
105930                      HYATT house Pleasant Hill                  Y
105935                      HYATT house Pleasant Hill                  Y
105961                      HYATT house Pleasant Hill                  Y
105983                      HYATT house Pleasant Hill                  Y
106003                      HYATT house Pleasant Hill                  Y
106007                      HYATT house Pleasant Hill                  Y
106031                      HYATT house Pleasant Hill                  Y
106033                      HYATT house Pleasant Hill                  Y
106040                      HYATT house Pleasant Hill                  Y
106043                      HYATT house Pleasant Hill                  Y
106051                      HYATT house Pleasant Hill                  Y
106058                      HYATT house Pleasant Hill                  Y
106065                      HYATT house Pleasant Hill                  Y
106083                      HYATT house Pleasant Hill                  Y
106104                      HYATT house Pleasant Hill                  Y
106116                      HYATT house Pleasant Hill                  Y
106161                      HYATT house Pleasant Hill                  Y
106180                      HYATT house Pleasant Hill                  Y
106182                      HYATT house Pleasant Hill                  Y
106202                      HYATT house Pleasant Hill                  Y
106206                      HYATT house Pleasant Hill                  Y
106216                      HYATT house Pleasant Hill                  Y
106218                      HYATT house Pleasant Hill                  Y
106253                      HYATT house Pleasant Hill                  Y
106278                      HYATT house Pleasant Hill                  Y
106305                      HYATT house Pleasant Hill                  Y
106326                      HYATT house Pleasant Hill                  Y
106328                      HYATT house Pleasant Hill                  Y
106331                      HYATT house Pleasant Hill                  Y
106359                      HYATT house Pleasant Hill                  Y
106389                      HYATT house Pleasant Hill                  Y
106418                      HYATT house Pleasant Hill                  Y
106434                      HYATT house Pleasant Hill                  Y
106441                      HYATT house Pleasant Hill                  Y
106464                      HYATT house Pleasant Hill                  Y
106468                      HYATT house Pleasant Hill                  Y
106474                      HYATT house Pleasant Hill                  Y
106526                      HYATT house Pleasant Hill                  Y
106530                      HYATT house Pleasant Hill                  Y
134812             HYATT house Belmont/Redwood Shores                  Y
134815             HYATT house Belmont/Redwood Shores                  Y
134820             HYATT house Belmont/Redwood Shores                  Y
134822             HYATT house Belmont/Redwood Shores                  Y
134825             HYATT house Belmont/Redwood Shores                  Y
134826             HYATT house Belmont/Redwood Shores                  Y
134827             HYATT house Belmont/Redwood Shores                  Y
134829             HYATT house Belmont/Redwood Shores                  Y

提前感谢您的时间和精力!

1 个答案:

答案 0 :(得分:1)

如果没有一个易于使用的数据集或图表来准确理解你做错了什么,我不得不在这里稍微猜测一下。这是一个示例数据框。

df <- data.frame(name = rep(c("One", "Two", "One", "Two"), 6),
             group = c("Red", "Blue", "Blue", "Blue", "Red", "Red"))

您需要在geom_text堆栈中制作标签,以便为每个条形堆叠它们。通过添加position=position_stack(vjust = 0.5),它将在每个条形图的中心绘制标签:

library(ggplot2)
ggplot(df, aes(x=name, y = group, fill= group)) +
  geom_bar(aes(y=(..count..)/sum(..count..))) +
  geom_text(aes(y = ((..count..)/sum(..count..)),
                label = scales::percent((..count..)/sum(..count..))), 
            stat = "count", 
            position=position_stack(vjust = 0.5))

enter image description here

如果您不希望它们堆叠,您需要更改geom_bargeom_text参数:

library(ggplot2)
ggplot(df, aes(x=name, y = group, fill= group)) +
  geom_bar(aes(y=(..count..)/sum(..count..)), position = position_dodge()) +
  geom_text(aes(y = ((..count..)/sum(..count..)),
                label = scales::percent((..count..)/sum(..count..))), 
            stat = "count", 
            position = position_dodge(width = 1), vjust = -1)

enter image description here