【问题标题】:Filling up histograms with ggplot - Changing colours用 ggplot 填充直方图 - 改变颜色
【发布时间】:2022-01-17 05:08:02
【问题描述】:

我正在学习如何使用 R 和 ggplot 库,并遇到了更改以下直方图颜色的挑战!我确实设法从图表中更改了全色,但它是纯色的!我想用不同的颜色计数,但不知何故我找不到正确的方法!你能帮我把它换成另一种颜色吗?

df4 <- data.frame(rnorm(10000,100,10))
colnames(df4) <- c("Value")

histi_base2 <- ggplot(df4, aes(x=Value))

histi5 <- histi_base2 + geom_histogram(binwidth = 1, colour="blue", alpha=0.8, aes(fill=..count..)) + labs(title="My first Histogram", subtitle = "in blue")
histi5

Blue Histogram

【问题讨论】:

    标签: r ggplot2 graph histogram fill


    【解决方案1】:

    你可以使用scale_fill_gradient:

    df4 <- data.frame(rnorm(10000,100,10))
    colnames(df4) <- c("Value")
    
    library(ggplot2)
    
    ggplot(df4, aes(x=Value)) + 
      geom_histogram(binwidth = 1, alpha=0.8, aes(fill=..count..)) + 
      scale_fill_gradient(low = "red", high = "green") +
      labs(title="My first Histogram")
    

    【讨论】:

    • 天啊!谢谢=)
    【解决方案2】:

    试试:

    df4 <- data.frame(rnorm(10000,100,10))
    colnames(df4) <- c("Value")
    
    histi_base2 <- ggplot(df4, aes(x=Value))
    histi5 <- histi_base2 + 
      geom_histogram(binwidth = 1, alpha=0.8, fill="red", color="black") + 
      labs(title="Your second Histogram", subtitle = "now in solid red")
    histi5
    

    使用fill 可以更改条内的颜色,使用color 可以更改边框。

    PS:对不起,我认为我没有正确理解它。 Martins 解决方案似乎更适合。

    【讨论】:

    • 嗨,Marco,感谢您抽出宝贵时间来回答。是的,到目前为止我明白了,但我指的是 Martin Cal 所做的!谢谢你
    猜你喜欢
    • 1970-01-01
    • 2011-02-02
    • 2019-08-22
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 2016-12-11
    相关资源
    最近更新 更多