【问题标题】:color bar plot depending on the x value with R颜色条图取决于 x 值与 R
【发布时间】:2021-06-25 15:41:29
【问题描述】:

我用 R 实现了条形图,我想根据 x 轴的值用不同的颜色为条形着色。例如,我将为 x = 1 的条分配橙色,为 x = 2 的条分配黄色,为条 3=

【问题讨论】:

    标签: r


    【解决方案1】:

    您可以通过barplot() 函数的color 参数为条形分配颜色。该参数采用单一颜色(如果您希望所有条看起来都相同)或颜色向量。

    在您的情况下,给定存储在矩阵m 中的数据,您可以这样写:

    barplot(m,color=c("orange","yellow",rep("lightgreen",3),rep("darkgreen",23)))
    

    【讨论】:

    • 如果我使用 ggplot 函数?我可以这样做吗?
    • 是的,你可以。通过将参数 +scale_fill_manual(values = c("orange","yellow",rep("lightgreen",3),rep("darkgreen",23))) 添加到您的绘图中
    猜你喜欢
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 2021-06-23
    相关资源
    最近更新 更多