I'm using ggplot2 in R to create a dotplot where the x-axis is the variable name and the y-axis is the cell line ID. The plot is faceted by a Group variable ("Foci formation", "Soft agar", "3D Matrigel growth").
I want to rotate or flip the entire plot horizontally, so that:
What is currently on the left becomes the right (and vice versa).
The facet labels (currently on the left) move to the right side.
The ID labels (currently on the right) move to the left side.
In short, I want a mirror image of the current plot across the vertical axis, like a 180-degree rotation along the y-axis.
Here's the code and data I'm using:
library(dplyr)
library(tidyverse)
library(ggplot2)
df1 <- structure(list(ID = c("OVCAR3", "OVCR", "AFC", "OVCAR3", "OVCR", "AFC",
"OVCAR3", "OVCR", "AFC"),
V = c(1.467, 1.402, 1.445, 1.48, 1.313, 1.418, 1.5, 1.456, 1.489),
N = c(0.823, 0.762, 0.34, 0.738, 0.739, 0.533, 0.891, 0.904, 0.412),
A = c(0.734, 0.771, 1.098, 0.793, 0.799, 0.938, 1.12, 0.853, 1.076),
`N+A` = c(-1.075, -0.577, -0.832, -1.025, -0.633, -0.977, -1.21, -0.517, -1.032),
C = c(-0.239, 0.342, 0.33, -0.314, 0.341, 0.202, -0.324, 0.303, 0.207),
`N+C` = c(-1.403, -1.002, -1.162, -1.5, -1.106, -1.22, -1.329, -1.043, -1.164),
T = c(0.113, 0.487, 0.393, -0.105, 0.336, 0.366, 0.089, 0.329, 0.275),
`N+T` = c(-1.25, -1.045, -1.066, -1.206, -0.981, -0.928, -1.321, -1.12, -1.032),
Group = c("Foci formation", "Foci formation", "Foci formation",
"Soft agar", "Soft agar", "Soft agar",
"3D Matrigel growth", "3D Matrigel growth", "3D Matrigel growth")),
class = "data.frame", row.names = c(NA, -9L))
# Convert to long format
df_long <- df1 %>%
pivot_longer(cols = V:`N+T`, names_to = "Variable", values_to = "Z_score")
# Create dot plot
ggplot(df_long, aes(x = Variable, y = ID, color = Z_score, size = abs(Z_score))) +
geom_point() +
facet_grid(rows = vars(Group), scales = "free_y", space = "free_y") +
scale_color_gradient2(low = "blue", mid = "white", high = "red", midpoint = 0) +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
strip.text.y = element_text(angle = 0)
) +
labs(x = "Variables", y = "Cell Line", color = "Z-Score", size = "Magnitude")
output:
You can try as below:
# Flip ID order to mirror horizontally
df_long$ID <- factor(df_long$ID, levels = rev(unique(df_long$ID)))
# Create mirrored plot
ggplot(df_long, aes(x = Variable, y = ID, color = Z_score, size = abs(Z_score))) +
geom_point() +
facet_grid(rows = vars(Group), scales = "free_y", space = "free_y", switch = "y") +
scale_color_gradient2(low = "blue", mid = "white", high = "red", midpoint = 0) +
scale_y_discrete(position = "right") + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1),strip.placement = "outside", strip.text.y.right = element_text(angle = 0), strip.text.y.left = element_blank(), strip.background = element_blank() ) + labs(x = "Variables", y = "Cell Line", color = "Z-Score", size = "Magnitude")