簡體   English   中英

使用 scale_fill_manual 在 R 中出現顏色變化問題

[英]Problem with color changing in R using scale_fill_manual

我有一個名為 temperature4countries 的數據集

西班牙 塞浦路斯 冰島 奧地利
1998 15 17 7 8
1999 20 21 7.5 8.5
2000 16 18 7 8
2001年 17 20 8 8
2002年 17.5 19 8 8
2003年 20 21 7.5 8.5
2004年 20 22 8 9
2005年 20 21 8.5 9.5
2006年 21 27 9 10
2007年 22 23 9.5 10.5
2008年 25 24 9 11
temperature <- data.frame(
  stringsAsFactors = FALSE,
  Year= c(1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008),
  Spain = c(15,20,16,17,17.5,20,20,20,21,22,25),
  Cyprus = c(17,21,18,20,19,21,22,21,27,23,24),
  Iceland = c(7,7.5,7,8,8,7.5,8,8.5,9,9.5,9),
  Austria = c(8,8.5,8,8,8,8.5,9,9.5,10,10.5,11),
  check.names = FALSE
)
dput(temperature)

我的目標是創建一個折線圖

我用了那個代碼

df <- pivot_longer(temperature, 
                        cols = c(Spain, Cyprus, Iceland, Austria), 
                        values_to = "Temperature",
                        names_to = "Countries")
ggplot(df,
       aes(
         x = Year,
         y = Temperature,
         color = Countries
       )) +
  geom_line() + 
  ggtitle("Trend of Temperature in 4 European countries") +
  xlab("Year") +
  ylab("Temperature[C]") 

同樣,colors 真的很丑。 我想改變它。 我用了那個代碼

df <- pivot_longer(temperature, 
                        cols = c(Spain, Cyprus, Iceland, Austria), 
                        values_to = "Temperature",
                        names_to = "Countries")
ggplot(df,
       aes(
         x = Year,
         y = Temperature,
         color = Countries
       )) +
  geom_line() + 
  ggtitle("Trend of Temperature in 4 European countries") +
  xlab("Year") +
  ylab("Temperature[C]")  + scale_fill_manual(values=c("aqua marine", "teal","lime","yellow"))

但是 colors 沒變???

因為我認為這是如何改變 colors

在這種情況下,您應該使用scale_color_manual而不是scale_fill_manual 在你的values參數中明確,傳遞一個命名向量:

+ scale_color_manual(values=c("Austria" = "red", "Cyprus" = "blue",
                             "Iceland" = "green","Spain" = "orange"))

檢查名為 colors 的可用colors() 如果您願意,可以使用"#008080"代替"teal""limegreen"代替"lime"

顏色不改變的原因是你不能使用scale_fill_manual 將其更改為scale_color_manual ,您將看到更改。

ggplot(df,
       aes(
         x = Year,
         y = Temperature,
         color = Countries
       )) +
  geom_line() + 
  ggtitle("Trend of Temperature in 4 European countries") +
  xlab("Year") +
  ylab("Temperature[C]") + scale_color_manual(values=c("aqua marine", "red","blue","yellow"))

注意:我不得不更改一些 colors 因為它找不到“石灰”和“藍綠色”。

PS。 它們之間的區別在於:

  • scale_fill_manual()用於需要填充的繪圖,例如箱線圖、條形圖、小提琴圖、點圖等。

  • scale_color_manual()用於線和點。

這里有更多信息

你進展順利:你犯了三個錯誤:

  1. 您在最后一行代碼中連續添加了兩次+
  2. 您正在使用scale_fill_manual(...) ,但 plot 中使用的美學是color 因此,正確的 function 將是scale_colour_manual(...)
  3. 您指定的顏色不是 R 顏色。 您可以使用colors()獲得 R 中所有命名顏色的概覽。 使用例如"aquamarine" %in% colors()檢查單個顏色

因此,正確的代碼是:

ggplot(df,
       aes(
         x = Year,
         y = Temperature,
         color = Countries
       )) +
  geom_line() + 
  ggtitle("Trend of Temperature in 4 European countries") +
  xlab("Year") +
  ylab("Temperature[C]") + scale_colour_manual(values=c("aquamarine", "cyan4","limegreen","yellow"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM