Y-axis variables on R ggplot graph

Viewed 39

Below please find the data and code. I was wondering how can I get the following graph from my data. I don't have all the levels on the y-axis.

library(ggplot2)
data <- read.table(text = "br tr ac met
L FA   73 OLD
L FA   70 NEW
Y FA   50 OLD
Y FA   57 NEW
L DL   17 OLD
L DL   7 NEW
Y DL   29 OLD
Y DL   23 NEW
L GA   57 OLD
L GA   51 NEW
Y GA   75 OLD
Y GA   87 NEW", header = TRUE)

ggplot(data = data, aes(x = br, y = ac, fill = met)) +
geom_bar(stat = "identity", width = 1) +
facet_wrap(~tr, strip.position = "bottom", scales = "free_x") +
theme(panel.spacing = unit(0, "lines"),
     strip.background = element_blank(),
     strip.placement = "outside")

enter image description here

2 Answers

First swap variables tr and br in your code and add position = "dodge" with smaller width like this:

library(ggplot2)
library(ggthemes)
data <- read.table(text = "br tr ac met
L FA   73 OLD
L FA   70 NEW
Y FA   50 OLD
Y FA   57 NEW
L DL   17 OLD
L DL   7 NEW
Y DL   29 OLD
Y DL   23 NEW
L GA   57 OLD
L GA   51 NEW
Y GA   75 OLD
Y GA   87 NEW", header = TRUE)

ggplot(data = data, aes(x = tr, y = ac, fill = met)) +
  geom_bar(stat = "identity", position = "dodge", width = 0.9) +
  facet_wrap(~br, strip.position = "bottom", scales = "free_x") +
  theme(panel.spacing = unit(0, "lines"),
        strip.background = element_blank(),
        strip.placement = "outside") +
  theme_excel_new()

Created on 2022-07-28 by the reprex package (v2.0.1)

Please note: you can remove the theme_excel_new theme if you want.

In addition to @Quinten solution:

library(ggplot2)
ggplot(data = data, aes(x = tr, y = ac, fill = met)) +
  geom_col(width = 0.5, position = position_dodge(0.7))+
  scale_fill_manual(name = "", values =  c("#D55E00", "#56B4E9"))+
  facet_wrap(~br, strip.position = "bottom", scales = "free_x") +
  xlab("")+
  ylab("")+
  scale_y_continuous(breaks = seq(0, 100, 10))+
  theme_grey(base_size = 18) +
  theme(panel.grid.major.y = element_line(color = "grey",
                                          size = 1,
                                          linetype = 1),
        panel.spacing = unit(0, "lines"),
        panel.background = element_blank(),
        strip.background = element_blank(),
        strip.placement = "outside",
        legend.position="bottom") 

enter image description here

Related