0

Below is the desired order: 10% runoff, 20% runoff, NOEC, LC50. Thank you in anticipation.

My code:

ggplot(pest_ana, aes(x = Pesticides, y = `Concentration (ug/L)`, fill= Concentrations)) + 
  geom_bar(stat = 'identity', position='dodge') +  scale_fill_discrete (breaks = c("10% runoff","20% runoff","NOEC", "LC50"))+ scale_x_discrete(guide = guide_axis(n.dodge=2)) + scale_y_continuous(trans = log10_trans(), 
   breaks = trans_breaks("log10", function(x) 10^x),
    labels = trans_format("log10", math_format(10^.x)))

Pesticides  Concentrations                       Concentration (ug/L)     Individual
Cypermethrin       NOEC                               4.8                      1
Deltamethrin       NOEC                               3.37                     2
Actara             NOEC                               20000                    3
Carbofuran         NOEC                               40.6                     4
Methomyl           NOEC                               260                      5
Endosulfan         NOEC                               10.2                     6
Fenvalerate        NOEC                               6.02                     7
Glyphosate         NOEC                               5000                     8
Mancozeb           NOEC                               301                      9
Cypermethrin       LC50                               0.03                     1
Deltamethrin       LC50                               0.032                    2
Actara             LC50                               322000                   3
Carbofuran         LC50                               500                      4
Methomyl           LC50                               4015                     5
Endosulfan         LC50                               0.05                     6
Fenvalerate        LC50                               15                       7
Glyphosate         LC50                               36800                    8
Mancozeb           LC50                               11680                    9
Cypermethrin       20% runoff                         3.95                     1
Deltamethrin       20% runoff                         0.69                     2
Actara             20% runoff                         3.95                     3
Carbofuran         20% runoff                         78.99                    4
Methomyl           20% runoff                         10.86                    5
Endosulfan         20% runoff                         41.47                    6
Fenvalerate        20% runoff                         8.85                     7
Glyphosate         20% runoff                         14.22                    8
Mancozeb           20% runoff                         74.05                    9
Cypermethrin       10% runoff                         1.97                     1
Deltamethrin       10% runoff                         0.35                     2
Actara             10% runoff                         1.97                     3
Carbofuran         10% runoff                         39.49                    4
Methomyl           10% runoff                         5.43                     5
Endosulfan         10% runoff                         20.73                    6
Fenvalerate        10% runoff                         4.42                     7
Glyphosate         10%  runoff                        7.11                     8
Mancozeb           10% runoff                         37.03                    9
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • 1
    Does this answer your question? [Reorder bars in geom\_bar ggplot2](https://stackoverflow.com/questions/25664007/reorder-bars-in-geom-bar-ggplot2) – NelsonGon May 18 '20 at 10:47

1 Answers1

0

You can use the following code

library(scales)
library(tidyverse)

df$Concentrations <- factor(df$Concentrations, levels = c("10% runoff", "20% runoff", "NOEC", "LC50"))

df %>% 
  ggplot(aes(x = Pesticides, y = `Concentration (ug/L)`, fill= Concentrations)) + 
  geom_bar(stat = 'identity', position='dodge') +  
  scale_fill_discrete (breaks = c("10% runoff","20% runoff","NOEC", "LC50"))+ 
  scale_x_discrete(guide = guide_axis(n.dodge=2)) + 
  scale_y_continuous(trans = log10_trans(), 
  breaks = trans_breaks("log10", function(x) 10^x),
  labels = trans_format("log10", math_format(10^.x)))

enter image description here

Data

df = structure(list(Pesticides = structure(c(3L, 4L, 1L, 2L, 9L, 5L, 
6L, 7L, 8L, 3L, 4L, 1L, 2L, 9L, 5L, 6L, 7L, 8L, 3L, 4L, 1L, 2L, 
9L, 5L, 6L, 7L, 8L, 3L, 4L, 1L, 2L, 9L, 5L, 6L, 7L, 8L), .Label = c("Actara", 
"Carbofuran", "Cypermethrin", "Deltamethrin", "Endosulfan", "Fenvalerate", 
"Glyphosate", "Mancozeb", "Methomyl"), class = "factor"), Concentrations = structure(c(3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("10% runoff", "20% runoff", "NOEC", "LC50"
), class = "factor"), `Concentration (ug/L)` = c(4.8, 3.37, 20000, 
40.6, 260, 10.2, 6.02, 5000, 301, 0.03, 0.032, 322000, 500, 4015, 
0.05, 15, 36800, 11680, 3.95, 0.69, 3.95, 78.99, 10.86, 41.47, 
8.85, 14.22, 74.05, 1.97, 0.35, 1.97, 39.49, 5.43, 20.73, 4.42, 
7.11, 37.03), Individual = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L)), row.names = c(NA, 
-36L), class = "data.frame")
Community
  • 1
  • 1
UseR10085
  • 7,120
  • 3
  • 24
  • 54