如何更改条形图内的颜色?

发布于 2025-01-16 22:27:57 字数 734 浏览 2 评论 0原文

因此,我使用以下代码来获取条形图:

selected_players %>%
  group_by(Player, Season) %>%
  summarise(total= sum(Goals)) %>%
  ggplot(aes(x= Player, y= total, fill= Season)) +
  geom_bar(stat= "identity")+
  scale_color_manual(values= pointcolor)



pointcolor<- c("14-15"= "green", 
+                "15-16"= "wheat4", 
+                "16-17"= "bisque", 
+                "17-18"= "chartreuse", 
+                "18-19"= "violetred1", 
+                "19-20"= "grey15", 
+                "20-21"= "steelblue1")

并且我得到了下图中的图表。我的问题是:如何在此图表上指定每个季节所需的颜色? 感谢您抽出时间。

输入图片此处描述

So, I have used the following code to obtain a bar graph:

selected_players %>%
  group_by(Player, Season) %>%
  summarise(total= sum(Goals)) %>%
  ggplot(aes(x= Player, y= total, fill= Season)) +
  geom_bar(stat= "identity")+
  scale_color_manual(values= pointcolor)



pointcolor<- c("14-15"= "green", 
+                "15-16"= "wheat4", 
+                "16-17"= "bisque", 
+                "17-18"= "chartreuse", 
+                "18-19"= "violetred1", 
+                "19-20"= "grey15", 
+                "20-21"= "steelblue1")

and I'm getting the graph in the image below. My question is: How can I specify the colors I want for each season on this graph?
Thank you for your time.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大姐,你呐 2025-01-23 22:27:57

正如 @stefan 建议的那样,使用 scale_fill_manual () 而不是 scale_color_manual (),因为您尚未将任何变量映射到美学中的颜色。

示例代码:

library(dplyr)
library(ggplot2)
library(ggthemes)


pointcolor<- pointcolor<- c("14-15"= "green", 
                            "15-16"= "wheat4", 
                            "16-17"= "bisque", 
                            "17-18"= "chartreuse", 
                            "18-19"= "violetred1", 
                            "19-20"= "grey15", 
                            "20-21"= "steelblue1")

selected_players %>%
  group_by(Player, Season) %>%
  summarise(total= sum(Goals)) %>%
  ggplot(aes(x= Player, y= total, fill= Season)) +
  geom_bar(stat= "identity")+
  labs(x="", y="Total number of goals", color="Seasons")+
  scale_fill_manual(values=pointcolor)+
  theme_excel()+
   theme(axis.text.x = element_text(angle =45 , hjust = 1,family="Times", face="bold", size=12, color="black"), 
        axis.title.x = element_text( face="bold", size=16, color="black"),
        axis.text.y = element_text( face="bold", size=12, color="black"),
        axis.title.y = element_text( face="bold", size=16, color="black"),
        strip.text = element_text(size=10, face="bold"),
        legend.text = element_text( color = "black", size = 16,face="bold"),
        legend.title = element_text( face="bold", size=16, color="black"),
        legend.position="bottom",
        legend.box = "horizontal",
        plot.title = element_text(hjust = 0.5))

使用 scale_fill_manual() 绘图

在此处输入图像描述

使用 scale_color_manual() 绘图

在此处输入图像描述

示例数据:

selected_players<-structure(list(Player = c("Ronaldo", "Ronaldo", "Ronaldo", "Ronaldo", 
"Ronaldo", "Ronaldo", "Ronaldo", "Tadic", "Tadic", "Tadic", "Tadic", 
"Tadic", "Tadic", "Tadic", "Kane", "Kane", "Kane", "Kane", "Kane", 
"Kane", "Kane", "Messi", "Messi", "Messi", "Messi", "Messi", 
"Messi", "Messi", "Suarez", "Suarez", "Suarez", "Suarez", "Suarez", 
"Suarez", "Suarez", "Salah", "Salah", "Salah", "Salah", "Salah", 
"Salah", "Salah", "Aubameyang", "Aubameyang", "Aubameyang", "Aubameyang", 
"Aubameyang", "Aubameyang", "Aubameyang", "Lewandoski", "Lewandoski", 
"Lewandoski", "Lewandoski", "Lewandoski", "Lewandoski", "Lewandoski", 
"Aguero", "Aguero", "Aguero", "Aguero", "Aguero", "Aguero", "Aguero"
), Season = c("14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21"), Goals = c(50, 39, 16, 47, 42, 18, 14, 6, 48, 10, 34, 
14, 29, 46, 13, 14, 22, 56, 49, 39, 51, 23, 48, 45, 48, 50, 35, 
54, 21, 24, 31, 12, 51, 39, 29, 29, 29, 15, 25, 58, 41, 35, 31, 
1, 53, 5, 4, 16, 4, 57, 38, 33, 49, 25, 49, 36, 4, 1, 53, 14, 
26, 2, 37)), class = "data.frame", row.names = c(NA, -63L))

