R 标签出现故障(ggplot2)

R 标签出现故障(ggplot2),r,ggplot2,geom-text,R,Ggplot2,Geom Text,将标签添加到绘图时,它们的顺序错误。我想重新排列标签,而不是条本身。示例代码: df = data.frame(A = c("Apples", "Apples", "Apples", "Apples", "Apples", "Apples", "Oranges", "Oranges", "Oranges", "Oranges", "Oranges", "Oranges"), B = c("Red", "Green", "

将标签添加到绘图时,它们的顺序错误。我想重新排列标签,而不是条本身。示例代码:

df = data.frame(A = c("Apples", "Apples", "Apples", "Apples", "Apples", "Apples",
                      "Oranges", "Oranges", "Oranges", "Oranges", "Oranges", "Oranges"),
                B = c("Red", "Green", "Red", "Green", "Red", "Green",
                      "Red", "Green", "Red", "Green", "Red", "Green"),
                C = c(3, 4, 2, 3, 4, 6, 2, 2, 3, 8, 8, 6))

library(tidyverse)

df.summary = df %>%
  group_by(A, B) %>%
  summarise(new = list(mean_se(C))) %>%
  unnest(new)
df.summary$B <- factor(df.summary$B, levels = c("Red", "Green"))

df.labels = c(1, 2, 3, 4)

ggplot(df.summary, aes(x = A, y = y, fill = factor(B))) +
  geom_bar(stat = "identity", position = position_dodge(.95)) +
  geom_errorbar(aes(ymin = ymin, ymax = ymax, width = 0.5), 
                position = position_dodge(.95)) +
  geom_text(position = position_dodge(width = 1), size = 6, 
            label = df.labels, hjust = .5, vjust = 0, angle = 0)
df=data.frame(A=c(“苹果”,“苹果”,“苹果”,“苹果”,“苹果”,“苹果”,“苹果”,
“橙子”、“橙子”、“橙子”、“橙子”、“橙子”、“橙子”、“橙子”),
B=c(“红色”、“绿色”、“红色”、“绿色”、“红色”、“绿色”),
“红”、“绿”、“红”、“绿”、“红”、“绿”),
C=C(3,4,2,3,4,6,2,2,3,8,6))
图书馆(tidyverse)
df.summary=df%>%
(A,B)组%>%
总结(新=列表(平均值(C)))%>%
unnest(新)

df.summary$B这有帮助吗?假设A和B是因子

 library(tidyverse)
    df.summary$A<-fct_reorder(df.summary$A,df.labels)
    df.summary$B<-fct_reorder(df.summary$B,df.labels)
    ggplot(df.summary, aes(x=A, y=y, fill=B))+
      geom_bar(stat = "identity", position = position_dodge(.95))+
      geom_errorbar(aes(ymin=ymin, ymax=ymax, width=0.5), position=position_dodge(.95))+
      geom_text(position = position_dodge(width = 1), size = 6, aes(label = df.labels),
                hjust = .5, vjust = 0, angle = 0)
库(tidyverse)

df.summary$AI在图片中看不到这些数字。无论如何,你能试着把标签放到data.frame中吗?另外,您应该以一种不需要加载某些未知包的方式提供数据。抱歉,上载了错误的文件。现在已修复,并更新为接受NelsonGon的答案。看起来将label=df.labels更改为aes(label=df.labels)成功了。谢谢