按y轴值

时间:2015-08-30 20:35:20

标签: rcharts

我正在使用multiBarHorizontalChart和nplot()来显示平均速率的差异。我通过“posneg”变量进行分组,以红色突出显示“负变化”条,以绿色突出显示正变化率条。但是,当我按此变量分组时,y轴上的年份不再排序。任何想法如何保持这个年份的顺序,同时仍然按这个变量分组?就个人而言,我认为色差使图表更易于理解。这是一个可重复的例子,使用Socrata上托管的数据:

install.packages("RSocrata")
library(RSocrata)

url="https://opendata.socrata.com/dataset/Preliminary-Data-Data-Visulaization-Project-8-12-1/4xgc-ygke"

dfRatePer100= read.socrata(url)

dfRatePer100=subset(dfRatePer100, select=c(1,3), Year!="NA")

colnames(dfRatePer100)= c("Year", "Dollar.Rate")

dfRatePer100$Dollar.Rate= as.numeric(dfRatePer100$Dollar.Rate, 3)
dfRatePer100$mean= mean(dfRatePer100$Dollar.Rate)

dfRatePer100=dfRatePer100%>%
  mutate(rateVariance= Dollar.Rate - mean) %>%
  arrange(desc(Year))

dfRatePer100$PosNeg=ifelse(dfRatePer100$rateVariance>0, "Positive rate change from mean", ifelse(dfRatePer100$rateVariance<0, "Negative rate change from mean", "No change from mean"))

ratePer100 <- nPlot(rateVariance~ Year, group="PosNeg",data =     dfRatePer100,type = 'multiBarHorizontalChart')
ratePer100$chart(showLegend=T)
ratePer100$chart(showControls=F)
ratePer100$chart(color = c('green','red'))
ratePer100$yAxis(axisLabel='Variance from mean rate (in dollars)')
ratePer100$yAxis(tickFormat = "#! function(d) {return d3.format('.2f')(d)} !#")
ratePer100$set(width=600)
ratePer100

我感谢任何帮助!感谢。

1 个答案:

答案 0 :(得分:0)

不是答案,而是建议,因为查看源代码,nvd3 multiBarHorizontalChart将首先按组分组,然后按值排序,因此不要认为可能。 <{1}}可能是一个不错的选择,如果rCharts不是必需的。

library(rCharts)

df <- data.frame(
  year = as.character(2000:2012)
  ,value = runif(13,-1,1)
)
df$group <- ifelse(df$value>0,"positive","negative")

np <-nPlot(
  value ~ year,
  group = "group",
  data = df,
  type = 'multiBarHorizontalChart'
)
np$chart(color = c('green','red'))
np

library(taucharts)
tauchart( df ) %>%
  tau_bar( "value", "year", "group", horizontal=TRUE) %>%
  tau_legend()