I am trying to sort my bar chart
using reorder
but, it is not working. I am getting the bar as unsorted
format.
The code I am using,
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))
The plot I am getting
I want to sort
the bar chart based on the Top_Values
. After using the reorder
I am getting reorder(Top_Percision, -Top_Value)
in my figure but I want to show only Top_Precision
. Moreover, I want to add the Model_Name and Top_Precision
in the bar (horizontally)`. Because the values are getting overwritten one after another.
Reproduciable Code:
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"))
The reorder()
function used in that way sees the entire dataset at once, whereas I suspect you want to reorder the model names within the x-position in every facet. To do this, you can use the interaction()
function with the grouping variables to quickly form subgroups.
df <- transform(
Top_SP_NB_Precison_Avg_long,
group = reorder(interaction(Top_Percision, Model_Name, Dataset_Number),
-Top_Value)
)
The only adjustment you'd have to make while plotting is setting the groups correctly in every geom. Also, I had assumed you meant to say 'vertically' instead of 'horizontally' for text orientation, as I couldn't figure out how leaving the text horizontal would lead to clearer labelling.
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))