Home > Back-end >  R plots histograms
R plots histograms

Time:11-05

I have code.

V <- function(C, E, HS, EC_50) {
  response <- E   (1 - E) / (1   exp(HS * (C - EC_50)))
  list(response = response,
       mean_to_hist = mean(response),
       sd_to_hist = 1)
}

example1 <- V(seq(-0.1, 0, by = 0.01),0,1,log(1e-3))
example2 <-V(seq(-2500, 0, by = 0.01),0,1,log(1e-3))

Fluorescence_Intensity <- function(my_V, METHOD, ...){ 
  fun <- METHOD
  fun(mean = my_V$mean_to_hist, sd = my_V$sd_to_hist, ...) 
}

x <- Fluorescence_Intensity(example1, rnorm, n = 1000)
y <- Fluorescence_Intensity(example2, rnorm, n = 1000)

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)

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 = scales::alpha('gray70', 0.4), 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)

I want to change it so that only the darker histogram along with the greener mean bar is visible, but the blue bar and the background are in the exact same place. (a light histogram creation may in the code as long as it is not visible).

CodePudding user response:

Is it solving your issue? (removed light histogram and its associated mean as abline)

example1 <- V(seq(-0.1, 0, by = 0.01),0,1,log(1e-3))
example2 <-V(seq(-2500, 0, by = 0.01),0,1,log(1e-3))

Fluorescence_Intensity <- function(my_V, METHOD, ...){ 
  fun <- METHOD
  fun(mean = my_V$mean_to_hist, sd = my_V$sd_to_hist, ...) 
}

x <- Fluorescence_Intensity(example1, rnorm, n = 1000)
y <- Fluorescence_Intensity(example2, rnorm, n = 1000)



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)


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(y, add = TRUE, col = 'gray70', border = FALSE)
abline(v = c(y.bar, x.q95), col = c("green", "blue"), lwd = 2)

enter image description here

  • Related