简体   繁体   English

R-一个图中有多个图,但是图上重叠部分的透明度不起作用

[英]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. 我正在R中创建一些海拔数据的频率/密度图。下面的代码非常适合在一张图中获取两个不同的变量,每个密度和频率。 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. 问题是,条形图彼此重叠,我无法正确设置“ alpha”的透明度设置。 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. 我已经在代码中的不同位置尝试了alpha函数,但是没有用。

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. 考虑使用允许r参数的?rgb Also, for better color comparison use the same color of differing alpha values. 另外,为了更好地进行颜色比较,请使用不同alpha值的相同颜色。

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)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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