'Vary colors in ggExtra marginal histogram
I'm analyzing time series of blood pressure readings. Blood pressure readings conventionally fall into six categories according to the level of each reading: Low, Normal, Elevated, Stage 1, Stage 2, and Crisis.
ggplot from the ggplot2 package make it easy to color points in the scatterplot according to the conventional categories, and the ggMarginal function in the ggExtra package provides a useful visual summary by adding a histogram along the y-axis of plots of the time series.
But the histogram's bars are all one color, and I see no way to color the histogram's bars to reflect the corresponding categories.
Can you suggest any way to do this?
Solution 1:[1]
I would recommend the use of the ggside package which can be a more expressive alternative to ggExtra's ggMarginal. I would recommend looking at this vignette for more information.
library(ggside)
#> Loading required package: ggplot2
#> Registered S3 method overwritten by 'ggside':
#> method from
#> +.gg ggplot2
ggplot(mpg, aes(displ, hwy, colour = class, fill = class)) +
geom_point(size = 2) +
geom_xsidehistogram(position = "stack") +
geom_ysidehistogram(position = "stack") +
theme(axis.text.x = element_text(angle = 90, vjust = .5))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2022-02-01 by the reprex package (v2.0.1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Justin Landis |
