Using the following dataframe:
df <- structure(list(Var1 = c("mpg", "disp", "hp", "drat", "wt", "qsec", "mpg", "disp", "hp", "drat", "wt", "qsec", "mpg", "disp", "hp", "drat", "wt", "qsec", "mpg", "disp", "hp", "drat", "wt", "qsec", "mpg", "disp", "hp", "drat", "wt", "qsec", "mpg", "disp", "hp", "drat", "wt", "qsec"), Var2 = c("mpg", "mpg", "mpg", "mpg", "mpg", "mpg", "disp", "disp", "disp", "disp", "disp", "disp", "hp", "hp", "hp", "hp", "hp", "hp", "drat", "drat", "drat", "drat", "drat", "drat", "wt", "wt", "wt", "wt", "wt", "wt", "qsec", "qsec", "qsec", "qsec", "qsec", "qsec"), value = c(1, -0.85, -0.78, 0.68, -0.87, 0.42, -0.85, 1, 0.79, -0.71, 0.89, -0.43, -0.78, 0.79, 1, -0.45, 0.66, -0.71, 0.68, -0.71, -0.45, 1, -0.71, 0.09, -0.87, 0.89, 0.66, -0.71, 1, -0.17, 0.42, -0.43, -0.71, 0.09, -0.17, 1), Var3 = c("A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "C", "D", "D", "D", "D", "D", "D", "E", "E", "E", "E", "E", "E", "F", "F", "F", "F", "F", "F")), class = "data.frame", row.names = c(NA, 36L))
I'm using the following ggplot2
code to create a simple geom_tile
heatmap. I have Var1
on the x-axis and Var2
on the y-axis.
ggplot(data = df, aes(x=Var1, y=Var2, fill=value)) +
geom_tile(color = "white") +
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Pearson\nCorrelation")+
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1)) +
coord_fixed()
I'd like to use Var3
to label the right hand side of the y-axis, whilst maintaining the Var2
labels on the left hand side. Note as these values are discrete, and Var2
variables always correspond with a particular letter in Var3
, I'm not looking to alter the ticks or scale of the y-axis on the right side, in relation to the left. The ticks will match on each side but the label for each tick will be different. I hope I have explained this clearly.
Any suggestions would be greatly appreciated.