【问题标题】:Change order of groups in stacked bar chart AND legend in R?更改堆叠条形图中组的顺序和R中的图例?
【发布时间】:2017-02-21 14:34:04
【问题描述】:

我知道已经有人问过这类问题,但我已经尝试将其应用于我的数据集几个小时,但没有成功。

这是我的数据框

> df
        prot    X nr
1 unmodified same 68
2       1 Ac same 14
3       2 Ac same  7
4       3 Ac same  4
5       4 Ac same  3
6       5 Ac same  2
7       6 Ac same  1
8       7 Ac same  2

我尝试了两种调整组顺序的方法,但在这两种情况下,图例的顺序正好相反。如何将图中组的顺序与图例中的组匹配? 方式1:

y <- read.csv("df.csv",sep=",") #sep=";"
df <- data.frame(y)

df$prot <- factor(df$prot, levels = rev(df$prot))   #order groups

p<-ggplot(df, aes(x=X, y=nr, fill=prot)) +
geom_bar(stat="identity", position=position_stack(), width=0.2, colour="gray", aes(fill = prot))+ #  stacked, bar-width, outline-colour = colour
theme(panel.background = element_rect(fill = "ivory" , colour="ivory"))+
coord_flip()+
theme(legend.position="bottom") +
guides(fill = guide_legend(nrow = 1)) +
geom_text(aes(label=nr), color="black",vjust = -3, position = position_stack())   # add labels (%) non-overlapping

p

给我这个结果:

方式2:

y <- read.csv("df.csv",sep=",") #sep=";"
df <- data.frame(y)

df$prot <- factor(df$prot, levels = c("7 Ac", "6 Ac", "5 Ac", "4 Ac", "3 Ac", "2 Ac", "1 Ac","unmodified"))   #order groups

df <- ddply(df, .(X),
        transform, pos = cumsum(nr) - (0.5 * nr)) #adjust the position of the data labels:create new variable at the centre of each bar 

p<-ggplot(data=df, aes(x=X, y=nr, fill=prot)) +
geom_bar(stat="identity", position=position_stack(), width=0.2, colour="gray")+ #  stacked, bar-width, outline-colour = colour
theme(panel.background = element_rect(fill = "ivory" , colour="ivory"))+ #backgroundcolor
coord_flip()+
theme(legend.position="bottom") +
guides(fill = guide_legend(nrow = 1)) +
geom_text(data=df, aes(x = X, y = pos, label = paste0(nr,"%")),
        size=4)

p

p

给我这个结果:

【问题讨论】:

    标签: r ggplot2 legend stacked


    【解决方案1】:

    只需要使用guide_legend中的reverse参数,并将其设置为TRUE,如:

    df<- data.frame(prot = c("unmodified", paste(1:7, "AC")), X = rep("same", 8),
                       nr = c(68,14,7,4,3,2,1,2))
    
    library(ggplot2)
    
    df$prot <- factor(df$prot, levels = rev(df$prot))   #order groups
    
    p<-ggplot(df, aes(x=X, y=nr, fill=prot)) +
            geom_bar(stat="identity", position=position_stack(), width=0.2, colour="gray", aes(fill = prot))+ #  stacked, bar-width, outline-colour = colour
            theme(panel.background = element_rect(fill = "ivory" , colour="ivory"))+
            coord_flip()+
            theme(legend.position="bottom") +
            # to change ordering of legend, set reverse = TRUE
            guides(fill = guide_legend(nrow = 1, reverse = TRUE)) +
            geom_text(aes(label=nr), color="black",vjust = -3, position = position_stack())   # add labels (%) non-overlapping
    
    p
    

    【讨论】:

      猜你喜欢
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      相关资源
      最近更新 更多