r - 如何使用列作为ggplot2 R中的图例值和列属性作为标题
问题描述
我有这个数据:
dput(padmin1)
structure(list(q0005_0001 = structure(c(5, 4, 3, 3, 4, 4, NA,
3, NA, 4, 4, 4, 3, 4, 4, 4, 3, 4, 3, 4, NA, 3, 5, 4, NA, 4, 4,
3, 3, 4, 4, NA, 5, 5, 5, 3, 5, 4, 4, 4, 4, 4, 4, 5, 3, 3, 5,
2, 4, NA, 3, 4, 4, 5, 5, 3, 4, 3, 3, 4, 5, 4, 3, 4, 4, 4, 4,
3, 4, 4, 5, 4, 5, NA, 4, 4, NA, 3, 4, 4, 5, NA, 5, 4, 4, 4), format.spss = "F8.2", class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(`No reciboinformación` = 0,
Insuficiente1 = 1, `Poco Suficiente2` = 2, Regular3 = 3, Suficiente4 = 4,
`Muy Suficiente5` = 5), label = "La información brindada por la facultad le resultó...")), row.names = c(NA,
-86L), class = c("tbl_df", "tbl", "data.frame"), label = "File created by user 'asyncjobs_user' at Mon Jul 5 19:01:40 202")
和以下代码:
padmin1 %>%
#Ordenando la data
rename(Var1=q0005_0001) %>%
group_by(Var1) %>%
select(Var1) %>%
haven::as_factor() %>%
table() %>%
as.data.frame() %>%
mutate(Freq = round(100 * Freq/sum(Freq), 0),
Colores = c( "#7F7F7F", "#8A0000", "#FFCD2F", "#DAA600", "#144D6C", "#071C27")) %>%
filter(Freq != 0) %>%
#Para el gráfico
ggplot(aes(x = "", y = Freq, fill = Colores )) +
scale_fill_identity() +
geom_bar(stat = "identity", width = 0.2) +
#Texto
geom_text(aes(label = paste0(Freq, "%"),
color = ifelse(grepl("^(#7F7F7F|#8A0000|#FFCD2F)", Colores), "black", "white"),
group = fct_rev(ordered(.)) ), position = position_stack(vjust=0.5) , fontface = "bold") +
scale_color_identity() +
#Rotar gráfico
coord_flip() +
#Título y pie de página
labs(title= "La información brindada por la facultad le resultó...", caption = "Enterprise, 2021") +
#Temas de colores
theme(axis.title = element_blank(),
line = element_blank(),
panel.background = element_rect(fill = "transparent", color = NA),
plot.background = element_rect(fill = "transparent", color = NA),
legend.position = "bottom",
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.background = element_rect(fill = "transparent", linetype = "solid", colour = "transparent"),
legend.box.background = element_rect(fill = "transparent", colour = "transparent"),
axis.text = element_blank())
要求:
我想在不牺牲 HEX 颜色的情况下使用值(Poco Suficiente2、Regular3、Suficiente4、Muy Suficiente5)作为底部的图例,因为按该顺序着色对我来说很重要。
我的第二个请求是使用列的属性作为图形的标题,因为我正在处理从 SPSS 导入的标记数据。
补充说明:
*我期待着从数据中尽可能地使这个图形自动化,这就是为什么我使用 dplyr 的管道系统来创建一个表并从那里工作,所以解决方案应该从那里开始而不是创建额外的对象。
*最后,我希望输出看起来像这样,但在开始时指定给定数据以及第一个图形的数字标签的颜色(黑色表示亮条颜色,白色表示暗条颜色):
非常感谢 Stackoverflow 社区,我期待您的反馈。随时向我询问更多详细信息。和平
解决方案
推荐阅读
- td-engine - 如何使用 tdengine 的 Aggregation
- google-bigquery - 基于存储过程创建视图或使用其结果创建表
- flutter - 如何为 Prefixicon Flutter 添加背景
- c++ - 我该如何解决:找不到-lsqlite3?
- javascript - HTML列表项被滚动功能instgram隐藏
- python - 写入 10 位/颜色(位深度)alpha 通道图像的问题
- java - 在@Test 中撤销浓缩咖啡测试的权限
- powerbuilder - 在 powerbuilder 文本框中打印整个数组
- android - 有没有办法将音频字节发送到 SpeechRecognizer
- kotlin - Modifier.clip 被忽略