首页 文章

绘制矢量作为barplot

提问于
浏览
4

我有一个矢量:

x<-c(1,1,1,1,2,3,5,1,1,1,2,4,9)
y<-length(x)

我想绘制这样的图,使得每个值分别绘制而不是绘制计数 .

因此,基本上每个值应在图中单独表示,其中x轴的长度等于 y ,并且每个值都绘制在y轴上 .

如何使用qplot完成?

对于矩阵:

a<-matrix(NA, ncol=3, nrow=100)

a[,1]<-1:100
a[,2]<-rnorm(100)
a[,3]<-rnorm(100)

a<-melt(as.data.frame(a),id.vars="V1")

ggplot(a,aes(seq_along(a),a))geom_bar(stat =“identity”)facet_wrap(V1)

3 回答

  • 0

    使用 ggplot2 将x用作y值,并沿x轴的x值生成序列 .

    ggplot(data.frame(x),aes(seq_along(x),x))+geom_bar(stat="identity")
    

    enter image description here

    如果你有矩阵 a ,你需要为每一行制作图,然后将其熔化,然后使用 variable 作为x轴, value 作为y轴

    a<-melt(as.data.frame(a),id.vars=1)
    
    ggplot(a,aes(variable,value))+geom_bar(stat="identity")+facet_wrap(~V1)
    

    enter image description here

  • 10

    简单解决方案

    barplot(x,xlim = c(0,15), ylim = c(0,10))
    

    xlim和ylim按比例缩放,具体取决于矢量长度

  • 0

    您也可以尝试这一点,而无需明确创建 dataframe

    ggplot() + geom_bar(aes(x=seq_along(x),y=x), stat='identity') + xlab('x') + ylab('y')
    

    enter image description here

相关问题