【问题标题】:cannot change color for barplot when combine plots组合图时无法更改条形图的颜色
【发布时间】:2017-03-18 13:53:03
【问题描述】:

我尝试使用条形图进行绘图,当hist(breaks=200) 时,条形图可以使用不同的颜色。但是当hist(breaks=1000) 无法在条形图中更改颜色时。以及为什么ylab合并三个情节时没有显示在条形图中?在单个情节中可以)。

breaks=200 breaks=1000

这是我的代码:breaks=1000,不能改变颜色。

def.par <- par(no.readonly = TRUE) # save default, for resetting... 
x <- pmin(3, pmax(-3, rnorm(5000)))
y <- pmin(3, pmax(-3, rnorm(5000)))
xhist <- hist(x, breaks=1000, plot=FALSE)
yhist <- hist(y, breaks=1000,plot=FALSE)
top <- max(c(xhist$density, yhist$density))
xrange <- c(-3,3)
yrange <- c(-3,3)
nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE)#layout.show(nf)ZSSS

par(mar=c(3,3,0,0))
plot(x, y, xlim=xrange, ylim=yrange, xlab="T1", ylab="T2")

par(mar=c(0,3,1,1))
barplot(xhist$density, axes=TRUE, ylim=c(0, top),ylab="T3",col="red")

par(mar=c(3,0,1,1))
barplot(yhist$density, axes=TRUE, xlim=c(0, top),xlab="T4",space=0, horiz=TRUE,col="green")

par(def.par)

【问题讨论】:

    标签: r plot histogram bar-chart


    【解决方案1】:

    只需改变边框的颜色:

    def.par <- par(no.readonly = TRUE) # save default, for resetting... 
    x <- pmin(3, pmax(-3, rnorm(5000)))
    y <- pmin(3, pmax(-3, rnorm(5000)))
    xhist <- hist(x, breaks=100, plot=FALSE)
    yhist <- hist(y, breaks=100,plot=FALSE)
    top <- max(c(xhist$density, yhist$density))
    xrange <- c(-3,3)
    yrange <- c(-3,3)
    nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3),TRUE)#layout.show(nf)ZSSS
    
    par(mar=c(3,3,0,0))
    plot(x, y, xlim=xrange, ylim=yrange, xlab="T1", ylab="T2")
    
    par(mar=c(0,3,1,1))
    barplot(xhist$density, axes=TRUE, ylim=c(0, top),ylab="T3",col="red", border = "red")
    
    par(mar=c(3,0,1,1))
    barplot(yhist$density, axes=TRUE, xlim=c(0, top),xlab="T4",space=0, horiz=TRUE,col="green", border = "green")
    

    【讨论】:

    • 谢谢!但是你知道为什么ylab(T2,T3,T4)没有出现在三个图中吗?
    猜你喜欢
    • 2018-04-02
    • 2016-07-26
    • 1970-01-01
    • 2011-08-03
    • 2012-01-19
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多