Lieke
Lieke

Reputation: 149

Sorting bars of a barplot

My dataset looks as follows:

bioassay
# A tibble: 8 x 6
# Groups:   treatment.tx, date, combinedwith.tx [8]
  treatment.tx            date   combinedwith.tx   conc  mean_hatch stdev_hatch
  <chr>                   <fct>  <chr>             <fct>      <dbl>       <dbl>
1 8cG_.2mM_200527         200527 none              200         25.3        6.31
2 8cG_1mM_200527          200527 none              1000        57.7        8.25
3 8cG_1mM_atr5mM_200702   200702 atropine5mM       1000        30.8        6.45
4 8cG_1mM_dfd50uM_200702  200702 dafadine50uM      1000        23.6        6.27
5 8cG_1mM_ktc500uM_200702 200702 ketoconazole500uM 1000        14.1        9.76
6 8cG_1mM_LY500uM_200702  200702 LY500uM           1000        28.4        6.47
7 8cG_2.5mM_200527        200527 none              2500        57.7        8.13
8 8cG_5mM_200507          200507 none              5000        40.7        5.53

dput of the tibble:

structure(list(treatment.tx = c("8cG_.2mM_200527", "8cG_1mM_200527", 
"8cG_1mM_atr5mM_200702", "8cG_1mM_dfd50uM_200702", "8cG_1mM_ktc500uM_200702", 
"8cG_1mM_LY500uM_200702", "8cG_2.5mM_200527", "8cG_5mM_200507"
), date = structure(c(2L, 2L, 3L, 3L, 3L, 3L, 2L, 1L), .Label = c("200507", 
"200527", "200702"), class = "factor"), combinedwith.tx = c("none", 
"none", "atropine5mM", "dafadine50uM", "ketoconazole500uM", "LY500uM", 
"none", "none"), conc = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 3L, 
4L), .Label = c("200", "1000", "2500", "5000"), class = "factor"), 
    mean_hatch = c(25.2525252525253, 57.7160062183685, 30.7640235581412, 
    23.5513647506745, 14.1329043365367, 28.3725235338139, 57.6624355184606, 
    40.6641760066418), stdev_hatch = c(6.30807878626101, 8.2511325192673, 
    6.45000163857553, 6.26978502069754, 9.75908792638684, 6.47448596773916, 
    8.13374952479032, 5.52569421842133)), row.names = c(NA, -8L
), groups = structure(list(treatment.tx = c("8cG_.2mM_200527", 
"8cG_1mM_200527", "8cG_1mM_atr5mM_200702", "8cG_1mM_dfd50uM_200702", 
"8cG_1mM_ktc500uM_200702", "8cG_1mM_LY500uM_200702", "8cG_2.5mM_200527", 
"8cG_5mM_200507"), date = structure(c(2L, 2L, 3L, 3L, 3L, 3L, 
2L, 1L), .Label = c("200507", "200527", "200702"), class = "factor"), 
    combinedwith.tx = c("none", "none", "atropine5mM", "dafadine50uM", 
    "ketoconazole500uM", "LY500uM", "none", "none"), .rows = structure(list(
        1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), 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"))

I want to make a barplot that is ordered by date. I wrote the following:

bioassay$date <- factor(bioassay$date,levels=c("200507","200527","200702"))

But this does not order the tibble nor the bars in my barplot:

ggplot(data=bioassay, aes(fill=combinedwith.tx, x=treatment.tx, y=mean_hatch)) +
  geom_bar(stat='identity',
           width=0.5,
           alpha=0.5) +
  geom_errorbar(aes(ymin=mean_hatch-stdev_hatch, ymax=mean_hatch+stdev_hatch),
                width=0.2,
                size=0.5) +
  theme_light() +
  labs(fill = "combined with") +
  theme(text=element_text(size=15),
        axis.text.x=element_text(angle=45,
                                 hjust=1)) +
  scale_fill_manual(values=c("yellow2","darkgreen","red2","blue","grey0")) +
  scale_x_discrete(name = "combined with inhibitor") +
  scale_y_continuous(name = 'hatching (%)',
                     limits=c(0, 66),
                     breaks=c(-20,0,20,40,60,80,100)
                     )

gives the following:

this

whereas everywhere I find that the line of code described above should be the solution.

Upvotes: 0

Views: 82

Answers (1)

YBS
YBS

Reputation: 21287

Try this aes in your ggplot

aes(fill=combinedwith.tx, x=reorder(treatment.tx, dateorder), y=mean_hatch)

where dateorder is a new numeric variable in the order you like to display the bars.

Upvotes: 1

Related Questions