Home > Enterprise >  Y-axis variables on R ggplot graph
Y-axis variables on R ggplot graph

Time:07-29

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

CodePudding user response:

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 enter image description here

  • Related