R ggplot barchat无法创建正确的标签

时间:2016-02-21 18:04:32

标签: r ggplot2

我想在条形图中显示公司名称作为标签,在5年内绘制前10名财富500强公司的条形图。我面临以下问题: 1. y轴上的每个等级应该是相同的大小,并从顶部开始按1到10排序 2.更好地显示公司名称标签。

我在问题Plot a 'top 10' style list/ranking in R based on numerical column of dataframe上发现了这个问题,但它使用数值绘制。

这是我能够实现的目标:

# 2011 to 2015 top 10 Fortune 500 companies
year <- c('2015',  '2015',  '2015', '2015', '2015',
          '2015',   '2015', '2015', '2015', '2015', 
          '2014',   '2014', '2014', '2014', '2014', 
          '2014',   '2014', '2014', '2014', '2014',
          '2013',   '2013', '2013', '2013', '2013', 
          '2013',   '2013', '2013', '2013', '2013',
          '2012',   '2012', '2012', '2012', '2012',
          '2012', '2012',   '2012', '2012', '2012',
          '2011',   '2011', '2011', '2011', '2011', 
          '2011',   '2011', '2011', '2011', '2011'
          )
rank <- c('1',  '2',    '3',    '4',    '5',    '6',    '7',    '8',    '9',    '10',
          '1',  '2',    '3',    '4',    '5',    '6',    '7',    '8',    '9',    '10',
          '1',  '2',    '3',    '4',    '5',    '6',    '7',    '8',    '9',    '10',
          '1',  '2',    '3',    '4',    '5',    '6',    '7',    '8',    '9',    '10',   
          '1',  '2',    '3',    '4',    '5',    '6',    '7',    '8',    '9',    '10'
        )
companyname <- c('Walmart',  'Exxon Mobil', 'Chevron',  'Berkshire Hathaway',   'Apple',
                 'General Motors',  'Phillips 66',  'General Electrics',    'Ford Motor',   'CVS Health',
                 'Walmart', 'Exxon Mobil',  'Chevron',  'Berkshire Hathaway',   'Apple',
                 'Phillips 66', 'General Motors',   'Ford Motor',   'General Electrics',    'Valero Energy',
                 'Walmart', 'Exxon Mobil',  'Chevron',  'Phillips 66',  'Berkshire Hathaway',
                 'Apple',   'General Motors',   'General Electrics',    'Valero Energy',    'Ford Motor',
                 'Exxon Mobil', 'Walmart',  'Chevron',  'Conoco Phillips',  'General Motors',   
                 'General Electrics',   'Berkshire Hathaway',   'Fannie Mae',   'Ford Motor',   'Hewlett-Packard Company',
                 'Walmart', 'Exxon Mobil',  'Chevron',  'Conoco Phillips',  'Fannie Mae',   
                 'General Electrics',   'Berkshire Hathaway',   'General Motors',   'Bank of America Corporation',
                 'Ford Motor'
)
companysymbol <- c('WMT',  'XOM',   'CVX',  'BRK',  'AAPL', 'GM',   'PSX',  'GE',   'F',    'CVS',
                   'WMT',   'XOM',  'CVX',  'BRK',  'AAPL', 'PSX',  'GM',   'F',    'GE',   'VLO',
                   'WMT',   'XOM',  'CVX',  'PSX',  'BRK',  'AAPL', 'GM',   'GE',   'VLO',  'F',
                   'XOM',   'WMT',  'CVX',  'COP',  'GM',   'GE',   'BRK',  'FNMA', 'F',    'HPQ',
                   'WMT',   'XOM',  'CVX',  'COP',  'FNMA', 'GE',   'BRK',  'GM',   'BAC',  'F'
                 )
fortune500 <- as.data.frame(cbind(year,rank,companyname,companysymbol))

library(ggplot2)

p =  ggplot(fortune500,aes(x = year, y = rank, fill = as.factor(rank), label=(companyname))) +
  geom_bar(stat = "identity") +
  geom_text()

# PNG output
png(filename = "result/Fortune500.png")
# Render a barplot
print(p)
dev.off()

输出: enter image description here

1 个答案:

答案 0 :(得分:1)

尝试

ggplot(fortune500,aes(x = year, y = 1, fill = as.factor(rank), label=(companyname))) +
  geom_bar(stat = "identity", position="stack") +
  geom_text(position="stack", vjust=1)