简体   繁体   English

着色 R 中的可格式化数据表 - 股票数据

[英]Coloring Formattable Data Table in R - Stock Data

I have a datatable storing the daily/weekly/monthly returns for several stocks that I am trying to color with R's formattable package.我有一个数据表,存储了几只股票的每日/每周/每月回报,我试图用 R 的 formattable 包对这些股票进行着色。 I want negative returns to be red, and positive returns to be green.我希望负回报为红色,正回报为绿色。 Or to get fancy, have a separate gradient for positive and negative values - so if Apple was down 10% for a day it would be dark red, and if Amazon was down 2%, that cell would be a lighter red - and vice versa for positive returns (light and dark green).或者为了花哨,为正值和负值设置单独的渐变——所以如果苹果一天下跌 10%,它将是深红色,如果亚马逊下跌 2%,该单元格将是浅红色——反之亦然获得正回报(浅绿色和深绿色)。

My issue is that when I use a red-green gradient, the colors in the middle appear brown-ish colored.我的问题是,当我使用红绿色渐变时,中间的颜色呈现褐色。 The code I have below has transparent/white as the low end of the gradient, and green as the upper end, but is pretty tough to differentiate.我下面的代码将透明/白色作为渐变的低端,绿色作为高端,但很难区分。

My datatable looks something like:我的数据表看起来像:

Stock day  week   month
AAPL   1.5  3.2   10.6
AMZN   3.2  5.3   4.4
BA    -2.1 -4.0  -10.5
PYPL   -5  -8.5  -12.1



Green <- "#71ca99"

sign_formatter <- formatter("span", 
                            style = x ~ style(color = ifelse(x > 0, "green", 
                                                             ifelse(x < 0, "red", "black"))))
sign_formatter(c(-1, 0, 1))

returns <- formattable(stocks_df, align =c("l","c","c","c","c"), list(
  `Stock` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")), 
  daily_return = color_tile("transparent", Green),
  week_return = color_tile("transparent", Green),
  month_return = color_tile("transparent", Green)))

@dc37 @dc37

I am using the exact code you suggested - the colors are working perfectly.我正在使用您建议的确切代码 - 颜色工作正常。 But when I sort by weekly return like in the example below, to get stocks with the best performance for the week, it is not sorting highest to lowest (having the same issue when I try to sort by worst performing stocks).但是,当我像下面的例子一样按每周回报排序时,为了获得本周表现最好的股票,它不是从高到低排序(当我尝试按表现最差的股票进行排序时,也有同样的问题)。

Here is a picture of what my formattable output looks like.这是我的可格式化输出的图片。 I am also running this in Shiny, not sure if that is what could be throwing the error with sorting.我也在 Shiny 中运行它,不确定这是否会引发排序错误。

在此处输入图片说明

A possible solution will be to create multiple ifelse statement in order to set 5 different colors (red / lightred / white / lightgreen / green) in function of the value of each columns.一个可能的解决方案是创建多个ifelse语句,以便根据每列的值设置 5 种不同的颜色(红色/浅红色/白色/浅绿色/绿色)。

Here, a possible way of doing it:在这里,一种可能的方法:

Test <- formatter("span",
                  style = x ~ style(display = "block", font.weight = "bold",color = "black","border-radius" = "4px",
                                 "padding-right" = "4px",
                                 "background-color" = ifelse(x <= -10, "red", ifelse(x > -10 & x <= -2, "tomato", ifelse(x > -2 & x <= 2, "white", ifelse(x > 2 & x <= 10, "palegreen",ifelse(x > 10, "green",NA)))))),
                  x ~ percent(x/100))

formattable(stocks_df,align =c("l","c","c","c","c"), list(
  `Stock` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")),
  day = Test,
  week = Test, 
  month = Test))

在此处输入图片说明

Does it answer your question ?它回答你的问题吗?

Reproducible example可重现的例子

structure(list(Stock = c("AAPL", "AMZN", "BA", "PYPL"), day = c(1.5, 
3.2, -2.1, -5), week = c(3.2, 5.3, -4, -8.5), month = c(10.6, 
4.4, -10.5, -12.1)), row.names = c(NA, -4L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x55b7eeb735c0>)

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

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