为什么我的重新订购在 geom_bar 中不起作用
Why my reorder is not working in geom_bar
我正在尝试使用 reorder
对我的 bar chart
进行排序,但是它不起作用。我得到的栏是 unsorted
格式。
我正在使用的代码,
ggplot(Top_SP_NB_Precison_Avg_long) +
geom_bar(aes(x = reorder(Top_Percision, -Top_Value), y = Top_Value, fill=Model_Name), position=position_dodge(width=1), stat="identity") +
geom_text(aes(Top_Percision, Top_Value, label=round(Top_Value, digits = 2), group=Model_Name), position=position_dodge(width=1), size=4)+
facet_grid( ~ Dataset_Number,
labeller = labeller(Dataset_Number = function(x)paste0("Test Dataset ",x))) +
ggtitle("Percentage of Top 1, Top 2, and Top 5 for (Dataset 1: Disjoint and Dataset 2: Joint)") +
theme(plot.title = element_text(hjust = 0.5))
我得到的情节
我想sort
基于Top_Values
的条形图。使用 reorder
后,我的图中得到 reorder(Top_Percision, -Top_Value)
,但我只想显示 Top_Precision
。此外,我想在栏中添加 Model_Name and Top_Precision
(水平)`。因为值被一个接一个地覆盖。
可重现代码:
structure(list(Model_Name = c("AA", "AA", "CN", "CN", "Dice",
"Dice", "JAC", "JAC", "L3", "L3", "RA", "RA", "SP_2", "SP_2",
"SP_3", "SP_3", "AA", "AA", "CN", "CN", "Dice", "Dice", "JAC",
"JAC", "L3", "L3", "RA", "RA", "SP_2", "SP_2", "SP_3", "SP_3",
"AA", "AA", "CN", "CN", "Dice", "Dice", "JAC", "JAC", "L3", "L3",
"RA", "RA", "SP_2", "SP_2", "SP_3", "SP_3"), Dataset_Number = structure(c(1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("dataset_1",
"dataset_2"), class = "factor"), Top_1_Number_1 = c(968.2, 1103.4,
968.2, 1103.4, 968.2, 1103.4, 968.2, 1103.4, 968.2, 1103.4, 968.2,
1103.4, 49.7333333333333, 168.16577540107, 23.1891891891892,
278.5, 968.2, 1103.4, 968.2, 1103.4, 968.2, 1103.4, 968.2, 1103.4,
968.2, 1103.4, 968.2, 1103.4, 49.7333333333333, 168.16577540107,
23.1891891891892, 278.5, 968.2, 1103.4, 968.2, 1103.4, 968.2,
1103.4, 968.2, 1103.4, 968.2, 1103.4, 968.2, 1103.4, 49.7333333333333,
168.16577540107, 23.1891891891892, 278.5), Top_2_Number_1 = c(1936.6,
2207.4, 1936.6, 2207.4, 1936.6, 2207.4, 1936.6, 2207.4, 1936.6,
2207.4, 1936.6, 2207.4, 100.144444444444, 336.540106951872, 47,
557.7, 1936.6, 2207.4, 1936.6, 2207.4, 1936.6, 2207.4, 1936.6,
2207.4, 1936.6, 2207.4, 1936.6, 2207.4, 100.144444444444, 336.540106951872,
47, 557.7, 1936.6, 2207.4, 1936.6, 2207.4, 1936.6, 2207.4, 1936.6,
2207.4, 1936.6, 2207.4, 1936.6, 2207.4, 100.144444444444, 336.540106951872,
47, 557.7), Top_5_Number_1 = c(4841.9, 5519.6, 4841.9, 5519.6,
4841.9, 5519.6, 4841.9, 5519.6, 4841.9, 5519.6, 4841.9, 5519.6,
251.122222222222, 841.545454545455, 118.175675675676, 1394.8,
4841.9, 5519.6, 4841.9, 5519.6, 4841.9, 5519.6, 4841.9, 5519.6,
4841.9, 5519.6, 4841.9, 5519.6, 251.122222222222, 841.545454545455,
118.175675675676, 1394.8, 4841.9, 5519.6, 4841.9, 5519.6, 4841.9,
5519.6, 4841.9, 5519.6, 4841.9, 5519.6, 4841.9, 5519.6, 251.122222222222,
841.545454545455, 118.175675675676, 1394.8), Top_Percision = c("Top_1",
"Top_1", "Top_1", "Top_1", "Top_1", "Top_1", "Top_1", "Top_1",
"Top_1", "Top_1", "Top_1", "Top_1", "Top_1", "Top_1", "Top_1",
"Top_1", "Top_2", "Top_2", "Top_2", "Top_2", "Top_2", "Top_2",
"Top_2", "Top_2", "Top_2", "Top_2", "Top_2", "Top_2", "Top_2",
"Top_2", "Top_2", "Top_2", "Top_5", "Top_5", "Top_5", "Top_5",
"Top_5", "Top_5", "Top_5", "Top_5", "Top_5", "Top_5", "Top_5",
"Top_5", "Top_5", "Top_5", "Top_5", "Top_5"), Top_Value = c(0.562825438620055,
0.666442239087732, 0.527325534968645, 0.646535342402335, 0.395479141226974,
0.594138479126739, 0.395479141226974, 0.594138479126739, 0.575402174648061,
0.313552087504645, 0.636320734135852, 0.70882815479768, 0.836930861233382,
0.225230231229633, 0.0511163337250294, 0.00110342848845767, 0.356378424218613,
0.368592532979545, 0.331746900931854, 0.35800341437544, 0.309550233556993,
0.350144576861987, 0.309550233556993, 0.350144576861987, 0.416685970371373,
0.392537436373919, 0.395005999891646, 0.38587835397129, 0.604172142037466,
0.151509806996773, 0.0325980177679993, 0.00145325630169788, 0.174284724341723,
0.159738121683694, 0.171757392425061, 0.157253225028994, 0.173476420027052,
0.159230629844388, 0.173476420027052, 0.159230629844388, 0.172942924490357,
0.15904978122944, 0.174328632135356, 0.159738121683694, 0.395331576314268,
0.0906560167264891, 0.0225437625433477, 0.00086710964578538)), row.names = c(NA,
-48L), groups = structure(list(Model_Name = c("AA", "CN", "Dice",
"JAC", "L3", "RA", "SP_2", "SP_3"), .rows = structure(list(c(1L,
2L, 17L, 18L, 33L, 34L), c(3L, 4L, 19L, 20L, 35L, 36L), c(5L,
6L, 21L, 22L, 37L, 38L), c(7L, 8L, 23L, 24L, 39L, 40L), c(9L,
10L, 25L, 26L, 41L, 42L), c(11L, 12L, 27L, 28L, 43L, 44L), c(13L,
14L, 29L, 30L, 45L, 46L), c(15L, 16L, 31L, 32L, 47L, 48L)), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -8L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
以这种方式使用的 reorder()
函数会立即查看整个数据集,而我怀疑您想在每个方面的 x 位置内重新排序模型名称。为此,您可以使用带有分组变量的 interaction()
函数来快速形成子组。
df <- transform(
Top_SP_NB_Precison_Avg_long,
group = reorder(interaction(Top_Percision, Model_Name, Dataset_Number),
-Top_Value)
)
您在绘图时必须进行的唯一调整是在每个几何中正确设置组。另外,我还以为你是想说 'vertically' 而不是 'horizontally' 来表示文本方向,因为我不知道如何让文本水平放置会导致标签更清晰。
library(ggplot2)
ggplot(df) +
geom_col(
aes(x = Top_Percision,
y = Top_Value,
fill= Model_Name,
group = group),
position=position_dodge(width=1)
) +
geom_text(
aes(Top_Percision,
Top_Value, label=paste0(Model_Name, ": ", round(Top_Value, digits = 2)),
group= group),
position=position_dodge(width=1), size=3, angle = 90, hjust = 1
)+
facet_grid( ~ Dataset_Number,
labeller = labeller(Dataset_Number = function(x)paste0("Test Dataset ",x))) +
ggtitle("Percentage of Top 1, Top 2, and Top 5 for (Dataset 1: Disjoint and Dataset 2: Joint)") +
theme(plot.title = element_text(hjust = 0.5))
我正在尝试使用 reorder
对我的 bar chart
进行排序,但是它不起作用。我得到的栏是 unsorted
格式。
我正在使用的代码,
ggplot(Top_SP_NB_Precison_Avg_long) +
geom_bar(aes(x = reorder(Top_Percision, -Top_Value), y = Top_Value, fill=Model_Name), position=position_dodge(width=1), stat="identity") +
geom_text(aes(Top_Percision, Top_Value, label=round(Top_Value, digits = 2), group=Model_Name), position=position_dodge(width=1), size=4)+
facet_grid( ~ Dataset_Number,
labeller = labeller(Dataset_Number = function(x)paste0("Test Dataset ",x))) +
ggtitle("Percentage of Top 1, Top 2, and Top 5 for (Dataset 1: Disjoint and Dataset 2: Joint)") +
theme(plot.title = element_text(hjust = 0.5))
我得到的情节
我想sort
基于Top_Values
的条形图。使用 reorder
后,我的图中得到 reorder(Top_Percision, -Top_Value)
,但我只想显示 Top_Precision
。此外,我想在栏中添加 Model_Name and Top_Precision
(水平)`。因为值被一个接一个地覆盖。
可重现代码:
structure(list(Model_Name = c("AA", "AA", "CN", "CN", "Dice",
"Dice", "JAC", "JAC", "L3", "L3", "RA", "RA", "SP_2", "SP_2",
"SP_3", "SP_3", "AA", "AA", "CN", "CN", "Dice", "Dice", "JAC",
"JAC", "L3", "L3", "RA", "RA", "SP_2", "SP_2", "SP_3", "SP_3",
"AA", "AA", "CN", "CN", "Dice", "Dice", "JAC", "JAC", "L3", "L3",
"RA", "RA", "SP_2", "SP_2", "SP_3", "SP_3"), Dataset_Number = structure(c(1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("dataset_1",
"dataset_2"), class = "factor"), Top_1_Number_1 = c(968.2, 1103.4,
968.2, 1103.4, 968.2, 1103.4, 968.2, 1103.4, 968.2, 1103.4, 968.2,
1103.4, 49.7333333333333, 168.16577540107, 23.1891891891892,
278.5, 968.2, 1103.4, 968.2, 1103.4, 968.2, 1103.4, 968.2, 1103.4,
968.2, 1103.4, 968.2, 1103.4, 49.7333333333333, 168.16577540107,
23.1891891891892, 278.5, 968.2, 1103.4, 968.2, 1103.4, 968.2,
1103.4, 968.2, 1103.4, 968.2, 1103.4, 968.2, 1103.4, 49.7333333333333,
168.16577540107, 23.1891891891892, 278.5), Top_2_Number_1 = c(1936.6,
2207.4, 1936.6, 2207.4, 1936.6, 2207.4, 1936.6, 2207.4, 1936.6,
2207.4, 1936.6, 2207.4, 100.144444444444, 336.540106951872, 47,
557.7, 1936.6, 2207.4, 1936.6, 2207.4, 1936.6, 2207.4, 1936.6,
2207.4, 1936.6, 2207.4, 1936.6, 2207.4, 100.144444444444, 336.540106951872,
47, 557.7, 1936.6, 2207.4, 1936.6, 2207.4, 1936.6, 2207.4, 1936.6,
2207.4, 1936.6, 2207.4, 1936.6, 2207.4, 100.144444444444, 336.540106951872,
47, 557.7), Top_5_Number_1 = c(4841.9, 5519.6, 4841.9, 5519.6,
4841.9, 5519.6, 4841.9, 5519.6, 4841.9, 5519.6, 4841.9, 5519.6,
251.122222222222, 841.545454545455, 118.175675675676, 1394.8,
4841.9, 5519.6, 4841.9, 5519.6, 4841.9, 5519.6, 4841.9, 5519.6,
4841.9, 5519.6, 4841.9, 5519.6, 251.122222222222, 841.545454545455,
118.175675675676, 1394.8, 4841.9, 5519.6, 4841.9, 5519.6, 4841.9,
5519.6, 4841.9, 5519.6, 4841.9, 5519.6, 4841.9, 5519.6, 251.122222222222,
841.545454545455, 118.175675675676, 1394.8), Top_Percision = c("Top_1",
"Top_1", "Top_1", "Top_1", "Top_1", "Top_1", "Top_1", "Top_1",
"Top_1", "Top_1", "Top_1", "Top_1", "Top_1", "Top_1", "Top_1",
"Top_1", "Top_2", "Top_2", "Top_2", "Top_2", "Top_2", "Top_2",
"Top_2", "Top_2", "Top_2", "Top_2", "Top_2", "Top_2", "Top_2",
"Top_2", "Top_2", "Top_2", "Top_5", "Top_5", "Top_5", "Top_5",
"Top_5", "Top_5", "Top_5", "Top_5", "Top_5", "Top_5", "Top_5",
"Top_5", "Top_5", "Top_5", "Top_5", "Top_5"), Top_Value = c(0.562825438620055,
0.666442239087732, 0.527325534968645, 0.646535342402335, 0.395479141226974,
0.594138479126739, 0.395479141226974, 0.594138479126739, 0.575402174648061,
0.313552087504645, 0.636320734135852, 0.70882815479768, 0.836930861233382,
0.225230231229633, 0.0511163337250294, 0.00110342848845767, 0.356378424218613,
0.368592532979545, 0.331746900931854, 0.35800341437544, 0.309550233556993,
0.350144576861987, 0.309550233556993, 0.350144576861987, 0.416685970371373,
0.392537436373919, 0.395005999891646, 0.38587835397129, 0.604172142037466,
0.151509806996773, 0.0325980177679993, 0.00145325630169788, 0.174284724341723,
0.159738121683694, 0.171757392425061, 0.157253225028994, 0.173476420027052,
0.159230629844388, 0.173476420027052, 0.159230629844388, 0.172942924490357,
0.15904978122944, 0.174328632135356, 0.159738121683694, 0.395331576314268,
0.0906560167264891, 0.0225437625433477, 0.00086710964578538)), row.names = c(NA,
-48L), groups = structure(list(Model_Name = c("AA", "CN", "Dice",
"JAC", "L3", "RA", "SP_2", "SP_3"), .rows = structure(list(c(1L,
2L, 17L, 18L, 33L, 34L), c(3L, 4L, 19L, 20L, 35L, 36L), c(5L,
6L, 21L, 22L, 37L, 38L), c(7L, 8L, 23L, 24L, 39L, 40L), c(9L,
10L, 25L, 26L, 41L, 42L), c(11L, 12L, 27L, 28L, 43L, 44L), c(13L,
14L, 29L, 30L, 45L, 46L), c(15L, 16L, 31L, 32L, 47L, 48L)), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -8L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
以这种方式使用的 reorder()
函数会立即查看整个数据集,而我怀疑您想在每个方面的 x 位置内重新排序模型名称。为此,您可以使用带有分组变量的 interaction()
函数来快速形成子组。
df <- transform(
Top_SP_NB_Precison_Avg_long,
group = reorder(interaction(Top_Percision, Model_Name, Dataset_Number),
-Top_Value)
)
您在绘图时必须进行的唯一调整是在每个几何中正确设置组。另外,我还以为你是想说 'vertically' 而不是 'horizontally' 来表示文本方向,因为我不知道如何让文本水平放置会导致标签更清晰。
library(ggplot2)
ggplot(df) +
geom_col(
aes(x = Top_Percision,
y = Top_Value,
fill= Model_Name,
group = group),
position=position_dodge(width=1)
) +
geom_text(
aes(Top_Percision,
Top_Value, label=paste0(Model_Name, ": ", round(Top_Value, digits = 2)),
group= group),
position=position_dodge(width=1), size=3, angle = 90, hjust = 1
)+
facet_grid( ~ Dataset_Number,
labeller = labeller(Dataset_Number = function(x)paste0("Test Dataset ",x))) +
ggtitle("Percentage of Top 1, Top 2, and Top 5 for (Dataset 1: Disjoint and Dataset 2: Joint)") +
theme(plot.title = element_text(hjust = 0.5))