Equal scale for x and y axis in a ggplot plot

I have some ggplot plots in the Plotly function.
I would like the scale of x-axis and y-axis would be equal.
I used coord_fixed(below line), but it didn't work
coord_fixed(ratio = 1, xlim = NULL, ylim = NULL, expand = TRUE, clip = "on")
my desired output is like the attached figure , I need the distance between numbers on both axes would be equal, for example, 0.5 in the mentioned image.
Do you have any recommendations?
I need to mention, that I have a lot of input. the data is different for each of them, so the start number for all of them would be different.

output$fancyPlot <- renderPlotly({
   plot1 = ggplot(N, aes(x = Group1, y = Group2))+
              labs(title= input$TF_query1,
                   x= input$celltype1, y = input$celltype2)+
              geom_point(color = 'tomato')
            plot1 = plot1 + coord_fixed(ratio = 1, xlim = NULL, ylim = NULL, expand = TRUE, clip = "on")

})

Make sure the modified plot is actually returned in the end:

output$fancyPlot <- renderPlotly({
      plot1 = ggplot(N, aes(x = Group1, y = Group2)) +
                 labs(title= input$TF_query1,
                      x= input$celltype1, y = input$celltype2)+
        geom_point(color = 'tomato')
      
      plot1 <- plot1 +
        coord_fixed(ratio = 1, xlim = NULL, ylim = NULL, expand = TRUE, clip = "on")

    # return it!
      plot1
})

Or alternatively should work directly if you don't save it:

output$fancyPlot <- renderPlotly({
   ggplot(N, aes(x = Group1, y = Group2)) +
          labs(title= input$TF_query1,
           x= input$celltype1, y = input$celltype2) +
       geom_point(color = 'tomato') +
       coord_fixed(ratio = 1, xlim = NULL, ylim = NULL, expand = TRUE, clip = "on")
})

There is also something weird in your final )} which should be }).

@alexisw
thank you for your answer, yes I did, I wrote only a part of my code here.
But the mentioned solution didn't work so I changed it a bit.

              xy.limits <- range( c(x,y) )
              plot1 = plot1 +
                scale_x_continuous(limits=xy.limits) + 
                scale_y_continuous(limits=xy.limits) + 
                coord_fixed( ratio=1)
```
now it works.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.