在R中创建调色板的调色板,用于具有19个分类变量的饼图
抬头:这篇文章有点冗长。道歉。
我正在对我的书阅读习惯进行分析。我从一个简单的问题开始:“我最阅读什么类型”。我使用聚合功能回答了这个问题。现在,我正在尝试制作饼图。我已经成功创建了一个饼图。以下数据创建了帧。创建了“标签”列,以便饼图中的标签具有类型和百分比。
类型 | number_read | label |
---|---|---|
仙女 | 93 | 童话,21.5%的 |
通用小说 | 7 | 通用小说,1.6%的 |
神秘 | 29 | 神秘,6.7% |
浪漫 | 的浪漫史117 | 史,27.1% |
短篇小说 | 2 | 短篇小说2短篇小说2短篇小说,0.5% |
wired | 4 | wired wird wird,0.9 |
%,经典 | 28 | Classics,6.5%的FanFiction Fanification,6.5%的 |
FanFiction Fanifiction | 25 | 幻想小说,5.8%的 |
历史小说 | 12 | 历史小说,2.8% |
非小说 | 20 | 非小说,4.6% |
讽刺 | 1 | 讽刺,0.2 |
%惊悚片 | 6 | 惊悚片,1.4% |
的年轻人 | 2 | 年轻人2年轻人,0.5% |
环境小说 | 1 | 环境小说,0.2% |
36 FANTASY | 36 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:
颜色位于错误的位置,并且仍然有没有自己切片的变量。有什么建议吗?我在做什么错?我不需要那些特定的颜色 - 我只需要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.
genre | number_read | label |
---|---|---|
Fairytales | 93 | Fairytales, 21.5% |
General Fiction | 7 | General Fiction, 1.6% |
Mystery | 29 | Mystery, 6.7% |
Romance | 117 | Romance, 27.1% |
Short Story | 2 | Short Story, 0.5% |
Weird | 4 | Weird, 0.9% |
Classics | 28 | Classics, 6.5% |
Fanfiction | 25 | Fanfiction, 5.8% |
Historical Fiction | 12 | Historical Fiction, 2.8% |
Nonfiction | 20 | Nonfiction, 4.6% |
Satire | 1 | Satire, 0.2% |
Thriller | 6 | Thriller, 1.4% |
Young Adult | 2 | Young Adult, 0.5% |
Environmental Fiction | 1 | Environmental Fiction, 0.2% |
Fantasy | 36 | Fantasy, 8.3% |
Horror | 28 | Horror, 6.5% |
Paranormal | 3 | Paranormal, 0.7% |
Science Fiction | 16 | Science Fiction, 3.7% |
Unknown | 2 | Unknown, 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:
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我面对相同的20多种颜色饼图,并尝试了您的方法。实际上,您可以将您的程序插入
I am facing the same more than 20 colors pie chart and tried your method. Actually, you can twig your program to