Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding x=y line to hexplot in ggplot2

Hi I am trying to look at different ways to visualize a dataset by using hexplots in ggplot. I essentially want a hexplot with 1. loess line 2. regression line 3.x=y line --> equivalent of abline(0,1)

So far I have come up with this kind of code:

c <- ggplot(mtcars, aes(qsec, wt))
c+stat_binhex()+stat_smooth(method="loess", colour="red")+stat_smooth(method='lm', se=FALSE, colour="orange")+ geom_abline(intercept=0, slope=1)

This gives the picture below, but I still do not see the x=y reference line. Please help. I'm not sure why it is not working. Thanks

enter image description here

like image 396
CadisEtRama Avatar asked Dec 07 '13 01:12

CadisEtRama


People also ask

How do I add a horizontal line in ggplot2?

Example: To add the horizontal line on the plot, we simply add geom_hline() function to ggplot2() function and pass the yintercept, which basically has a location on the Y axis, where we actually want to create a vertical line.

How do I insert a horizontal line in R?

The R function abline() can be used to add vertical, horizontal or regression lines to a graph. A simplified format of the abline() function is : abline(a=NULL, b=NULL, h=NULL, v=NULL, ...)

What does %>% do in ggplot?

%>% is a pipe operator reexported from the magrittr package. Start by reading the vignette. Adding things to a ggplot changes the object that gets created. The print method of ggplot draws an appropriate plot depending upon the contents of the variable.


1 Answers

The y=x reference line is not inside the coordinate ranges you plot. If you change to

geom_abline(slope=1, intercept=-15)

you will see your line on the plot.

like image 190
topchef Avatar answered Oct 05 '22 03:10

topchef