如何将刻面条形图值显示为负数

时间:2014-02-17 18:19:46

标签: r ggplot2

我正在努力制作一个垂直切面的条形图。我在下面添加了一个“迄今为止”的版本。我的主要问题是负值没有像我期望的那样显示出来。不应该有一些行,或勾选,表示0,负条在其下面注册?以下代码应完全可重现。你可以在我试图绘制的最终数据集中看到几个负值。我得到一个相当冗长的错误,以“Mapping a variable to y and also using stat="bin".”开头,我觉得这可能与我的问题有关,但我无法找到或得出具体的解决方案。

另外,作为次要点,如果任何人有任何建议超过当前的障碍,我的目标最终结果是将那些负极条颜色设为红色,将正面颜色设为绿色,以将“spdrNames”添加到y轴,用实际值标记条形,并从x轴移除难以辨认的值。

require('ggplot')
require('reshape')
require('tseries')

spdrTickers = c('XLY','XLP','XLE','XLF','XLV','XLI','XLB','XLK','XLU')
spdrNames = c('Consumer Discretionary','Consumer Staples', 'Energy', 
    'Financials','Health Care','Industrials','Materials','Technology',
    'Utilities')

latestDate =Sys.Date()

dailyPrices = lapply(spdrTickers, function(ticker) get.hist.quote(instrument= ticker, start = "2012-01-01", 
                end = latestDate, quote="Close", provider = "yahoo", origin="1970-01-01", compression = "d", retclass="zoo"))

perf5Day = lapply(dailyPrices, function(x){(x-lag(x,k=-5))/lag(x,k=-5)})
perf20Day = lapply(dailyPrices, function(x){(x-lag(x,k=-20))/lag(x,k=-20)})
perf60Day = lapply(dailyPrices, function(x){(x-lag(x,k=-60))/lag(x,k=-60)})

names(perf5Day) = spdrTickers
names(perf20Day) = spdrTickers
names(perf60Day) = spdrTickers

perfsMerged = lapply(spdrTickers, function(spdr){merge(perf5Day[[spdr]],perf20Day[[spdr]],perf60Day[[spdr]])})
perfNames = c('1Week','1Month','3Month')
perfsMerged = lapply(perfsMerged, function(x){
  names(x)=perfNames
  return(x)
  })

latestDataPoints = t(sapply(perfsMerged, function(x){return(x[nrow(x)])}))
latestDataPoints = data.frame(cbind(spdrTickers,latestDataPoints))
names(latestDataPoints) = c('Ticker', '1Week','1Month','3Month')

drm = melt(latestDataPoints, id.vars=c('Ticker'))
names(drm) = c('Ticker','Period','Value')

p = ggplot(drm, aes(x=Ticker,y=Value)) + geom_bar() + coord_flip() + facet_grid(. ~ Period)

产生这个:

enter image description here

1 个答案:

答案 0 :(得分:1)

不知何故,您已将Value-values转换为一个因子:

str(drm)
'data.frame':   27 obs. of  3 variables:
 $ Ticker: Factor w/ 9 levels "XLB","XLE","XLF",..: 9 6 2 3 8 4 1 5 7 9 ...
 $ Period: Factor w/ 3 levels "1Week","1Month",..: 1 1 1 1 1 1 1 1 1 2 ...
 $ Value : Factor w/ 27 levels "0.0164396430248944",..: 2 4 5 1 8 3 7 6 9 11 ...

可能发生在这里:

latestDataPoints = data.frame(cbind(spdrTickers,latestDataPoints))
> str( latestDataPoints )
'data.frame':   9 obs. of  4 variables:
 $ Ticker: Factor w/ 9 levels "XLB","XLE","XLF",..: 9 6 2 3 8 4 1 5 7
 $ 1Week : Factor w/ 9 levels "0.0164396430248944",..: 2 4 5 1 8 3 7 6 9
 $ 1Month: Factor w/ 9 levels "-0.00139291932675571",..: 2 3 1 5 8 4 6 7 9
 $ 3Month: Factor w/ 9 levels "-0.0110357512357742",..: 3 2 1 5 9 6 7 8 4

在该步骤之前,您有一个来自t(sapply(perfsMerged, function(x){return(x[nrow(x)])}))

的数字矩阵

然后这样做:

latestDataPoints[2:4] <- lapply( latestDataPoints[2:4], function(x) 
                                         as.numeric(as.character(x)) )
drm = melt(latestDataPoints, id.vars=c('Ticker'))
names(drm) = c('Ticker','Period','Value')

p = ggplot(drm, aes(x=Ticker,y=Value)) + geom_bar() + coord_flip() + 
                                                      facet_grid(. ~ Period)
png();print(p);dev.off()

产地:

enter image description here

构造data.frame(cbind(...))是一个真正的陷阱。我已经看到被所谓的权威来源使用,它是一个经常出现的困惑来源。我认为如果解释器只是用红色突出显示该组合(以及应用于因子的as.numeric),R将是一种更安全的语言。当你cbind一个字符向量到数字矩阵时,你得到一个全部字符矩阵。