Change font style depending on value in flextable - r

I want to change font to bold and red if Value > 10. How to reach that in flextable?
My example:
file_path <- "c:\\temp\\test_table.docx"
df <- data.frame(
InstanceName = c("Instance1", "Instance2", "Instance3", "Instance4", "Instance5"),
Value = c(15, 5, 11, 0, 5)
)
table_to_save <- flextable(df)
save_as_docx(
table_to_save,
path = file.path(file_path)
)
What I want:

library(flextable)
library(magrittr)
df <- data.frame(
InstanceName = c("Instance1", "Instance2", "Instance3", "Instance4", "Instance5"),
Value = c(15, 5, 11, 0, 5)
)
flextable(df) %>%
color(i = ~ Value > 10, j = "Value", color = "red") %>%
bold(i = ~ Value > 10, j = "Value")
Note that all of this is documented here: https://ardata-fr.github.io/flextable-book/format.html#usual-functions-for-formatting

Related

How to add values ​between hours

I have code that breaks down hours with corresponding values ​​into quarters of an hour.
Unfortunately, when broken down into quarters of an hour, the values ​​are identical for the entire hour.
After adding quarters of an hour, I would also like to add values ​​between the original hours so that the graph is smooth and not sharp. How to do it, average it, interpolate it?
df <- data.frame(
h = 0:23,
x = c(22, 11, 5, 8 , 22, 88, 77, 7, 11, 5, 8 , 22, 88, 77, 11, 5, 8 , 22, 88, 77, 11, 5, 8 , 22))
library(dplyr)
library(stringr)
df %>%
data.frame(h = rep(df$h, each = 4), # quadruplicate rows
x = rep(df$x, each = 4)) %>% # quadruplicate rows
mutate(h.1 = str_pad(h.1, width = 2, side = "left", pad = "0"), # add leading '0'
qu = paste0(h.1, c(":00", ":15", ":30", ":45"))) %>% # create quarters
select( - c(h,x)) %>% # deselect obsolete cols
rename(c("h" = "h.1", "x" = "x.1"))
df %>%
ggplot() +
geom_point(aes(qu, x), color = "red", size = 2) +
labs(x= "", y = "",
title = "Example")
Here I make a "decimal hour" variable to simplify the calculations. We can also use hms::hms() to define a timestamp that ggplot2 can understand. I use base:approx here to interpolate between hourly points.
df2 <- df %>%
tidyr::uncount(4) %>% # make 4 copies of each row
mutate(h_dec = h + (0:3)/4,
h_time = hms::hms(hours = h_dec),
x = x * c(1, NA, NA, NA), # this is to make non-hourly into NA,
# so that approx only uses hourly
x_interp = approx(x = h, y = x, xout=h_dec)$y)
df2 %>%
ggplot() +
geom_point(aes(h_time, x_interp), color = "red", size = 2) +
labs(x= "", y = "",
title = "Example")

The dataframe with only 1 row can't display 3D bar whereas dataframe with more than one rows can display 3D bars

I am trying to produce 3d barchart with highcharter with z-axis label and x-axis label showing.
But when my dataframe has only one row the chart doesnot display the bars. If the dataframe consist of more than one row it displays properly.
I want to be able to display 1 row from the dataframe also in the chart.
Below is a reproducible example:
library(shiny)
library(highcharter)
shinyApp(
ui <- fluidPage(
fluidRow(
highchartOutput('chart')
)
),
server <- function(input, output, session) {
df <- data.frame(
Z1 = c(1, 2, 1, 4),
Z2 = c(2, 3, 2, 7),
Z3 = c(6, 3, 7, 4),
Z4 = c(3, 4, 1, 5)
)
df <- df[-(1:3),] #reduced dataframe to 1 row
dta <- lapply(seq(ncol(df)), function(x) {
list(
name = colnames(df)[x],
data = df[, x]
)
})
output$chart <- renderHighchart({
highchart() %>%
hc_chart(
type = "column",
options3d = list(
enabled = TRUE,
beta = 20,
alpha = 30,
depth = 400,
viewDistance = 10
)
) %>%
hc_xAxis(categories = row.names(df[1:nrow(df)-1,])) %>%
hc_zAxis(
min = 0,
max = 3,
categories = colnames(df)
) %>%
hc_plotOptions(
series = list(
depth = 100,
grouping = FALSE,
groupZpadding = 10
)
) %>%
hc_add_series_list(dta)
})
})

