문제
가 데이터 프레임 df.all
나는 음모를 꾸미에서 그것을 바 줄거리와 ggplot2 코드를 사용하여 아래.하고 싶습니다 그래서 그것의 순서를 회피 바가 뒤집혔다.는,그래서는 바에 표시"단수형"오기 전에 바로,"복수".
ggplot(df.all, aes(gram, V1, fill=number)) +
geom_bar(stat="identity", position="dodge") +
scale_x_discrete(labels=c("Grammatical","Ungrammatical")) +
scale_y_continuous(formatter="percent", limits=c(0,1)) +
facet_grid(. ~ experiment) +
scale_fill_hue("Attractor", breaks=c("S","P"), labels=c("Singular","Plural"))
해봤고 levels(df.all$number) = c("S", "P")
고 생각하는 어쩌면 ggplot 의 순서를 사용하여 레벨을 결정하는 음모를 꾸미기 위해,하지만 그 작동하지 않았다.나는 확실하지 않는 다른 무엇을 시도합니다.어떤 아이디어가?
의 내용 df.all
, 는 경우에,그것은 유용합니다:
> df.all
number gram experiment V1
1 S G BERIMBAU_AGR_A 0.8133333
2 S G BERIMBAU_AGR_B 0.8658537
3 S U BERIMBAU_AGR_A 0.5436242
4 S U BERIMBAU_AGR_B 0.4597701
5 P G BERIMBAU_AGR_A 0.8580645
6 P G BERIMBAU_AGR_B 0.8536585
7 P U BERIMBAU_AGR_A 0.3087248
8 P U BERIMBAU_AGR_B 0.3975904
> str(df.all)
'data.frame': 8 obs. of 4 variables:
$ number : Factor w/ 2 levels "S","P": 2 2 2 2 1 1 1 1
..- attr(*, "scores")= num [1:2(1d)] 0 -1
.. ..- attr(*, "dimnames")=List of 1
.. .. ..$ : chr "P" "S"
$ gram : Factor w/ 2 levels "G","U": 1 1 2 2 1 1 2 2
$ experiment: Factor w/ 4 levels "BERIMBAU_AGR_A",..: 1 4 1 4 1 4 1 4
$ V1 : num 0.813 0.866 0.544 0.46 0.858 ...
해결책 3
해들리는 솔루션을 제공했습니다. 다음은 문제와 해결책의 복제입니다.
목표는 "S"라는 바를 "P"로 표시하는 막대 앞에 오도록하는 것입니다. r은 알파벳 순서로 레벨을 내기 때문에 기본적으로 발생하지 않습니다.
df <- read.csv("http://pealco.net/code/ggplot_dodge/df.txt")
ggplot(df, aes(gram, V1, fill=number))
+ geom_bar(stat="identity", position="dodge")
Hadley가 다른 답변에서 언급했듯이 "Y 변수가 아닌 X 변수를 기반으로 재주문해야합니다". 이것이 왜 작동하는지 잘 모르겠지만.
이 예제의 요인 순서를 뒤집기 위해 계수를 숫자로 변환하고 -1을 곱할 수 있습니다.
df <- with(df, df[order(gram, -as.numeric(number)), ])
나는 여전히 이유에 대한 더 많은 설명을 원합니다 df <- with(df, df[order(gram, -as.numeric(number)), ])
공장.
다른 팁
제 생각에는 df.all$number
주문 요인이어야합니다. 노력하다 df.all$number <- ordered(df.all$number)
어떤 경우에는 이것이 불가능하다고 생각하지 않습니다.
layerCake<-data.frame(group=c(rep("normal",4),rep("tumor",4)),
class=factor(rep(c("exon","intron","intergenic","unmapped"),2),levels=rev(c("exon","intron","intergenic","unmapped")),ordered=TRUE),
fraction=c(.02,.25,.50,.23,.015,.20,.555,.23)
)
layerCake[layerCake$group=='normal',"reads"]<-130948403*layerCake[layerCake$group=='normal',"fraction"]
layerCake[layerCake$group=='tumor',"reads"]<-200948403*layerCake[layerCake$group=='tumor',"fraction"]
g<-ggplot(layerCake, aes(x=factor(group),y=reads, fill=factor(class),order = as.numeric(class)))+xlab("Group")+scale_fill_discrete(name="Anno Class",breaks=c("exon","intron","intergenic","unmapped"))
쌓인 올바른 순서 :
g+geom_bar (stat = "Identity", position = "stack")
닷지에서 잘못된 순서 :
g+geom_bar(stat="identity",position="dodge")
GGPlot에서 순서를 뒤집어 보자.
g<-ggplot(lc, aes(x=factor(group),y=reads, fill=factor(class),order = -as.numeric(class)))+xlab("Group")+scale_fill_discrete(name="Anno Class",breaks=c("exon","intron","intergenic","unmapped"))
g+geom_bar(stat="identity",position="dodge")
주사위가 없습니다
데이터 프레임을 재정렬하려고합니다
lc <- with(lc, lc[order(-as.numeric(class)), ])
g<-ggplot(lc, aes(x=factor(group),y=reads, fill=factor(class),order = -as.numeric(class)))+xlab("Group")+scale_fill_discrete(name="Anno Class",breaks=c("exon","intron","intergenic","unmapped"))
g+geom_bar(stat="identity",position="dodge")
아니요
변화하는 요인 수준 정말 순서를 변경의 피됩니다.일반적인 함정:의 색상이 여전히 어떤 위치에서,이렇게 빠른 눈처럼 보이게하기 위해 변경되지 않았습니다.하지만 경우에 당신은 값을 살펴보고 당신이 볼 수는 순서가 변경되었습니다.
편집:이전 대답만 아래의 순서가 변경 색상에 주어진 바가 있습니다.이것은 여전히 유용하고,우리가 종종 있습니다 원하는 역 색 구성표와 동시에 변화의 순서는 바
용 scale_fill_manual 고 싶었기 때문에 수동으로 채우기의 색상이 나 있습니다.
ggplot(data, aes_string(x = "countries", y = "population", fill = "agegroups")) +
scale_fill_manual(values = CustomColorFunction(), limits = (levels(data$agegroups)))
5 시간을 보냈 땜질로 변화하는 요인 수준이고 배치 데이터 프레임 희망이 누군가가 도움이 됩!