使用ggplot在R中创建分组条形图

时间:2016-04-02 14:33:31

标签: r ggplot2

我正在尝试使用ggplot创建一个分组的条形图,因为它产生的美学质量更加美观。我有一个数据帧,一起,包含每个值的值和名称,但我无法设法创建情节呢?数据帧如下

          USperReasons      USperReasonsNY                 USuniquNegR
1    0.198343304187759   0.191304347826087                 Late Flight
2     0.35987114588127   0.321739130434783      Customer Service Issue
3   0.0667280257708237    0.11304347826087                Lost Luggage
4   0.0547630004601933 0.00869565217391304     Flight Booking Problems
5    0.109065807639208   0.121739130434783                  Can't Tell
6  0.00460193281178095                   0             Damaged Luggage
7   0.0846755637367694  0.0782608695652174            Cancelled Flight
8   0.0455591348366314  0.0521739130434783                  Bad Flight
9   0.0225494707777266  0.0347826086956522                   longlines
10  0.0538426138978371  0.0782608695652174 Flight Attendant Complaints

我尝试了所有错误的不同方法,其中一个例子在

之下
ggplot(together,aes(USuniquNegR, USperReasons,USperReasonsNY))+ geom_bar(position = "dodge") 

谢谢, 艾伦。

1 个答案:

答案 0 :(得分:1)

df <- reshape2::melt(together, 3)

ggplot(reshape2::melt(df, 3), 
    aes(USuniquNegR, value, fill = variable)) + 
        geom_bar(stat = 'identity', position = 'dodge') + 
        coord_flip() + 
        theme(legend.position = 'top')

Output Plot