these are the code segments, because I don't know how to upload data, so no data provided.
code1:
ggplot(overall_hist,aes(x=Rate, y=..count..)) +
geom_histogram(bins=50, fill="steelblue", color="white") +
geom_vline(aes(xintercept = ref), linetype="dashed", size=1.0) +
xlim(30,50) + ylim(0,50)
code2:
ggplot(strata_hist,aes(x=Rate, y=..count..)) +
geom_histogram(bins=50, fill="steelblue", color="white") +
xlim(30,50) + ylim(0,50) +
geom_vline(aes(xintercept = Rate), strata_ref, linetype="dashed", size=1.0) +
facet_wrap(~Strata, scales="free_y")
My questions are
- in general , how to set up maximum value for histogram y axis based on data
- to generate histograms by above codes, the code1 and code2 syntax are similar.
Because the data structure is different, the first 2 lines of ggplot statements
are the same, the 3rd and 4th lines are different. Is it possible to generate a function and based on
same flag to perform the function differently? Pseudo-code like such
draw_hist <- function(ds,flag){
ggplot(ds,aes(x=Rate, y=..count..)) +
geom_histogram(bins=50, fill="steelblue", color="white") +
xlim(30,50) + ylim(0,50) +
if flag=='O'{
geom_vline(aes(xintercept = ref), linetype="dashed", size=1.0)
}else{
geom_vline(aes(xintercept = Rate), strata_ref, linetype="dashed", size=1.0) +
facet_wrap(~Strata, scales="free_y")
to use the function like such:
draw_hist(overall_hist, 'O')
draw_hist(strata_hist, 'S')
output from code1
output from code2