【问题标题】:Color Barplot by Count按计数的颜色条形图
【发布时间】:2011-10-15 04:14:01
【问题描述】:

我正在 R 中创建一个条形图,并希望按条形高度(计数)为每个条形着色

目前我所拥有的:

z=rnorm(n,1)
Z=runif(n)
h=barplot(Z)

我有图片,但没有足够的声誉来发布它们。 这是 MatLab 中的示例: MatLab-Color bars by height

【问题讨论】:

  • 错误:找不到函数“rand”
  • sorry rand 是 matlab 代码。我想我用的是 rnorm

标签: r graphics plot


【解决方案1】:

除了 baptiste 的 ggplot2 解决方案,这里有一个使用barplot的简单示例:

Z <- sample(20,15,replace = TRUE)
barplot(Z,col = heat.colors(max(Z))[Z])

产生这样的东西:

【讨论】:

  • 这很棒。非常感谢!
【解决方案2】:

试试这个,

library(ggplot2)
d = data.frame(x = rnorm(100))
ggplot(d) + geom_bar(aes(x, fill = ..count..))

【讨论】:

    猜你喜欢
    • 2021-09-16
    • 2014-10-30
    • 2019-06-11
    • 1970-01-01
    • 2020-12-07
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多