R 在ggplot2中对图例进行排序

R 在ggplot2中对图例进行排序,r,ggplot2,R,Ggplot2,我已根据csv文件中的以下数据生成了堆叠百分比条形图 ,ONE,TWO,THREE 1,2432,420,18 2,276,405,56 3,119,189,110 4,90,163,140 5,206,280,200 6,1389,1080,1075 7,3983,3258,4878 8,7123,15828,28111 9,8608,48721,52576 10,9639,44725,55951 11,8323,45695,32166 12,2496,18254,26600 13,1524,

我已根据csv文件中的以下数据生成了堆叠百分比条形图

,ONE,TWO,THREE
1,2432,420,18
2,276,405,56
3,119,189,110
4,90,163,140
5,206,280,200
6,1389,1080,1075
7,3983,3258,4878
8,7123,15828,28111
9,8608,48721,52576
10,9639,44725,55951
11,8323,45695,32166
12,2496,18254,26600
13,1524,8591,18583
14,7861,1857,1680
15,10269,5165,4618
16,13560,64636,63262
使用以下代码

library(ggplot2)
library(reshape2)
library(scales)

data <- read.csv(file="file.csv",sep=",",header=TRUE)
data <- data[,2:ncol(data)]
datam <- melt(cbind(data,ind = sort(rownames(data))),is.var = c('ind'))
datam$ind <- as.numeric(datam$ind)
ggplot(datam,aes(x = variable, y = value,fill = factor(as.numeric(ind)))) +
geom_bar(position = "fill") + scale_y_continuous(labels =percent_format()) +
scale_fill_discrete("Barcode\nMatch")  +xlab("Barcode")+ylab("Reads")
库(ggplot2)
图书馆(E2)
图书馆(比例尺)
数据添加
+比例填充色调(中断=c(“新订单1”、“新订单2”、“新订单…”)
,如下所示:

library(ggplot2)
ggplot(data=PlantGrowth, aes(x=group, fill=group)) + geom_bar() + 
    geom_bar(colour="black", legend=FALSE) + 
    scale_fill_hue(breaks=c("trt1","ctrl","trt2"))
我还想了解更多信息

使用新的ggplot,这可能已经改变,变得更容易,但我不确定。

添加
+比例填充色调(breaks=c(“新顺序1”,“新顺序2”,“新顺序…”)
,如:

library(ggplot2)
ggplot(data=PlantGrowth, aes(x=group, fill=group)) + geom_bar() + 
    geom_bar(colour="black", legend=FALSE) + 
    scale_fill_hue(breaks=c("trt1","ctrl","trt2"))
我还想了解更多信息


使用新的ggplot,这可能已经改变,变得更容易,但我不确定。

您可以使用一个新选项
reverse=TRUE

ggplot(datam,aes(x = variable, y = value,fill = factor(as.numeric(ind)))) +
  geom_bar(position = "fill") + scale_y_continuous(labels =percent_format()) +
  scale_fill_discrete("Barcode\nMatch") + xlab("Barcode")+ylab("Reads") +
  guides(fill = guide_legend(reverse = TRUE))

您可以使用一个新选项
reverse=TRUE

ggplot(datam,aes(x = variable, y = value,fill = factor(as.numeric(ind)))) +
  geom_bar(position = "fill") + scale_y_continuous(labels =percent_format()) +
  scale_fill_discrete("Barcode\nMatch") + xlab("Barcode")+ylab("Reads") +
  guides(fill = guide_legend(reverse = TRUE))

此处是有关更多图例信息的更新链接:。此处是有关更多图例信息的更新链接:。