在每个栏上添加自定义文本

Add custom text by onto each bars

谁能帮我把文字放到每个栏上。我正在使用 geomtext 函数,但不确定如何在每个栏上放置文本这是代码:

df <- read.csv("F:/open_end_frequency_response.csv")
colnames(df)[1] = "modality"
colnames(df)[2] = "First_response"
colnames(df)[3] = "Second_response"
colnames(df)[4] = "Third_response"
colnames(df)[5] = "Fourth_response"
colnames(df)[6] = "Fifth_response"
dput(df)
colnames(df) <- c("modality","First_response","Second_response","Third_response",
                  "Fourth_response", "Fifth_response", "Frequency")

df2 <- select(df, -Frequency)
dat <- melt(df2)

#Using modality as id variables
p1 <- ggplot(dat, aes(modality, value, fill=interaction(variable))) +
  geom_bar(stat='identity', position='dodge') +
  ggtitle("A: General OEQ") + 
  theme_bw() + theme(axis.text.x = element_text(angle=90, hjust=1, size = 10, face = "bold")) +
  scale_fill_brewer('Open-end', palette='RdPu') +  theme(legend.position = c(.51, .80)) +
  theme(legend.title=element_blank()) +
  xlab("") + ylab("Frequency") + theme(panel.border = element_blank()) + 
  theme(axis.line = element_line(colour = "black")) + 
  theme(legend.key.size = unit(1.0, "cm"), legend.key.width = unit(0.5,"cm"),
        legend.key.height = unit(0.5,"cm")) + 
    theme(legend.text = element_text(size = 7)) +
  geom_text(aes(label = "a"), position = position_dodge(0.9), size = 2,
            vjust = -0.25, fontface="bold")

请在下面找到我的一段数据,希望这对您玩转数据有所帮助。另外我不确定如何在 Whosebug 上共享数据,如果有人可以与我共享 link:

squads <- tibble::tribble(
                   ~test, ~first_res, ~second_res, ~third_res, ~fourth_res, ~fifth_res,
            "Appearance",       201L,         72L,        54L,         46L,        39L,
                 "Aroma",         8L,         17L,        17L,         24L,        13L,
                "Flavor",       107L,        148L,       177L,        168L,       122L,
               "Texture",       151L,        225L,       220L,        198L,       150L,
              "Abstract",       282L,        260L,       360L,        356L,       402L
            )
head(squads)
#> # A tibble: 5 x 6
#>   test       first_res second_res third_res fourth_res fifth_res
#>   <chr>          <int>      <int>     <int>      <int>     <int>
#> 1 Appearance       201         72        54         46        39
#> 2 Aroma              8         17        17         24        13
#> 3 Flavor           107        148       177        168       122
#> 4 Texture          151        225       220        198       150
#> 5 Abstract         282        260       360        356       402

reprex package (v2.0.1)

于 2021-09-01 创建

例如,您需要 25 条文本,让 x <- c(1:25)

geom_text(aes(label = x), position = position_dodge(width = 0.9), vjust = -0.25)

ggplot(dat, aes(modality, value, fill=interaction(variable))) +
  geom_bar(stat='identity', position='dodge') +
  ggtitle("A: General OEQ") + 
  theme_bw() + theme(axis.text.x = element_text(angle=90, hjust=1, size = 10, face = "bold")) +
  scale_fill_brewer('Open-end', palette='RdPu') +  theme(legend.position = c(.51, .80)) +
  theme(legend.title=element_blank()) +
  xlab("") + ylab("Frequency") + theme(panel.border = element_blank()) + 
  theme(axis.line = element_line(colour = "black")) + 
  theme(legend.key.size = unit(1.0, "cm"), legend.key.width = unit(0.5,"cm"),
        legend.key.height = unit(0.5,"cm")) + 
  theme(legend.text = element_text(size = 7)) +
  geom_text(aes(label = x), position = position_dodge(width = 0.9), vjust = -0.25)

那么结果就像

plot_grid(p1, p2, labels = c("p1", "p2"))

序列已替换

最终代码编辑(我猜)

dat <- melt(df2)中添加一些代码作为dat <- melt(df2) %>% arrange(modality)。然后,例如x <- c(1:25)dat$text <- x。 现在,要绘制的代码现在是

p1 <- ggplot(dat, aes(modality, value, fill=interaction(variable))) +
  geom_bar(stat='identity', position='dodge') +
  ggtitle("A: General OEQ") + 
  theme_bw() + theme(axis.text.x = element_text(angle=90, hjust=1, size = 10, face = "bold")) +
  scale_fill_brewer('Open-end', palette='RdPu') +  theme(legend.position = c(.51, .80)) +
  theme(legend.title=element_blank()) +
  xlab("") + ylab("Frequency") + theme(panel.border = element_blank()) + 
  theme(axis.line = element_line(colour = "black")) + 
  theme(legend.key.size = unit(1.0, "cm"), legend.key.width = unit(0.5,"cm"),
        legend.key.height = unit(0.5,"cm")) + 
  theme(legend.text = element_text(size = 7)) +
  geom_text(aes(label = text, group = ), position = position_dodge(width = 0.9), vjust = -0.25)