How can I overlay values on flextable minibars?

The closest I can get with flextable is this:
What I'd like to achieve is something like this - knocked up with ggplot2:
Any ideas? or is this a feature request?
Code used to generate flextable:
library(tibble)
library(flextable)
tib <- tibble(v1 = letters[1:4],
v2 = c(1, 3, 5, 2))
tib %>%
flextable()%>%
width(j = 1:2, width = c(0.5, 1.5)) %>%
mk_par(j = 2,
value = as_paragraph(as_chunk(v2, formater = function(x) sprintf("%.0f", x)),
" ",
minibar(value = v2, max = sum(v2))
),
part = "body")

How to do a semi circle donut with highcharter library?

I'm trying to do a semi circle donut with highcharter library but I only know how to do a pie chart. I know that with JS you can do it by adding "startAngle" and "endAngle" but I want to know how to do it with R:
A <- c("a", "b", "c", "d")
B <- c(4, 6, 9, 2)
C <- c(23, 26, 13, 15)
df <- data.frame(A, B, C)
highchart() %>%
hc_chart(type = "pie") %>%
hc_add_series_labels_values(labels = df$A, values = df$B)%>%
hc_tooltip(crosshairs = TRUE, borderWidth = 5, sort = TRUE, shared = TRUE, table = TRUE,
pointFormat = paste('<b>{point.percentage:.1f}%</b>')
) %>%
hc_title(text = "ABC",
margin = 20,
style = list(color = "#144746", useHTML = TRUE))
Thank you!
You can do something like this though not using Highcharts library.
library(tidyverse)
library(ggforce)
library(scales)
library(ggplot2)
# -------------------------------------------------------------------------
A <- c("a", "b", "c", "d")
B <- c(4, 6, 9, 2)
C <- c(23, 26, 13, 15)
df <- data.frame(A, B, C)
# Ensure A is a factor (we'll be using it to fill the pie)
df$A <- factor(df$A)
# compute the individual proportion in this case using var C
df$prop <- df$C/sum(df$C)
# compute the cumulative proportion and use that to plot ymax
df$p_end <- cumsum(df$prop)
# generate a y-min between 0 and 1 less value than p_end (using p_end)
df$p_start <- c(0, head(df$p_end ,-1))
# -------------------------------------------------------------------------
# plot
df %>%
mutate_at(c("p_start", "p_end"), rescale, to=pi*c(-.5,.5), from=0:1) %>%
ggplot +
geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = .5, r = 1, start = p_start, end = p_end, fill=A)) +
coord_fixed() +xlab("X_label") + ylab("Y_lablel") + guides(fill=guide_legend(title="Legend Title"))
Output
Hope that helps.
Try adding startAngle = -90, endAngle = 90 inside hc_add_series_labels_values.
Note as per the warning hc_add_series_labels_values is deprecated so suggest using hc_add_series.
highchart() %>%
hc_add_series(type = "pie", data = df, hcaes(x = A, y = B), startAngle = -90, endAngle = 90) %>%
hc_tooltip(pointFormat = '<b>{point.percentage:.1f}%</b>') %>%
hc_title(text = "ABC",
margin = 20,
style = list(color = "#144746", useHTML = TRUE))

echarts4r moving the plot down to make room for the legend

Is there a way to move the plot down so that there is some space between the legend and the plot area? Ideally have the chart area automatically spaced below the legend.
df <- data.frame(
x = seq(50),
y = rnorm(50, 10, 3),
z = rnorm(50, 11, 2),
w = rnorm(50, 9, 2)
)
df %>%
e_charts(x) %>%
e_line(w) %>%
e_line(y) %>%
e_line(z) %>%
e_legend(orient = 'vertical', left = 0, top = 0)
Use the e_grid function to adjust the "grid" on which the graph is plotted.
library(echarts4r)
df <- data.frame(
x = seq(50),
y = rnorm(50, 10, 3),
z = rnorm(50, 11, 2),
w = rnorm(50, 9, 2)
)
df %>%
e_charts(x) %>%
e_line(w) %>%
e_line(y) %>%
e_line(z) %>%
e_legend(
orient = 'vertical',
left = 0,
top = 0,
selectedMode = "single" # might be of use
) %>%
e_grid(left = 100, top = 5)
Plenty more options in the grid can be found here

Resources