【问题标题】:How to add test onto each bar?如何在每个栏上添加文字?
【发布时间】:2021-03-11 03:07:47
【问题描述】:

我正在尝试在每个栏上添加文本,有人可以帮我展示我该怎么做吗?我不是 R 用户,所以请原谅我的能力。我感谢您的帮助。我之前收到的回复很少,喜欢使用annotategeomtext,但我不知道如何运行它们。 这是我的代码:

library(viridis)
library(hrbrthemes)
library(tidyr)
library(dplyr)
library(tibble)
library(ggplot2)
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.69, 5.06),
  H3 = c(3.85, 5.13, 4.99, 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, 4.99, 5.73, 5.77, 5.45, 5.44, 5.41, 5.81, 4.46),
  H5 = c(4.08, 5.17, 4.77, 5.03, 5.00, 5.49, 5.49, 5.80, 5.51, 5.18, 5.76, 4.60),
  H6 = c(4.35, 5.59, 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("RB", "Ver", "Atl", "POR12PG28-3",
            "Valery", "Rio", "CO99076-6R", "Purple",
            "AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela"),
  specie = c(rep("Appearance", 12), rep("Aroma" , 12), rep("Flavor" , 12),
             rep("Overall" , 12), rep("Aftertaste", 12), rep("Texture", 12)),
  condition = rep(c("RB", "Ver", "Atl", "POR12PG28-3",
                    "Valery", "Rio", "CO99076-6R", "Purple",
                    "AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela") , 6))

df <- df %>%
  pivot_longer(starts_with("H"), names_to = "h.names")
df

#one condition per plot
nameframe <- enframe(unique(df$h.names))
specieframe <- enframe(unique(df$specie))
names.labs <- c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")
names(names.labs) <- c("H1", "H2", "H3", "H4", "H5", "H6")

ggplot() +
  geom_col(data = df, mapping = aes(x = names, y = value), 
           position = "dodge") +
  coord_flip() +
  ylim(c(0,9)) +
  scale_y_continuous(breaks=seq(0.0, 9, 3), limits=c(0, 9), labels = c("0", "3", "6", "Like\nExtremely")) +
  labs(y = "", x = "") + theme(legend.title = element_blank(), axis.text.y = element_text(face = "bold", size = 11),
                               axis.text.x = element_text(face = "bold", size = 9)) +
  scale_fill_discrete(breaks = c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")) +
  facet_wrap(~h.names, labeller = labeller(h.names = names.labs))


#add text onto each bar
p <- p + annotate("text", label = "Test", size = 4, x = 15, y = 5)
print(p)
text(x = H,
     y = y,
     labels = c("ab", "e", "a", "d", "cd", "ab", "ab", "e", "c", "ab", "b", "ab"),
     pos = 2)

【问题讨论】:

标签: r ggplot2 bar-chart facet-wrap


【解决方案1】:

以下是您将原始问题中的文本添加到每个栏的方法。请注意,我将data = df, mapping = aes(x = names, y = value 移动到ggplot() 美学中,它将应用于情节中的每一层。接下来,使用dplyr 包中的case_when 将条形图标签作为新列添加到df。之后,您将新列传递到geom_text,就像geom_text(aes(label = bar_labels, hjust = 0)) 一样,将标签应用到每个条的尖端。

library(viridis)
library(tidyr)
library(dplyr)
library(tibble)
library(ggplot2)

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.69, 5.06),
  H3 = c(3.85, 5.13, 4.99, 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, 4.99, 5.73, 5.77, 5.45, 5.44, 5.41, 5.81, 4.46),
  H5 = c(4.08, 5.17, 4.77, 5.03, 5.00, 5.49, 5.49, 5.80, 5.51, 5.18, 5.76, 4.60),
  H6 = c(4.35, 5.59, 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("RB", "Ver", "Atl", "POR12PG28-3",
            "Valery", "Rio", "CO99076-6R", "Purple",
            "AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela"),
  specie = c(rep("Appearance", 12), rep("Aroma" , 12), rep("Flavor" , 12),
             rep("Overall" , 12), rep("Aftertaste", 12), rep("Texture", 12)),
  condition = rep(c("RB", "Ver", "Atl", "POR12PG28-3",
                    "Valery", "Rio", "CO99076-6R", "Purple",
                    "AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela") , 6))

df <- df %>%
  pivot_longer(starts_with("H"), names_to = "h.names")


#one condition per plot
nameframe <- enframe(unique(df$h.names))
specieframe <- enframe(unique(df$specie))
names.labs <- c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")
names(names.labs) <- c("H1", "H2", "H3", "H4", "H5", "H6")


#add text onto each bar
df <- df %>% 
  arrange(desc(names)) %>% 
  group_by(names) %>% 
  mutate(
    bar_labels = case_when(
      names == "Ver" ~ "ab",
      names == "Valery" ~ "e",
      names == "Rio" ~ "a",
      names == "RB" ~ "d",
      names == "Purple" ~ "cd",
      names == "POR12PG28-3" ~ "ab",
      names ==  "Masquerade" ~ "ab",
      names == "CO99076-6R" ~ "e",
      names == "CO05068-1RU" ~ "c",
      names == "Canela" ~ "ab",
      names == "Atl" ~ "b",
      names == "AC99330-1P/Y" ~ "ab",
      TRUE ~ as.character(NA)
    ))

ggplot(data = df, mapping = aes(x = names, y = value)) +
  geom_col(position = "dodge") +
  coord_flip() +
  ylim(c(0,9)) +
  scale_y_continuous(breaks=seq(0.0, 9, 3), limits=c(0, 9), labels = c("0", "3", "6", "Like\nExtremely")) +
  labs(y = "", x = "") + theme(legend.title = element_blank(), axis.text.y = element_text(face = "bold", size = 11),
                               axis.text.x = element_text(face = "bold", size = 9)) +
  scale_fill_discrete(breaks = c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")) +
  facet_wrap(~h.names, labeller = labeller(h.names = names.labs)) +
  geom_text(aes(label = bar_labels, hjust = 0))

reprex package (v0.3.0) 于 2021-03-10 创建

【讨论】:

  • 感谢您的帮助。让我吸收你所说的。非常感谢。
  • 如何在每个类别下添加特定文本?就像,现在它们对所有样本都具有相同的特性,例如所有条形图中的 Ver - ab。同样 Atl - b 在所有地块中?
  • 采用此解决方案并在 StackOverflow 上创建另一个问题。最佳做法是不要继续在已回答的解决方案中添加 cmets 中的新问题。相反,提问者应该在已经回答的问题之外再问一个新问题,因为从技术上讲,它们是两个独立的问题,因此保留了已经捕获的信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多