# generate 100 random normal data values
y <- rnorm(100)
# normal curve and general density curves superimposed over histogram
# all defaults
dens(y)
# suppress the histogram, leaving only the density curves
# specify x-axis label per the xlab option for the plot function
dens(y, col.bars="transparent", xlab="My Var")
# specify (non-transparent) colors for the curves,
# to make transparent, need alpha option for the rgb function
dens(y, col.nrm="darkgreen", col.gen="plum")
# display only the general estimated density
# so do not display the estimated normal curve
# specify the bandwidth for the general density curve,
# use the standard bw option for the density function
dens(y, type="general", bw=.6)
# display only the general estimated density and a corresponding
# interval of unit width around x.pt
dens(y, type="general", x.pt=2)
# create data frame, mydata, to mimic reading data with rad function
# although data not attached, access the variable directly by its name
mydata <- data.frame(rnorm(100))
names(mydata) <- "X"
dens(X)
# variable of interest is in a data frame which is not the default mydata
# access the breaks variable in the R provided warpbreaks data set
# although data not attached, access the variable directly by its name
data(warpbreaks)
dens(breaks, dframe=warpbreaks)
Run the code above in your browser using DataLab