I wanted an alternative plot using ggplot2 for the one generated by the code below:
我希望使用ggplot2替代以下代码生成的图:
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")
}
)
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")
1 个解决方案
#1
1
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.
更新以包括OP的注释 - 注意y = 1的geom_point将如何在轴上创建样本。
This will look like this:
这将是这样的:
#1
1
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.
更新以包括OP的注释 - 注意y = 1的geom_point将如何在轴上创建样本。
This will look like this:
这将是这样的: