注意:这篇文章有点长,抱歉。
我正在分析我的阅读习惯。我从一个简单的问题“我读得最多的是什么类型的书”开始。我用聚合函数回答了这个问题。现在我正在尝试制作一个饼图。我已经成功地制作了一个饼图。下面的数据框架被创建了。“标签”列被创建了,这样饼图中的标签既有类型又有百分比。
| 体裁|编号_读取|标号|
| - ------|- ------|- ------|
| 童话|九十三|童话,21.5%|
| 普通小说|七|普通小说,1.6%|
| 神秘|二十九|神秘感,6.7%|
| 浪漫|一百一十七|爱情,27.1%|
| 短篇小说|第二章|短篇小说,0.5%|
| 奇怪|四个|奇怪,0.9%|
| 经典|二十八|经典,6.5%|
| 同人小说|二十五|同人小说,5.8%|
| 历史小说|十二|历史小说,2.8%|
| 非虚构作品|二十个|非小说类,4.6%|
| 讽刺|1个|讽刺,0.2%|
| 颤栗|六个|惊悚片,1.4%|
| 年轻成人|第二章|年轻成人,0.5%|
| 环境题材小说|1个|环保小说,0.2%|
| 幻想|三十六|幻想,8.3%|
| 恐怖|二十八|恐怖,6.5%|
| 超自然现象|三个|超自然,0.7%|
| 科幻小说|十六|科幻小说,3.7%|
| 不详|第二章|未知,0.5%|
问题是默认配色方案的变量太多。我需要创建自己的变量。我知道堆栈溢出中有无数关于调色板的问题。我试过了,但无法使它们工作。有人能告诉我哪里做错了吗?
以下是使用默认配色方案时图表的外观:
附带问题有人知道如何去除周围微小的灰色数字(300、400、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"))
我试着创建自己的调色板。使用的十六进制值来自color brewer
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:将scale_manual切换为scale_fill_brewer,然后再次尝试方法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")
方法D:将十六进制颜色代码切换为基本r颜色,然后再次尝试方法A/B
#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")
方法A的错误消息:
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的错误消息:
Error in discrete_scale(aesthetics, "brewer", brewer_pal(type, palette, :
unused arguments (values = "#fb9a99", "#d4b9da", "#006d2c", "#238443", "#525252", "#4d004b",
"#225ea8", "#8c510a")
方法D的错误消息:
-Method B: same as method B
-Method A: see below:
当我重新运行ggplot代码时,显示的颜色比例如下:
颜色放错了地方,仍然有变量没有自己的切片。有什么建议吗?我做错了什么?我不需要那些特定的颜色-我只需要19种颜色-任何颜色。
1条答案
按热度按时间zvms9eto1#
我面对着同样的20多种颜色的饼图,尝试了你的方法。实际上,你可以把你的程序