Editing aesthetics of my Histogram in R

点点圈 提交于 2019-12-25 02:52:06

问题


I made this code

RendBio= RtBio
m<-mean(RtBio)
std<-sqrt(var(RtBio))
hist(RendBio, density=20, prob=TRUE,
     xlab="Rendimientos Bio Pappel", ylim=c(0, 20), 
     main="normal curve over histogram", col="brown1")
curve(dnorm(x, mean=m, sd=std), 
      col="darkgoldenrod1", lwd=2, add=TRUE, yaxt="n")

When I run the code there are lines in my histogram but I'd like it to be filled with e solid color. Is there a way to do this?


回答1:


If you want transparency then you need to use an argument to rgb with the same values as col2rgb("brown1") but with an alpha argument:

col2rgb("brown1")
      [,1]
red    255
green   64
blue    64
# Since you want to have transparency of 20% use maxColorValue=1
rgb( 1,.25,.25, .2)
[1] "#FF404033"

Try:

hist(RendBio,  prob=TRUE,
     xlab="Rendimientos Bio Pappel", ylim=c(0, 20), 
     main="normal curve over histogram", col=rgb( 1,.25,.25, .2) )



回答2:


So my comment above explains the problem, but does not provide a solution. Here's a solution.

RendBio <- RtBio <- rnorm(1000,5,2)
m       <- mean(RtBio)
std     <- sqrt(var(RtBio))   # can just use sd(...) for this...
hist(RendBio, prob=TRUE, ylim=c(0,.2),
     xlab="Rendimientos Bio Pappel",  
     main="normal curve over histogram", 
     col=rgb(matrix(col2rgb("brown1"),ncol=3),alpha=50,maxColorValue=255))
curve(dnorm(x, mean=m, sd=std), 
      col="darkgoldenrod1", lwd=2, add=TRUE, yaxt="n")

As the other answer explains, you need to use the rgb(...) function with an alpha=... argument. The hard part is doing that while still using the color names (e.g. "brown1").

EDIT Evidently, this requires more explanation, based on the comment.

The rgb(...) function requires separate arguments for the three color channels, e.g.

rgb(red=..., blue=..., green=..., ...)

whereas col2rgb(...) returns a vector of length 3, containing the r, g, and b values. So this fails:

rgb(col2rgb("brown1"))
# Error in rgb(col2rgb("brown1")) : at least 3 columns needed

The error refers to the fact that rgb(...) will take a matrix with 3 columns as argument, returning a vector of colors with one color for each row of the matrix. So one way to do this is to convert the output of col2rgb(...) to a matrix with 1 row and 3 columns:

rgb(matrix(col2rgb("brown1"),ncol=3),alpha=50,maxColorValue=255)
# [1] "#FF404032"

Very convoluted, but it does work. If someone has a better way, I'd love to see it. You can of course just manually display the r,g,b values from col2rgb(...), and then type these into a call to rgb(...), but this approach lacks generality (try putting it into a function with "brown1" as the argument).

The point of the ggplot solution below is to show that you can avoid all this business of manipulating rgb values, and just use alpha=... directly.

Same thing using ggplot:

library(ggplot2)
gg <- data.frame(x=RendBio)
ggplot(gg,aes(x=x))+
  geom_histogram(aes(y=..density..),color="grey50",fill="brown1",alpha=.2, binwidth=1)+
  stat_function(fun=dnorm,args=list(mean=m,sd=std), color="darkgoldenrod1",size=1)+
  labs(x="Rendimientos Bio Pappel", title="normal curve over histogram")+
  theme_bw()

In ggplot an enclosed polygon (like the bars in the histogram) is colored by specifying fill=..., while color=... specifies the outline color. For linear geometries (like the curve), color=... specifies the color of the curve.



来源:https://stackoverflow.com/questions/23301811/editing-aesthetics-of-my-histogram-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!