1

My function:

Fluorescence_Intensity <- function(X, MEAN, SD, METHOD){
  if(METHOD=="rnorm"){
    res <- rnorm(X, MEAN, SD)
  }
  if(METHOD=="dnorm"){
    res <- dnorm(X, MEAN, SD)
  }
  if(METHOD=="qnorm"){
    res <- qnorm(X, MEAN, SD, METHOD, LOWER.TAIL=NULL, LOG.P=NULL)
  }
  print(res)
}

I have code that generates histograms for me:

hist(Fluorescence_Intensity(X=1000, MEAN=2, SD=1, METHOD="rnorm"),xlim=c(0,40),col=scales::alpha('gray70',.4),border=F)
hist((Fluorescence_Intensity(X=1000, MEAN=30, SD=1, METHOD="rnorm")),add=T,col=scales::alpha('gray70'),border=F)

I want to correct my graphs so that, as in the picture, they show the mean green for which the histograms cut off at 95th percentile in blue for a lighter histogram as shown in the picture.I do not know how to do it, so please help me. I don't need colourful background.

plot

Daisy
  • 31
  • 5
  • kindly please help( – Daisy Nov 03 '22 at 15:40
  • `Fluorescence_Intensity` is not a base R function. When using functions that are not base R functions please start the scripts with a call to `library(pkgname)` in order to load the packages needed. – Rui Barradas Nov 03 '22 at 15:44
  • Without knowing function `Fluorescence_Intensity`, it seems that you should first save its return values in two vectors, for instance, `x` and `y`, and compute the statistics you want, `mean` and `quantile(x, 0.95)`. Then plot the statistics with `abline(v = c(x.bar, y.bar, x.q95), col = c("green", "green", "blue"))` – Rui Barradas Nov 03 '22 at 15:47
  • sorry to answer only now I didn't have internet access, my function is: Fluorescence_Intensity <- function(X, MEAN, SD, METHOD){ if(METHOD=="rnorm"){ res <- rnorm(X, MEAN, SD) } if(METHOD=="dnorm"){ res <- dnorm(X, MEAN, SD) } if(METHOD=="qnorm"){ res <- qnorm(X, MEAN, SD, METHOD, LOWER.TAIL=NULL, LOG.P=NULL) } print(res) } – Daisy Nov 04 '22 at 07:00
  • can I ask for help again now with shown function please? – Daisy Nov 04 '22 at 07:12

1 Answers1

1

Here are two ways.

The first is exactly like I said in my comment to the question. Compute the statistics to be plotted and then plot the vertical lines with abline. The rest of the code is almost a copy&paste from the question.

Fluorescence_Intensity <- function(X, MEAN, SD, METHOD){ 
  if(METHOD=="rnorm"){ 
    res <- rnorm(X, MEAN, SD) 
  } 
  if(METHOD=="dnorm"){ 
    res <- dnorm(X, MEAN, SD) 
  } 
  if(METHOD=="qnorm"){ 
    res <- qnorm(X, MEAN, SD, METHOD, LOWER.TAIL=NULL, LOG.P=NULL) 
  } 
  res
}

set.seed(2022)  # make the code reproducible
x <- Fluorescence_Intensity(X = 1000, MEAN = 2, SD = 1, METHOD = "rnorm")
y <- Fluorescence_Intensity(X = 1000, MEAN = 30, SD = 1, METHOD = "rnorm")

x.bar <- mean(x)
y.bar <- mean(y)
x.q95 <- quantile(x, 0.95)

hist(x, xlim = c(0, 40), col = scales::alpha('gray70', 0.4), border = FALSE)
hist(y, add = TRUE, col = scales::alpha('gray70'), border = FALSE)
abline(v = c(x.bar, y.bar, x.q95), col = c("green", "green", "blue"), lwd = 2)

Created on 2022-11-04 with reprex v2.0.2

The second way draws the blue background rectangle.

  1. Create a hist object for each of the variables;
  2. get the axis limits, the x axis directly from the data, the y axis limits from those hist objects;
  3. open the plot with the right dimensions but plot nothing (type = "N");
  4. draw the grid lines;
  5. draw the background rectangle;
  6. and finally the histograms and vertical lines.

Note that the histograms code is a simplified version of the question's code. Above I have left it as is but there is no need for calls to scales::alpha.

hx <- hist(x, plot = FALSE)
hy <- hist(y, plot = FALSE)

xlim <- range(c(x, y))
ylim <- c(0, max(hx$counts, hy$counts))

plot(1, type = "n", xlim = xlim, ylim = ylim, 
     xlab = "Fluorescence Intensity", ylab = "Frequency")
grid()
rect(xlim[1] - 10, -10, x.q95, ylim[2] + 10, col = scales::alpha('lightblue', 0.4), border = FALSE)

hist(x, add = TRUE, col = 'gray70', border = FALSE)
hist(y, add = TRUE, col = 'gray70', border = FALSE)
abline(v = c(x.bar, y.bar, x.q95), col = c("green", "green", "blue"), lwd = 2)

Created on 2022-11-04 with reprex v2.0.2

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • The last question, what should I do, so that Fluorescence_Intensity appears in the place of x and index under the graphs and in 2 variations of histogtams instead of 1, frequency appears? – Daisy Nov 04 '22 at 07:56
  • @Daisy Use arguments `xlab = "Fluorescence Intensity"` and `ylab = "Frequency"`. – Rui Barradas Nov 04 '22 at 09:14
  • Can you be more precise, please? Where exactly should I put xlab = "Fluorescence Intensity" and ylab = "Frequency" in the codes because I try to insert it but it doesn't show up. – Daisy Nov 04 '22 at 09:26
  • @Daisy Put them in the initial `plot` call. Done, see the edit. – Rui Barradas Nov 04 '22 at 09:39