如何根据我指定的变量在 ggplot 中排序点
How to order points in a ggplot based on a variable I specify
我有一个比例与名称的关系图。我试图按比例订购一个 ggplot,但即使我订购了数据框,该图仍希望根据 x 轴值按字母顺序排列。我如何改为按 y 轴值排序
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
您需要根据 Proportion
顺序将 Names
设置为因子,这样 ggplot
就不会重新排序。所以试试这个:
df$Names = factor(df$Names, levels=df[order(df$Proportion), "Names"])
ggplot(df, aes(Names, Proportion)) + geom_point(shape=1)
我也重写了ggplot
的第一行,因为你的使用方式比较复杂,可以用更简单的方式来完成。
DMC 正确。
试试这个,因为我简化了你的 ggplot 调用。对我来说,诀窍是将 mean 参数添加到 reorder
:
df <- read.table(file = "clipboard")
ggplot(df) +
geom_point(aes(reorder(Names, Proportion, mean), y=Proportion)) +
coord_flip()
我有一个比例与名称的关系图。我试图按比例订购一个 ggplot,但即使我订购了数据框,该图仍希望根据 x 轴值按字母顺序排列。我如何改为按 y 轴值排序
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
您需要根据 Proportion
顺序将 Names
设置为因子,这样 ggplot
就不会重新排序。所以试试这个:
df$Names = factor(df$Names, levels=df[order(df$Proportion), "Names"])
ggplot(df, aes(Names, Proportion)) + geom_point(shape=1)
我也重写了ggplot
的第一行,因为你的使用方式比较复杂,可以用更简单的方式来完成。
DMC 正确。
试试这个,因为我简化了你的 ggplot 调用。对我来说,诀窍是将 mean 参数添加到 reorder
:
df <- read.table(file = "clipboard")
ggplot(df) +
geom_point(aes(reorder(Names, Proportion, mean), y=Proportion)) +
coord_flip()