简体   繁体   中英

R - multiple graphs in one plot, but transparency for overlying parts of graph not working

I am creating some frequency/density plots of altitude data in R. The code below is good for getting two different variables with each density and frequency in one graph. So in total its two bar graphs and two lines.

The problem is, that the bar graphs are overlying each other and I can not get the transparency setting with 'alpha' correct. This is were I need some help. I guess its a very simple problem.

I already tried the alpha function at different places within the code, but it did not work.

hist(Lake_DF1[[6]], col=c("#006CFF"), border = "black", prob = TRUE, 
     # show densities instead of frequencies
     breaks = 60, xlim = c(14,17), xlab = "Height [m]", main = "DTLB 6")

lines(density(na.omit(Lake_DF1[[6]])), lwd = 2)

hist(Buffer_DF1[[6]], col = c("#FF9900", alpha=0.4), border = "black", prob = TRUE, 
     # show densities instead of frequencies
     breaks = 60, xlim = c(14,17), add=TRUE)

lines(density(na.omit(Buffer_DF1[[6]])), lwd = 2)

The only result I could get were striped bars of the second variable instead of transparent ones, see picture below. I would like to have the orange bars transparent to see the blue bars and the line through. 在此处输入图片说明

Consider using ?rgb which allows the alpha argument. Also, for better color comparison use the same color of differing alpha values.

Blue

# CONVERSION: #006CFF --> rgb(0,108,255)
hist(Lake_DF1[[6]], col = rgb(0/255, 108/255, 255/255, 0.4), border = "black", prob = TRUE, 
     # show densities instead of frequencies
     breaks = 60, xlim = c(14,17), xlab = "Height [m]", main = "DTLB 6")

lines(density(na.omit(Lake_DF1[[6]])), lwd = 2)

hist(Buffer_DF1[[6]], col = rgb(0/255, 108/255, 255/255, 0.3), 
     border = "black", prob = TRUE, 
     # show densities instead of frequencies
     breaks = 60, xlim = c(14,17), add=TRUE)

蓝图输出


Orange

# CONVERSION: #FF9900 --> rgb(255,153,0)
hist(Lake_DF1[[6]], col = rgb(255/255, 153/255, 0/255, 0.4), border = "black", prob = TRUE, 
     # show densities instead of frequencies
     breaks = 60, xlim = c(14,17), xlab = "Height [m]", main = "DTLB 6")

lines(density(na.omit(Lake_DF1[[6]])), lwd = 2)

hist(Buffer_DF1[[6]], col = rgb(255/255, 153/255, 0/255, 0.3), 
     border = "black", prob = TRUE, 
     # show densities instead of frequencies
     breaks = 60, xlim = c(14,17), add=TRUE)

lines(density(na.omit(Buffer_DF1[[6]])), lwd = 2)

橙色图输出


Data

set.seed(8302019)
data_tools <- c("sas", "stata", "spss", "python", "r", "julia")

#################
### DATA BUILD
#################
Lake_DF1 <- data.frame(
  group = sample(data_tools, 500, replace=TRUE),
  int = sample(1:15, 500, replace=TRUE),
  num1 = rnorm(500),
  num2 = runif(500),
  num3 = rnorm(500),
  num4 = runif(500),
  num5 = rnorm(500),
  num6 = runif(500)
)

Buffer_DF1 <- data.frame(
  group = sample(data_tools, 500, replace=TRUE),
  int = sample(1:15, 500, replace=TRUE),
  num1 = runif(500, 14, 17),
  num2 = runif(500, 14, 17),
  num3 = runif(500, 14, 17),
  num4 = runif(500, 14, 17),
  num5 = runif(500, 14, 17),
  num6 = runif(500, 14, 17)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM