set up maximum value for histogram y axis

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

  1. in general , how to set up maximum value for histogram y axis based on data
  2. 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
code1_output
output from code2
code2_output

You can include an excerpt of your data using dput(), or (my personal favorite option) datapasta.

I did a little write-up on the latter a couple years ago here:

With that done, you'll be able to turn this into a proper reprex (short for reproducible example).

install.packages("reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

There's also a nice FAQ on how to do a minimal reprex for beginners, below:

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.