简体   繁体   English

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

[英]Create a grouped barplot in R using ggplot

I'm trying to create a grouped barplot using ggplot due to the more aesthetically pleasing quality it produces.我正在尝试使用 ggplot 创建一个分组的条形图,因为它产生的质量更美观。 I have a dataframe, together, containing the values and the name of each value but I can't manage to create the plot it?我有一个数据框,其中包含每个值的值和名称,但我无法创建绘图吗? the dataframe is as follows数据框如下

          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

I tried different methods with errors in all, one such example is below我尝试了不同的方法,但都有错误,下面是一个这样的例子

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

Thanks, Alan.谢谢,艾伦。

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')

输出图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM