在R中创建调色板的调色板,用于具有19个分类变量的饼图

发布于 2025-02-02 19:21:11 字数 5812 浏览 1 评论 0原文

抬头:这篇文章有点冗长。道歉。

我正在对我的书阅读习惯进行分析。我从一个简单的问题开始:“我最阅读什么类型”。我使用聚合功能回答了这个问题。现在,我正在尝试制作饼图。我已经成功创建了一个饼图。以下数据创建了帧。创建了“标签”列,以便饼图中的标签具有类型和百分比。

类型number_readlabel
仙女93童话,21.5%的
通用小说7通用小说,1.6%的
神秘29神秘,6.7%
浪漫的浪漫史117史,27.1%
短篇小说2短篇小说2短篇小说2短篇小说,0.5%
wired4wired wird wird,0.9
%,经典28Classics,6.5%的FanFiction Fanification,6.5%的
FanFiction Fanifiction25幻想小说,5.8%的
历史小说12历史小说,2.8%
非小说20非小说,4.6%
讽刺1讽刺,0.2
%惊悚片6惊悚片,1.4%
的年轻人2年轻人2年轻人,0.5%
环境小说1环境小说,0.2%
36 FANTASY36 FANTASY 36幻想36 FANTASY FANTASY 36 FANTASY 36 FANTASY 36 FANTASY ,8.3%的
恐怖28恐怖,6.5%
超自然现象3超自然现象,0.7%
科幻小说16科幻小说,3.7%
未知2未知,0.5%的

问题是我对默认配色方案的变量太多。我需要创建自己的。我知道关于堆栈溢出的调色板有无数问题。我已经尝试了它们,我无法让它们上班。有人可以告诉我我做错了什么吗?

默认配色方案是图表的样子:

​0等)?

这是我用来获取的代码:

require(ggplot2)
require(ggrepel)# allows pie chart labels to be outside chart
require(tidyverse)
require(dplyr)
library(ggmap)
library(RColorBrewer)

#positions of the labels:
genre.label.pos <- genre.count %>% 
mutate(csum = rev(cumsum(rev(number_read))), 
     pos = number_read/2 + lead(csum, 1),
     pos = if_else(is.na(pos), number_read/2, pos))

ggplot(data=genre.count, aes(x=1, y=number_read, fill=genre))+
geom_col(width=1, color=1)+
coord_polar(theta="y")+
geom_label_repel(data = genre.label.pos,
               aes(y = pos, label = paste0(genre.count$label)),
               size = 4.5, nudge_x = 1, show.legend = FALSE) +
guides(fill = guide_legend(title = "Genre"))+
theme(axis.text.y = element_blank(), #gets rid of values on y-axis
    axis.ticks.y= element_blank(), #gets rid of tick marks on y-axis
    axis.title = element_blank(),  #gets rid of title
    legend.position = "none", # Removes the legend
    panel.background = element_rect(fill = "white"))

我尝试创建自己的调色板。所使用的十六进制值来自彩色酿酒师,

genre_palette <- (c("#fb9a99","#1b9e77","#e6ab02",
                "#e7298a","#d95f02","#66a61e",
                "#1b9e77","#e41a1c","#377eb8",
                "#bd0026","#dd1c77","#66c2a5","#d4b9da",
                "#006d2c","#238443","#525252",
                "#4d004b","#225ea8","#8c510a"))

我尝试了各种方法来使此调色板显示在我的图表中。

方法A:

#original_ggplot_code_above()+
scale_fill_manual(values = "genre_palette")

方法B:

#original_ggplot_code_above()+
scale_fill_manual(values = "#fb9a99","#1b9e77","#e6ab02",
                "#e7298a","#d95f02","#66a61e",
                "#1b9e77","#e41a1c","#377eb8",
                "#bd0026","#dd1c77","#66c2a5","#d4b9da",
                "#006d2c","#238443","#525252",
                "#4d004b","#225ea8","#8c510a")

方法C:STACE_FILL_BREWER的SWITS scale_manual,然后尝试两种方法A和B方法

#method A
#original_ggplot_code_above()+
scale_fill_brewer(values =genre_palette)

#method B
#original_ggplot_code_above()+
scale_fill_brewer(values = "#fb9a99","#1b9e77","#e6ab02",
                "#e7298a","#d95f02","#66a61e",
                "#1b9e77","#e41a1c","#377eb8",
                "#bd0026","#dd1c77","#66c2a5","#d4b9da",
                "#006d2c","#238443","#525252",
                "#4d004b","#225ea8","#8c510a")

A

#method A:
genre_palette <- c("red","coral","darkred","chocolate1","cornsilk4","cyan1",
               "darkgoldenrod2","darkorchid2","deeppink2","darkslategray4","chartreuse4",
               "brown1","blueviolet","aquamarine","blue3","azure4","darkorange3",
               "brown3","burlywood4")
#original_ggplot_code_above()+
scale_fill_manual(values = genre_palette)

#method B
#original_ggplot_code_above()+
scale_fill_manual(values = "red","coral","darkred","chocolate1","cornsilk4","cyan1",
               "darkgoldenrod2","darkorchid2","deeppink2","darkslategray4","chartreuse4",
               "brown1","blueviolet","aquamarine","blue3","azure4","darkorange3",
               "brown3","burlywood4")

Insufficient values in manual scale. 19 needed but only 1 provided.

B :

Error in discrete_scale(aesthetic, "manual", pal, breaks = breaks, limits = limits,  : 
unused arguments ("#bd0026", "#dd1c77", "#66c2a5",
"#d4b9da","#006d2c","#238443","#525252","#4d004b","#225ea8", "#8c510a")

方法C的错误消息C:

Error in discrete_scale(aesthetics, "brewer", brewer_pal(type, palette,  :
unused arguments (values = "#fb9a99", "#d4b9da", "#006d2c", "#238443", "#525252", "#4d004b",
"#225ea8", "#8c510a")

方法D的错误消息D:

-Method B: same as method B

-Method A: see below:

当我重新运行GGPLOT代码时,这是显示的颜色标度:

颜色位于错误的位置,并且仍然有没有自己切片的变量。有什么建议吗?我在做什么错?我不需要那些特定的颜色 - 我只需要19种颜色 - 任何颜色。

Heads up: This post is a bit lengthy. Apologies.

I'm running an analysis of my book reading habits. I'm starting with the simple question "what genre do I read the most". I answered this question using the aggregate function. Now I'm trying to make a pie chart. I've successfully created a pie chart. The below data.frame was created. The "label" column was created so that the labels in the pie chart had both the genre and the percentage.

genrenumber_readlabel
Fairytales93Fairytales, 21.5%
General Fiction7General Fiction, 1.6%
Mystery29Mystery, 6.7%
Romance117Romance, 27.1%
Short Story2Short Story, 0.5%
Weird4Weird, 0.9%
Classics28Classics, 6.5%
Fanfiction25Fanfiction, 5.8%
Historical Fiction12Historical Fiction, 2.8%
Nonfiction20Nonfiction, 4.6%
Satire1Satire, 0.2%
Thriller6Thriller, 1.4%
Young Adult2Young Adult, 0.5%
Environmental Fiction1Environmental Fiction, 0.2%
Fantasy36Fantasy, 8.3%
Horror28Horror, 6.5%
Paranormal3Paranormal, 0.7%
Science Fiction16Science Fiction, 3.7%
Unknown2Unknown, 0.5%

The problem is that I have too many variables for the default color scheme. I need to create my own. I know there are countless questions regarding color palettes on Stack Overflow. I've tried them, and I can't get them to work. Can someone tell me what I'm doing wrong?

Here's what the chart looks like with the default color scheme:

pie chart with too many variables

Side question Does anyone know how to get rid of the tiny greyed out numbers around(300, 400, 0, etc)?

Here's the code I used to get it:

require(ggplot2)
require(ggrepel)# allows pie chart labels to be outside chart
require(tidyverse)
require(dplyr)
library(ggmap)
library(RColorBrewer)

#positions of the labels:
genre.label.pos <- genre.count %>% 
mutate(csum = rev(cumsum(rev(number_read))), 
     pos = number_read/2 + lead(csum, 1),
     pos = if_else(is.na(pos), number_read/2, pos))

ggplot(data=genre.count, aes(x=1, y=number_read, fill=genre))+
geom_col(width=1, color=1)+
coord_polar(theta="y")+
geom_label_repel(data = genre.label.pos,
               aes(y = pos, label = paste0(genre.count$label)),
               size = 4.5, nudge_x = 1, show.legend = FALSE) +
guides(fill = guide_legend(title = "Genre"))+
theme(axis.text.y = element_blank(), #gets rid of values on y-axis
    axis.ticks.y= element_blank(), #gets rid of tick marks on y-axis
    axis.title = element_blank(),  #gets rid of title
    legend.position = "none", # Removes the legend
    panel.background = element_rect(fill = "white"))

I tried creating my own color palette. Hex values used are from color brewer

genre_palette <- (c("#fb9a99","#1b9e77","#e6ab02",
                "#e7298a","#d95f02","#66a61e",
                "#1b9e77","#e41a1c","#377eb8",
                "#bd0026","#dd1c77","#66c2a5","#d4b9da",
                "#006d2c","#238443","#525252",
                "#4d004b","#225ea8","#8c510a"))

I tried various ways of getting this palette to display in my chart.

Method A:

#original_ggplot_code_above()+
scale_fill_manual(values = "genre_palette")

Method B:

#original_ggplot_code_above()+
scale_fill_manual(values = "#fb9a99","#1b9e77","#e6ab02",
                "#e7298a","#d95f02","#66a61e",
                "#1b9e77","#e41a1c","#377eb8",
                "#bd0026","#dd1c77","#66c2a5","#d4b9da",
                "#006d2c","#238443","#525252",
                "#4d004b","#225ea8","#8c510a")

Method C: Switch scale_manual for scale_fill_brewer and try both methods A and B again

#method A
#original_ggplot_code_above()+
scale_fill_brewer(values =genre_palette)

#method B
#original_ggplot_code_above()+
scale_fill_brewer(values = "#fb9a99","#1b9e77","#e6ab02",
                "#e7298a","#d95f02","#66a61e",
                "#1b9e77","#e41a1c","#377eb8",
                "#bd0026","#dd1c77","#66c2a5","#d4b9da",
                "#006d2c","#238443","#525252",
                "#4d004b","#225ea8","#8c510a")

Method D: Switch hex color codes for base r colors and try methods A/B again

#method A:
genre_palette <- c("red","coral","darkred","chocolate1","cornsilk4","cyan1",
               "darkgoldenrod2","darkorchid2","deeppink2","darkslategray4","chartreuse4",
               "brown1","blueviolet","aquamarine","blue3","azure4","darkorange3",
               "brown3","burlywood4")
#original_ggplot_code_above()+
scale_fill_manual(values = genre_palette)

#method B
#original_ggplot_code_above()+
scale_fill_manual(values = "red","coral","darkred","chocolate1","cornsilk4","cyan1",
               "darkgoldenrod2","darkorchid2","deeppink2","darkslategray4","chartreuse4",
               "brown1","blueviolet","aquamarine","blue3","azure4","darkorange3",
               "brown3","burlywood4")

Error message for method A:

Insufficient values in manual scale. 19 needed but only 1 provided.

Error message for method B:

Error in discrete_scale(aesthetic, "manual", pal, breaks = breaks, limits = limits,  : 
unused arguments ("#bd0026", "#dd1c77", "#66c2a5",
"#d4b9da","#006d2c","#238443","#525252","#4d004b","#225ea8", "#8c510a")

Error message for method C:

Error in discrete_scale(aesthetics, "brewer", brewer_pal(type, palette,  :
unused arguments (values = "#fb9a99", "#d4b9da", "#006d2c", "#238443", "#525252", "#4d004b",
"#225ea8", "#8c510a")

Error message for method D:

-Method B: same as method B

-Method A: see below:

When I rerun the ggplot code, here's the color scale that shows up:
enter image description here

Colors are in the wrong place and there are still variables without their own slices. Any suggestions? What am I doing wrong? I don't need those specific colors- I just need 19 colors- any colors.

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

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

发布评论

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

评论(1

浅忆流年 2025-02-09 19:21:12

我面对相同的20多种颜色饼图,并尝试了您的方法。实际上,您可以将您的程序插入

scale_fill_manual(values = c("red", "coral", "darkred", 
                             "chocolate1", "cornsilk4", "cyan1",
                             "darkgoldenrod2", "darkorchid2", "deeppink2",
                             "darkslategray4", "chartreuse4", "brown1",
                             "blueviolet", "aquamarine", "blue3", 
                             "azure4", "darkorange3", "brown3", 
                             "burlywood4"))

I am facing the same more than 20 colors pie chart and tried your method. Actually, you can twig your program to

scale_fill_manual(values = c("red", "coral", "darkred", 
                             "chocolate1", "cornsilk4", "cyan1",
                             "darkgoldenrod2", "darkorchid2", "deeppink2",
                             "darkslategray4", "chartreuse4", "brown1",
                             "blueviolet", "aquamarine", "blue3", 
                             "azure4", "darkorange3", "brown3", 
                             "burlywood4"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文