0
この種の質問は既に聞かれていますが、私はすでに数時間、データセットに適用しようとしましたが、成功しませんでした。Rの積み上げ棒グラフと凡例のグループの順序を変更しますか?
は、ここで私はグループの順序を調整する二つの方法を試してみました
> 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:
ウェイ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
私にこの結果を与える:あなただけでreverse
パラメータを使用する必要が