0

I have code like this:

data <- read.csv("test2.csv", stringsAsFactors=FALSE, header=TRUE)


facet_plot <- ggplot(data, aes(x = specie, y = value, fill = specie)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values=c("#3288bd", "#d53e4f", "#fc8d59", "#fee08b")) +
  ylab("Performance (ns/day) on Tesla P100")+
  facet_grid(. ~ condition, switch="both") +
  theme(plot.title = element_text(hjust = 0.5), 
        axis.title.y = element_text(face="bold", colour="black", size = 12),
        legend.title = element_text(face="bold", size = 10),  
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.x = element_blank(), 
        axis.text.x=element_blank(),
        strip.background = element_rect(fill="grey", colour="black", size=1),
        strip.text = element_text(face="bold", size=rel(1.2)))

# Call facet plot:

facet_plot + guides(fill=guide_legend(title="GROMACS command")) + scale_y_continuous(expand = c(0,0), limits = c(0,460))

My data looks like this:

> dput(data)
structure(list(specie = c("gmx mdrun -s benchMEM.tpr -nsteps 10000", 
"gmx mdrun -s benchMEM.tpr -nsteps 10000", "gmx mdrun  -s MD_5NM_WATER.tpr -nsteps 10000 ", 
"gmx mdrun  -s MD_5NM_WATER.tpr -nsteps 10000 ", "gmx mdrun  -s MD_10NM_WATER.tpr -nsteps 10000 ", 
"gmx mdrun  -s MD_10NM_WATER.tpr -nsteps 10000 ", "gmx mdrun -s MD_15NM_WATER.tpr -nsteps 10000 ", 
"gmx mdrun -s MD_15NM_WATER.tpr -nsteps 10000 "), condition = c("CUDA", 
"SYCL", "CUDA", "SYCL", "CUDA", "SYCL", "CUDA", "SYCL"), value = c(54.682, 
77.649, 452.48, 158.277, 81.426, 80.454, 33.449, 34.363)), class = "data.frame", row.names = c(NA, 
-8L))

How to order bars in this order: MD_5NM_WATER, MD_10NM_WATER, MD_15NM_WATER, benchMEM?

enter image description here

Axeman
  • 32,068
  • 8
  • 81
  • 94
anikaM
  • 409
  • 1
  • 4
  • 9

1 Answers1

0

You may use x = reorder(specie, row) and fill = reorder(specie, row) as below

you need to derive a new row with case_when


specie_order <- function(x) {
  case_when(
  x=='gmx mdrun  -s MD_5NM_WATER.tpr -nsteps 10000' ~ 1,
  x=='gmx mdrun  -s MD_10NM_WATER.tpr -nsteps 10000' ~ 2,
  x=='gmx mdrun -s MD_15NM_WATER.tpr -nsteps 10000' ~ 3,
  x=='gmx mdrun -s benchMEM.tpr -nsteps 10000' ~ 4
)
}

data <- data %>% mutate(row=specie_order(trimws(specie)))

facet_plot <- ggplot(data, aes(x = reorder(specie, row), y = value, fill = reorder(specie, row))) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values=c("#3288bd", "#d53e4f", "#fc8d59", "#fee08b")) +
  ylab("Performance (ns/day) on Tesla P100")+
  facet_grid(. ~ condition, switch="both") +
  theme(plot.title = element_text(hjust = 0.5), 
        axis.title.y = element_text(face="bold", colour="black", size = 12),
        legend.title = element_text(face="bold", size = 10),  
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.x = element_blank(), 
        axis.text.x=element_blank(),
        strip.background = element_rect(fill="grey", colour="black", size=1),
        strip.text = element_text(face="bold", size=rel(1.2)))

# Call facet plot:

facet_plot + guides(fill=guide_legend(title="GROMACS command")) + scale_y_continuous(expand = c(0,0), limits = c(0,460))

enter image description here

jkatam
  • 2,691
  • 1
  • 4
  • 12
  • can names in legend be ordered to they follow ordering on the plot? – anikaM Jul 14 '23 at 21:59
  • sure, i updated my answer to order the bars as well as legend as per expected – jkatam Jul 14 '23 at 22:05
  • I me getting some errors here: > specie_order <- \(x) { Error: unexpected input in "specie_order <- \" > case_when( + x=='gmx mdrun -s MD_5NM_WATER.tpr -nsteps 10000' ~ 1, + x=='gmx mdrun -s MD_10NM_WATER.tpr -nsteps 10000' ~ 2, + x=='gmx mdrun -s MD_15NM_WATER.tpr -nsteps 10000' ~ 3, + x=='gmx mdrun -s benchMEM.tpr -nsteps 10000' ~ 4 + ) Error in eval_tidy(pair$lhs, env = default_env) : object 'x' not found > } Error: unexpected '}' in "}" – anikaM Jul 14 '23 at 22:08
  • strange I dont get it, may be I believe you are missing the `}` in the end of the `specie_order ` – jkatam Jul 14 '23 at 22:12
  • I am not missing that }, please double check – anikaM Jul 14 '23 at 22:22
  • I already posted the code and plot which worked for me, so i am not sure why you are getting the error – jkatam Jul 14 '23 at 22:24
  • > specie_order <- \(x) { Error: unexpected input in "specie_order <- \" > case_when( + x=='gmx mdrun -s MD_5NM_WATER.tpr -nsteps 10000' ~ 1, + x=='gmx mdrun -s MD_10NM_WATER.tpr -nsteps 10000' ~ 2, + x=='gmx mdrun -s MD_15NM_WATER.tpr -nsteps 10000' ~ 3, + x=='gmx mdrun -s benchMEM.tpr -nsteps 10000' ~ 4 + ) Error in eval_tidy(pair$lhs, env = default_env) : object 'x' not found > } Error: unexpected '}' in "}" > – anikaM Jul 14 '23 at 22:31
  • specie_order <- function(x) { case_when( x=='gmx mdrun -s MD_5NM_WATER.tpr -nsteps 10000' ~ 1, x=='gmx mdrun -s MD_10NM_WATER.tpr -nsteps 10000' ~ 2, x=='gmx mdrun -s MD_15NM_WATER.tpr -nsteps 10000' ~ 3, x=='gmx mdrun -s benchMEM.tpr -nsteps 10000' ~ 4 ) } – jkatam Jul 14 '23 at 22:33
  • please try this code and check – jkatam Jul 14 '23 at 22:33
  • 1
    I believe the issue was with the use of `\(x)` which refers to as a function, but it seems did not work, for you only `function(x)` worked, may be you are using a lower version of base r – jkatam Jul 14 '23 at 22:42