r - R绘图表的条件格式
问题描述
我想有条件地格式化表格中的行。如果一列有 1,则该列应突出显示,否则不突出显示。我想在情节中做到这一点。数据如下:
Name Purchased
a 0
b 1
c 0
d 1
我想突出显示名称为 b 和 d 的行。
我正在使用以下类型的代码:
plot_ly(
type = 'table',
columnwidth = c(100, 100, 100),
columnorder = c(0, 1, 2),
header = list(
values = c(A,B),
align = c("center", "center"),
line = list(width = 1, color = 'black'),
fill = list(color = c("grey", "grey")),
font = list(family = "Arial", size = 14, color = "white")
),
cells = list(
values = rbind(df[[A]],df[[B]]),
align = c("center", "center"),
line = list(color = "black", width = 1),
font = list(family = "Arial", size = 12, color = c("black"))
))
我正在为 R 闪亮的应用程序编写此代码。请让我知道我该怎么做。
解决方案
我会highlight
在原始数据集中为必须突出显示的每一行添加一个变量。
df <- df %>%
mutate(highlgt = ifelse(Purchased == 1, "lightblue", "white"))
然后,在cells
参数中,使用fill
值列表更新highlight
值。
(我还更改了header/values
andcells/values
值以避免错误消息)。
plot_ly(
type = 'table',
columnwidth = c(100, 100),
#columnorder = c(0, 1, 2),
header = list(
values = c("A","B"),
align = c("center", "center"),
line = list(width = 1, color = 'black'),
fill = list(color = c("grey", "grey")),
font = list(family = "Arial", size = 14, color = "white")
),
cells = list(
values = rbind(df[[1]], df[[2]]),
align = c("center", "center"),
line = list(color = "black", width = 1),
font = list(family = "Arial", size = 12, color = c("black")),
fill = list(color = list(df$highlgt)) # highlight rows
))
推荐阅读
- ios13 - 与此版本的 iOS 链接时不再支持 UISearchDisplayController
- c# - 将一串空格分隔值解析为结构布局
- azure - 使用 Azure AD 角色或组来限制对某些页面的访问?
- java - use same while loop in different functions
- mysql - 分组时如何存储两种多态记录类型以获得最佳 MySQL 查询性能
- url - url 作为参数?在 ASP.NET 核心中
- java - 无法验证选中的复选框,Selenium Webdriver Java
- jenkins - Jenkins+RTC:是否有任何 jenkins 插件具有在不加载 RTC 服务器的情况下检查更改的功能
- python - 根据另一个文件中的字符串重命名文件夹中的文件-使用Python
- javascript - 有没有办法检查一个 npm 包是否需要发布,然后才发布?