本文介绍了在R-库存数据中上色Formattable数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,该数据表存储了我正在尝试使用R的formattable程序包上色的几只股票的每日/每周/每月收益.我希望负收益为红色,正收益为绿色.或想想看,请为正负值分别设置一个梯度-因此,如果Apple一天下降10%,它将变成深红色,而如果Amazon下降2%,则该单元将变成更浅的红色-反之亦然获得正回报(浅绿色和深绿色).

I have a datatable storing the daily/weekly/monthly returns for several stocks that I am trying to color with R's formattable package. 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).

我的问题是,当我使用红绿色渐变时,中间的颜色显示为褐色.我下面的代码将透明/白色作为渐变的低端,将绿色作为上端,但是很难区分.

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.

我的数据表如下:

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

我使用的是您建议的确切代码-颜色运行正常.但是,当我按照下面的示例对每周收益进行排序时,要使本周表现最佳的股票并不是按从高到低的顺序排序(当我尝试按表现最差的股票排序时,也会遇到同样的问题).

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

这是我的formattable输出看起来的图片.我也在Shiny中运行此命令,不确定是否会导致排序错误.

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.

推荐答案

可能的解决方案是创建多个 ifelse 语句,以设置5种不同的颜色(红色/浅红色/白色/浅绿色/绿色)表示每列的值.

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.

在这里,一种可行的方法:

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

它能回答您的问题吗?

可复制的示例

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

这篇关于在R-库存数据中上色Formattable数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:43