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
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.
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, ...)
%>% 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With