R 错误方向的条形图

R 错误方向的条形图,r,ggplot2,R,Ggplot2,我是一名学习R的生物学研究生。我希望有人能帮助我让这些条水平地向相反的方向移动(蓝色部分应该从0开始,红色部分应该在刻度的100末端) 带错误方向条的图形 这是数据 my_species <- c('apomict_2-17-17_compreh', 'apomict_2-17-17_compreh', 'apomict_2-17-17_compreh', 'apomict_2-17-17_compreh', 'parthenogen_2-17-17_compreh', 'parthen

我是一名学习R的生物学研究生。我希望有人能帮助我让这些条水平地向相反的方向移动(蓝色部分应该从0开始,红色部分应该在刻度的100末端)

带错误方向条的图形

这是数据

my_species <- c('apomict_2-17-17_compreh', 'apomict_2-17-17_compreh', 'apomict_2-17-17_compreh', 'apomict_2-17-17_compreh', 'parthenogen_2-17-17_compreh', 'parthenogen_2-17-17_compreh', 'parthenogen_2-17-17_compreh', 'parthenogen_2-17-17_compreh', 'sexual_2-9-17', 'sexual_2-9-17', 'sexual_2-9-17', 'sexual_2-9-17')
my_species <- factor(my_species)
my_species <- factor(my_species,levels(my_species)[c(length(levels(my_species)):1)]) # reorder your species here just by changing the values in the vector :
my_percentage <- c(36.3, 56.3, 2.6, 4.8, 42.2, 50.6, 2.4, 4.8, 56.0, 19.9, 6.7, 17.4)
my_values <- c(522, 811, 38, 69, 608, 729, 35, 68, 806, 286, 96, 252)
category <- c(rep(c("S","D","F","M"),c(1)))
category <-factor(category)
category = factor(category,levels(category)[c(4,1,2,3)])
df = data.frame(my_species,my_percentage,my_values,category)

my_species您需要重新排列因子级别,以便ggplot2知道该做什么。下面是一个例子(注意,我不得不重新排列标签和颜色):

。。。
#颜色

我的颜色请参见
?堆栈位置

position_fill()和position_stack()自动将值堆叠在 组美学的相反顺序,对于条形图通常是 由填充美学定义(形成默认组美学) 除x和y外,所有离散美学的组合)。 此默认值确保条形图颜色与默认图例对齐

要更改堆叠方向,只需将
position=position\u stack(reverse=TRUE)
添加到
geom\u条

figure <- ggplot() +       
    geom_bar(
        aes(y = my_percentage, x = my_species, fill = category),
        data = df, stat="identity", width=my_bar_height,
        position = position_stack(reverse = TRUE)) + 
    coord_flip() + 
...

图请参见。看看你的代码,我认为答案应该达到你想要的。谢谢。我会让你知道的!很抱歉,那篇文章似乎没有达到我的目的(除非我不能正确地实现它)。我是R新手,这个R脚本大部分是由busco python脚本生成的。我只是想让整个条带(包括彩色组件)水平翻转。没问题,我试图给你指出正确的方向,因为我没有时间提供完整的答案。请参阅下面我的答案,希望它能帮助您根据需要创建绘图。@mt1022的答案更简单,但我想我会把它留在这里,因为它仍然提供了实现这一点的另一种方法。谢谢!谢谢你抽出时间。
...
# Colors
my_colors <- c( "#F04442", "#F0E442", "#3492C7", "#56B4E9")
...
df$category = ordered(df$category, levels = c("M", "F", "D", "S"))

figure <- ggplot(data = df[order(df$category, decreasing = F),]) +       
  geom_bar(aes(y = my_percentage, x = my_species, fill = category), stat="identity", width=my_bar_height) + 
  coord_flip() + 
  theme_gray(base_size = 8) + 
  scale_y_continuous(labels = c("100","80","60","40","20","0"), breaks = c(100,80,60,40,20,0)) +
  scale_fill_manual(values = my_colors,labels =c(" Missing (M)", 
                                                 " Fragmented (F)  ",
                                                 " Complete (C) and duplicated (D)",
                                                 " Complete (C) and single-copy (S)  ")) +   
...
figure <- ggplot() +       
    geom_bar(
        aes(y = my_percentage, x = my_species, fill = category),
        data = df, stat="identity", width=my_bar_height,
        position = position_stack(reverse = TRUE)) + 
    coord_flip() + 
...