As suggested by @stefan use scale_fill_manual () instead of scale_color_manual ()because you have not mapped any variable to colour in the aesthetic.

Sample code:

library(dplyr)
library(ggplot2)
library(ggthemes)


pointcolor<- pointcolor<- c("14-15"= "green", 
                            "15-16"= "wheat4", 
                            "16-17"= "bisque", 
                            "17-18"= "chartreuse", 
                            "18-19"= "violetred1", 
                            "19-20"= "grey15", 
                            "20-21"= "steelblue1")

selected_players %>%
  group_by(Player, Season) %>%
  summarise(total= sum(Goals)) %>%
  ggplot(aes(x= Player, y= total, fill= Season)) +
  geom_bar(stat= "identity")+
  labs(x="", y="Total number of goals", color="Seasons")+
  scale_fill_manual(values=pointcolor)+
  theme_excel()+
   theme(axis.text.x = element_text(angle =45 , hjust = 1,family="Times", face="bold", size=12, color="black"), 
        axis.title.x = element_text( face="bold", size=16, color="black"),
        axis.text.y = element_text( face="bold", size=12, color="black"),
        axis.title.y = element_text( face="bold", size=16, color="black"),
        strip.text = element_text(size=10, face="bold"),
        legend.text = element_text( color = "black", size = 16,face="bold"),
        legend.title = element_text( face="bold", size=16, color="black"),
        legend.position="bottom",
        legend.box = "horizontal",
        plot.title = element_text(hjust = 0.5))

Plot with scale_fill_manual()

enter image description here

Plot with scale_color_manual()

enter image description here

Sample data:

selected_players<-structure(list(Player = c("Ronaldo", "Ronaldo", "Ronaldo", "Ronaldo", 
"Ronaldo", "Ronaldo", "Ronaldo", "Tadic", "Tadic", "Tadic", "Tadic", 
"Tadic", "Tadic", "Tadic", "Kane", "Kane", "Kane", "Kane", "Kane", 
"Kane", "Kane", "Messi", "Messi", "Messi", "Messi", "Messi", 
"Messi", "Messi", "Suarez", "Suarez", "Suarez", "Suarez", "Suarez", 
"Suarez", "Suarez", "Salah", "Salah", "Salah", "Salah", "Salah", 
"Salah", "Salah", "Aubameyang", "Aubameyang", "Aubameyang", "Aubameyang", 
"Aubameyang", "Aubameyang", "Aubameyang", "Lewandoski", "Lewandoski", 
"Lewandoski", "Lewandoski", "Lewandoski", "Lewandoski", "Lewandoski", 
"Aguero", "Aguero", "Aguero", "Aguero", "Aguero", "Aguero", "Aguero"
), Season = c("14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", 
"20-21"), Goals = c(50, 39, 16, 47, 42, 18, 14, 6, 48, 10, 34, 
14, 29, 46, 13, 14, 22, 56, 49, 39, 51, 23, 48, 45, 48, 50, 35, 
54, 21, 24, 31, 12, 51, 39, 29, 29, 29, 15, 25, 58, 41, 35, 31, 
1, 53, 5, 4, 16, 4, 57, 38, 33, 49, 25, 49, 36, 4, 1, 53, 14, 
26, 2, 37)), class = "data.frame", row.names = c(NA, -63L))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文