如何解决自定义标题并将文本添加到构面中?
有人可以帮我编辑我的地图标题吗?我正在尝试在方面中编辑条形图的标题,但是以某种方式无法正常工作,我也想在每个条形中添加文本,例如“ a”,“ b”或“ ab”(某种形式的统计解释)。 这是我的代码:
df <- data.frame(
H1 = c(6.36,3.03,6.85,4.07,4.69,6.27,6.67,3.11,5.07,6.14,5.93,6.49),H2 = c(5.15,5.00,5.71,5.50,4.99,5.81,6.05,5.76,5.28,5.69,5.06),H3 = c(3.85,5.13,4.91,5.01,5.73,5.77,5.94,5.57,5.35,6.00,4.39),H4 = c(3.84,4.80,5.15,4.85,5.45,5.44,5.41,4.46),H5 = c(4.08,5.17,4.77,5.03,5.49,5.80,5.51,5.18,4.60),H6 = c(4.35,5.59,4.83,5.52,5.63,5.85,5.74,5.66,5.19,5.79,4.84),fontface = c("bold"),names = c("Russian Banana","Vermillion","Atlantic","POR12PG28-3","Valery","Rio Colorado","CO99076-6R","Purple Majesty","AC99330-1P/Y","CO05068-1RU","Masquerade","Canela Russet"),specie = c(rep("Appearance",12),rep("Aroma",rep("Flavor",rep("Overall",rep("Aftertaste",rep("Texture",12)),condition = rep(c("Russian Banana",6))
df <- df %>%
pivot_longer(starts_with("H"))
nameframe <- enframe(unique(df$name))
specieframe <- enframe(unique(df$specie))
specie.labs <- c("Appearance","Aroma","Flavor","Overall","Aftertaste","Texture")
names(specie.labs) <- c("H1","H2","H3","H4","H5","H6")
(filtframe <- inner_join(nameframe,specieframe,by = "name") %>% mutate(
filtcont =
paste0(
"(name=='",value.x,"' & ","specie=='",value.y,"')"
)
))
(filtcond <- paste0(filtframe$filtcont,collapse = " | "))
df_filt <- filter(
df,!!rlang::parse_expr(filtcond)
)
ggplot() +
geom_col(data = df_filt,mapping = aes(x = names,y = value,fill = specie),position = "dodge") +
coord_flip() +
labs(y = "",x = "") + theme(legend.title = element_blank()) +
scale_fill_discrete(breaks=c("Appearance","Texture")) +
facet_wrap(vars(name),labeller = labeller(specie = specie.labs))
解决方法
像这样创建library(dplyr)
library(tidyr)
df2 %>%
pivot_longer(cols = everything(),values_to = 'aa') %>%
mutate(aa = replace(aa,aa == '-','')) %>%
left_join(df1,by = 'aa') %>%
arrange(name) %>%
group_by(name) %>%
mutate(row = row_number()) %>%
pivot_wider(names_from = name,values_from = c(aa,scale)) %>%
select(-row)
# aa_col1 aa_col2 scale_col1 scale_col2
# <chr> <chr> <chr> <chr>
# 1 "" D NA -0.9
# 2 "" D NA -0.9
# 3 "E" T -0.74 -0.05
# 4 "S" L -0.18 1.06
# 5 "P" D 0.12 -0.9
# 6 "V" D 1.08 -0.9
# 7 "F" S 1.19 -0.18
# 8 "A" D 0.62 -0.9
# 9 "F" E 1.19 -0.74
#10 "P" D 0.12 -0.9
# … with 15 more rows
:
labeller
然后在map_species <- function(species) specie.labs[species]
中使用公式符号,并在facet_wrap()
中引用换行列(我称其为h_name
):
labeller()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。