I have the following code but the y and x intercepts do not intersect, how can I get them to do so? #3 Simple Scatterplot DV VS Pred (pop pred) FOR D-METHYLPHN
Are you wanting the x and y axis to each begin at zero? If yes, you can control the x and y scales with the scale_x_continuous and scale_y_continuous functions. The limits argument let's you control the axis or scale values, and you can use NA to use an existing value.
I used the mtcars data set to demo using your sample code:
When I used the code I got an error: could not find function”scale_continuous” but it did produce a graph with a small off set of both zero values. The resulting graph did not have 0,0 zero intercept.
When I tried scale_y_continuous(limits=c(0,NA))+
scale_x_continuous(limits=c(0,NA)) I got the following error but it still produced a graph with the intercept having an off set and not being 0,0 which is what I want.
Sorry, typo in my example code -- my bad. The scale_continuous() should have been scale_y_continuous(). I think the plot was still created because R can be smart that way? If you type ?scale_x_continuous in your console you'll see the expand argument. If you set that to zeros it will force the 0 intercepts you are after. I hope this helps, and apologies for the typo!
You need the comma to separate the arguments to the function -- in the example single lines of code you pasted you have an extra bracket in the middle of the function and you are missing a closing bracket, that would be enough to generate your errors. R is plotting the object pda generated from the last run that worked. A bit if fiddling with syntax and you'll get the plot you want. Cheers.
I used the following code as suggested and there were no errors, however the plot still had the 0 values off set with x=0.0 while y=0 see attached graph:
#3 Simple Scatterplot DV VS Pred (pop pred) FOR D-METHYLPHN
I got another suggestion to use expand_limits (x=0,y=0) which I tried with the same result except there was no error message. I sent this response back to the individual for further clarification.
#3 Simple Scatterplot DV VS Pred (pop pred) FOR D-METHYLPHN
you do not have a + between the call of theme and the expand_limits call, which is on a separate line.
So the first two lines worked to create pda, but expand_limits was not added/applied to pda so would have made no difference (while not giving an error as it still ran correctly).
Ok. I'm guessing that what you are hoping to see is the x=0,y=0 point in the very corner of the plot?
In this case I think you need to go back to the suggestion by @stephhazlitt using scale_x_continuous and scale_y_continuous and get that running without errors.
However, with the latter approach you may need to provide upper limits too to avoid cutting/chopping/truncation of the extreme points (eg (5,5) in the example). I suspect some of your very low values which overlap the x=0 axis in you plot will be cut-off, so this may still not be satisfactory (maybe plot with smaller points? don't have 0,0 in the very corner?).
This answer is almost verbatim from this SO question
@jacksonan1, if the problem you are trying to solve is removing the small area to the left of x = 0 and below y = 0, then you should ask yourself whether this is really necessary.
Depending on the text size and/or dimensions you could end up with the problem of one of the 0 values not showing on the scales. When I first started with ggplot2 this is something I tried to force but soon realised it was not worth the effort. I came to appreciate the small additional area where there were data at 0 to avoid the points being chopped, as @ron indicated.