I have an existing report that I want to convert into R.
It looks like this:
It has 5 distinct columns, there's the name, the horizontal bar chart, actual values, guideline values, and a time series spark style chart. I can produce all 5 of the bits, but I can't quite wrap my head around how to bring them together and line them up properly.
It looks like there are a number of possibilities all with pretty significant challenges:
- Put it in an RMarkdown table: Seems like it would help line things up but will require building the table row-by-row. And putting
ggplot
inside of a table seems to have some challenges - Build the whole exhibit in
ggplot
... lol wut?!?!? Not with myggplot
skills. Seems like a maintenance nightmare (we all know the chart will change over time) - Build it up using
gridExtra::tableGrob
like in this example which is sort of a helper for doing #2. Still seems non-trivial.
I'm sure there are other ways. Any ideas?
While I'm mostly looking for ideas of how to glue these bits together, if you want a reprex
, here's some play data and graphs:
library(tidyverse)
#> Warning: package 'tibble' was built under R version 3.5.2
df <- tibble(
day = rep(1:4, 2),
test = c(rep("test 1", 4), rep("test 2", 4)),
value = c(5, 6, 4, 3, 6, 7, 5, 2),
guideline = rep(10, 8)
)
for (thistest in unique(df$test)) {
df %>%
filter(test == thistest &
day == max(.$day)) %>%
mutate(per_limit = value / guideline) ->
t
print(paste(thistest, "value is:", t$value))
print(paste(thistest, "guideline is:", t$guideline))
print(paste("thermometer for", thistest))
ggplot(t, aes(x = test, y = per_limit)) +
geom_bar(stat = "identity") +
theme_minimal() +
ylim(0, 1) +
coord_flip() ->
g
print(g)
df %>%
filter(test == thistest) %>%
mutate(per_limit = value / guideline) ->
t2
print(paste("time series for", thistest))
ggplot(t2, aes(day, per_limit)) +
geom_line() +
ylim(0, 1) ->
g2
print(g2)
}
#> [1] "test 1 value is: 3"
#> [1] "test 1 guideline is: 10"
#> [1] "thermometer for test 1"
#> [1] "time series for test 1"
#> [1] "test 2 value is: 2"
#> [1] "test 2 guideline is: 10"
#> [1] "thermometer for test 2"
#> [1] "time series for test 2"
Created on 2019-01-27 by the reprex package (v0.2.1)