2015-02-07 53 views
1

我有一個比例vs名稱的圖。我試圖通過比例來命令一個ggplot,但即使我命令數據幀,該圖也想根據x軸值按字母順序排列事情。如何以Y軸值改爲訂購如何根據變量在ggplot中訂購點我指定

resultOrder <- result[order(result$Proportion), ] 

ggplot() + 
geom_point(aes(resultOrder$Names, resultOrder$Proportion), resultOrder) + 
geom_point(shape=1) + 
labs(title="Number of SVs each repeat element is found in (as a percentage,  filtered for >20%)", x="TY [°C]", y="Txxx") + 
#geom_point(aes(mergedGroup4$Rptname, mergedGroup4$PercentageChangeForWholeSV),mergedGroup4) + theme(axis.text.x=element_text(angle=-90)) + 
xlab("Repetitive elements") + 
ylab("Percentage of SVs") + 
theme(axis.text.x=element_text(angle=-90)) + 
theme(legend.position="top") 

的樣本數據

      Names Values Proportion 
FLAM_C     FLAM_C 1112 20.03965 
MER112     MER112 1115 20.09371 
L1MA10     L1MA10 1116 20.11173 
L1PB3      L1PB3 1121 20.20184 
LTR78B     LTR78B 1125 20.27392 
MLT1H1     MLT1H1 1126 20.29194 
(TG)n      (TG)n 1127 20.30997 
Charlie7    Charlie7 1129 20.34601 
MamRep605    MamRep605 1133 20.41809 
LTR16A     LTR16A 1136 20.47216 
Charlie1b    Charlie1b 1139 20.52622 
L1PA6      L1PA6 1142 20.58028 
MLT1G1     MLT1G1 1148 20.68841 
LTR67B     LTR67B 1150 20.72445 
MER58A     MER58A 1162 20.94071 
+0

您可以將您的原data.frame結果的代表性? – 2015-02-07 21:17:39

+1

請參閱'help(reorder)' – davechilders 2015-02-07 21:22:10

+0

請參閱下面的答案。 – 2015-02-07 22:37:06

回答

1

DMC是正確的。

試試這個,因爲我簡化了你的ggplot調用。對我來說,訣竅是將平均參數的reorder

df <- read.table(file = "clipboard") 

ggplot(df) + 
    geom_point(aes(reorder(Names, Proportion, mean), y=Proportion)) + 
    coord_flip() 

enter image description here

1

你需要設置你的Names爲取決於Proportion順序,使ggplot沒有重新排序的因素。所以,試試這個:

df$Names = factor(df$Names, levels=df[order(df$Proportion), "Names"]) 

ggplot(df, aes(Names, Proportion)) + geom_point(shape=1) 

enter image description here

我還重新寫的ggplot第一線,因爲你用的方法是相當複雜的,能夠以更簡單的方式來完成。