Search code examples
rggplot2

How do modulate the y-axis in R and GG-plot


All I have some code in R to make a graph.

# Install and load necessary packages
library(ggplot2)
library(tidyr)

# Your data
data <- read.csv("./mae_random.csv")

# Reshape data for ggplot
data_long$Model.Names <- factor(data_long$Model.Names, levels = unique(data$Model.Names))
# Define colors for each Model Name
model_colors <- c("#1f78b4", "#33a02c", "#e31a1c", "#ff7f00", "#6a3d9a",
                  "#a6cee3", "#b2df8a", "#fb9a99", "#fdbf6f", "#cab2d6", "#ffff99")

# Create grouped bar chart with increased size and custom colors
ggplot(data_long, aes(x = Model.Names, y = Value, fill = Model.Names)) +
  geom_bar(position = position_dodge2(width = 1.2, preserve = "single"), stat = "identity", color = "black") +
  labs(title = "Grouped Bar Chart",
       x = "Model Names",
       y = "Mean Absolute Error") +
  scale_y_continuous(breaks = seq(0, max(data_long$Value), by = 0.1)) +
  scale_fill_manual(values = model_colors) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 16, hjust = 0.5),
    axis.text.x = element_text(angle = 45, hjust = 1),
    axis.title.x = element_text(size = 14),
    axis.title.y = element_text(size = 14),
    axis.text.y = element_text(size = 12),
    legend.title = element_text(size = 12),
    legend.text = element_text(size = 10),
    plot.margin = margin(1, 1, 1, 1, "cm")
  )

This produces this kind of a graph:

enter image description here

However, I want a slight modification and I have no idea how to go about this:

enter image description here

Any idea how I could modify the code? Any help would be lovely. Oh also the CSV file looks like this:

Model Names,TPE,Random,Grid
SVM MACCS,0.36,0.41,0.39
SVM Morgan,0.27,0.28,0.27
RF MACCS,0.31,0.31,0.26
RF Morgan,0.24,0.24,0.24
XGBoost MACCS,0.4,0.36,0.32
XGBoost Morgan,0.37,0.42,0.34
FFNN MACCS,0.7,0.67,0.64
FFNN Morgan,0.41,0.4,0.46
GCN,0.72,0.7,0.63
DMPNN,0.63,0.63,0.59
MAT,0.56,0.65,0.66

Solution

  • I'm not sure exactly what you are wanting to change in your plot, but this seems to match the elements of the desired plot fairly well:

    library(tidyverse)
    
    data %>%
      mutate(`Model Names` = factor(`Model Names`, `Model Names`)) %>%
      pivot_longer(-1) %>%
      mutate(name = factor(name, c('TPE', 'Random', 'Grid'))) %>%
      ggplot(aes(name, value, color = `Model Names`)) +
      geom_col(width = 0.6, aes(fill = after_scale(alpha(color, 0.5))),
               linewidth = 0.6) +
      scale_x_discrete(NULL, expand = c(0.5, 0.1)) +
      scale_color_manual(values = c('blue', 'red', 'green3', 'purple',
                                    'orange', 'black', '#9d6a13', 'blue4',
                                    'purple4', 'red4', 'green4'),
                         name = NULL) +
      facet_grid(.~`Model Names`) +
      scale_y_continuous('Mean absolute Error', expand = c(0, 0),
                         limits = c(0, 1.2), breaks = seq(0, 1.2, 0.2)) +
      theme_classic() +
      theme(text = element_text(color = 'black', face = 2),
            axis.text = element_text(color = 'black'),
            panel.spacing.x = unit(0, 'mm'),
            axis.text.x = element_text(angle = 45, hjust = 1),
            strip.background = element_blank(),
            strip.text = element_blank())
    

    enter image description here


    Data used - taken from csv data in question

    data <- data.frame(`Model Names` = c("SVM MACCS", "SVM Morgan", "RF MACCS", 
                                 "RF Morgan", "XGBoost MACCS", "XGBoost Morgan", 
                                 "FFNN MACCS", "FFNN Morgan", "GCN", 
                                 "DMPNN", "MAT"), 
                   TPE = c(0.36, 0.27, 0.31, 0.24, 0.4, 0.37, 0.7, 0.41, 
                           0.72, 0.63, 0.56), 
                   Random = c(0.41, 0.28, 0.31, 0.24, 0.36, 0.42, 
                              0.67, 0.4, 0.7, 0.63, 0.65), 
                   Grid = c(0.39, 0.27, 0.26, 0.24, 0.32, 0.34, 
                            0.64, 0.46, 0.63, 0.59, 0.66), check.names = FALSE)