0

我想知道如何将第二张图片的颜色更改为第一张我是 ggplot2 的新手并且仍在学习 atm

library(tidyverse)
data(mpg)
ggplot(data = diamonds) +
          geom_bar(
            mapping = aes(x = cut, fill = clarity),
            position = "dodge"
          )

我以为我会在第一张照片中获得很多颜色,但我在第二张照片中获得了颜色。第一张图 第二张图

4

1 回答 1

0

You can change the colour using various methods. Here are a couple of examples:

  1. You can specify manual colours using codes from a page like this and using scale_fill_manual
    ggplot(diamonds, aes(cut))+
      geom_bar(position = "dodge", aes(fill = clarity))+
      scale_fill_manual(values = c("#0CFCA0", "#A54408", "#E53975", "#7FB0FF", 
                                   "#4ACC0A", "#51397F", "#FFC059", "#062C7F"))

Which creates this: Manual colours - excuse the choices, they were at random

This page has the codes from the ggplot2 default colours you showed in yout first picture.

  1. As maarvd mentioned above you could use RColorBrewer
    ggplot(diamonds, aes(cut))+
      geom_bar(position = "dodge", aes(fill = clarity))+
      scale_fill_brewer(palette = "RdYlBu")

Which creates this: Brewer colours

于 2021-06-18T16:06:49.287 回答