Grouped overlapping histograms #18
Answered
by
stefaneng
stefaneng
asked this question in
Show and tell
-
One feature from ggplot2 that is not obvious how to do in set.seed(13);
# Sample size
N <- 1000;
# Generate some data in two groups
data <- rbind.data.frame(
cbind.data.frame(value = rnorm(N), group = 1),
cbind.data.frame(value = rnorm(N, mean = 1.5), group = 2)
);
data$group <- as.factor(data$group)
library(ggplot2)
ggplot(data) +
geom_histogram(aes(x = value, fill = group), alpha = 0.5, position = 'identity')
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. Created on 2023-01-12 with reprex v2.0.2 |
Beta Was this translation helpful? Give feedback.
Answered by
stefaneng
Jan 12, 2023
Replies: 1 comment
-
A solution in set.seed(13);
# Sample size
N <- 1000;
# Generate some data in two groups
data <- rbind.data.frame(
cbind.data.frame(value = rnorm(N), group = 1),
cbind.data.frame(value = rnorm(N, mean = 1.5), group = 2)
);
null.sim <- rnorm(N);
alt.sim <- rnorm(N, mean = 2);
group1.col <- '#E69F00';
group2.col <- '#56B4E9';
histogram1 <- create.histogram(
x = data$value[data$group == 2],
type = 'count',
ylab.label = 'Count',
xlab.label = 'value',
col = scales::alpha(group1.col, alpha = 0.7),
xlimits = c(-5, 6),
# Add key to upper left corner
key = list(
corner = c(0,.95),
rect = list(
col = c(group1.col, group2.col)
),
text = list(c('Group 1', 'Group 2'))
)
);
histogram2 <- create.histogram(
x = data$value[data$group == 1],
type = 'count',
col = scales::alpha(group2.col, alpha = 0.7)
);
# Simply add the two histograms together
histogram1 + histogram2 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
stefaneng
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A solution in
BoutrosLab.plotting.general
is