Uzg
Uzg

Reputation: 319

Alternative lattice plot using ggplot2 for density

I wanted an alternative plot using ggplot2 for the one generated by the code below:

library(lattice)
x<-rnorm(1000)

plot.new()
densityplot(x, main="Density",ylab="",
   panel=function(x, ...){
     panel.densityplot(x,col="black", ...)
     panel.abline(v=.5,col="gray")
   }
)

enter image description here

I need sample at the bottom of the plot (like bars at the right of these plots enter link description here). I've just found some plots similar to:

library(ggplot)

x1=seq(x)
data1<-data.frame(x1,x)
m <- ggplot(data1, aes(x = x))
m + geom_density(alpha=.3, fill="black")

enter image description here

Upvotes: 0

Views: 812

Answers (1)

CMichael
CMichael

Reputation: 1866

x<-rnorm(1000)
x<-as.data.frame(x)

require(ggplot2)

g <- ggplot(x,aes(x=x))
g <- g + geom_density()
g <- g + geom_vline(xintercept = .5)
g <- g + geom_point(aes(x=x,y=0),shape=1,size=5)
g <- g + theme_bw()
g

UPDATED to include comment of OP - note how the geom_point with y=1 will create the sample on the axis.

This will look like this:

enter image description here

Upvotes: 1

Related Questions