1

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.

tjebo
  • 21,977
  • 7
  • 58
  • 94
Darren
  • 277
  • 4
  • 17
  • 2
    This looks promising: https://stackoverflow.com/questions/45361904/duplicating-and-modifying-discrete-axis-in-ggplot2 – Kent Johnson Jan 08 '20 at 18:40
  • 1
    @Axeman I don't think this is a duplicate. If I understand correctly, the OP wishes to add ticks on the right and left side of the same y-axis, i.e. the one to the left, not to create two y-axis – tjebo Jan 08 '20 at 19:22
  • Hmm, that's not how I read it. Perhaps @Darren can help clarify. – Axeman Jan 08 '20 at 21:07
  • I also don't think this is a duplicate. The OP clearly states that they want different ticks straddling the same axis, rather than the axes on either side. – alan ocallaghan Jan 09 '20 at 11:58
  • Sorry for the confusion. @Axeman, I think, has the correct idea - I need two vertical ('y') axes at either end of the x-axis, with identical ticks positions but different labels. I have looked at, and tried, to implement the method the post suggested, however, mirroring (or creating two separate plots) using `geom_tile` cause clashes between the plots that I'm struggling to resolve. Is there no way around this without creating two plots? If so, should I create a new post for this? In my head, creating a second plot seems a convoluted way to label each end of the rows on a heat map?? – Darren Jan 09 '20 at 19:37
  • @Darren, I'm confused, the answer in the linked duplicate doesn't use two plots... – Axeman Jan 09 '20 at 19:40
  • @Axeman - Sorry, I misunderstood what the duplicate post was referring to - I thought it was duplicating the plot rather than the y-axis. Thanks everyone for comments - I will try and tinker with this a bit more. – Darren Jan 10 '20 at 09:39

0 Answers0