I am new to piping and dplyr in R and need some help. Please note- I have a solution for this question using cut function.
CodePudding user response:
It sounds like you want to see frequency of each status level 1:3
as a bar plot for each range of reference values. One option here is to use faceting to split out a new graph for each range of interest. To generate the splits, an alternative to base::cut()
is plyr::round_any()
. In my example I split into bins of 15 to make the graphic more simple but you can adjust to suit.
Note: it is often undesirable to load the {plyr} library due to conflicts with {dplyr} functions that share a name. Therefore you may want to just call this one function explicitly or define it manually in your script as defined here.
library(tidyverse)
d <- structure(list(reference = c(1.2, 0.2, 0.6, 1.6, 1, 1, 0.4, 0.4, 0.8, 0.8, 0.4, 0.2, 15.8, 59.2, 55.4, 54.8, 54.6, 54.2, 49, 53, 47.2, 44, 40.2, 39, 34.2, 35.8, 33.4, 30.6, 29.4, 29.2, 27.6, 24.8, 24, 22, 21.2, 20.6, 18.6, 18, 17, 17.2, 14.8, 15.2, 13.2, 13.4, 12, 11.8, 11, 10.8, 10, 9.2, 8.8, 8.8, 8.4, 7.8, 7.6, 6.6, 6.4, 6.2, 6, 5.8, 5.4, 5, 4.8, 4.4, 4.2, 4, 3.8, 3.6, 3.6, 3.6, 3, 2.8, 3, 2.8, 2.6, 2.4, 2.4, 2.2, 2, 2.2, 2.2, 1.8, 1.8, 1.6, 1.8, 1.8, 2.2, 71.2, 75.8, 74.6, 74.6, 74.2, 67.2, 66.2, 60.6, 60.6, 54.8, 53.6, 48.4, 45.2),
sensor1_status = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3)), row.names = 113:212, class = "data.frame")
d %>%
mutate(ref_range = plyr::round_any(reference, accuracy = 15, f = ceiling)) %>%
ggplot(aes(sensor1_status))
geom_bar()
facet_grid(rows = "ref_range", scales = "free_y")
Created on 2022-07-05 by the reprex package (v2.0.1)