How to obtain y=0 and x=0 for a scatter plot

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

pda<- ggplot(newd, aes(x=PRED,y=DV )) +
geom_point() +geom_abline (intercept=0, slope=1,linetype=1,size=2) + theme(text=element_text(size=10))
print(pda)

pda +
xlab("Population Predictions (ng/ml)") +
ylab("Observed Concentration (ng/ml)" ) + theme_bw() + theme(text=element_text(size=10))

#SAVE SCATTER PLOT dv vs pred
ggsave("pda_ggsave.png")
print (pda)
dev.off

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:

pda <- ggplot(mtcars, aes(x=cyl, y=mpg)) +
  geom_point() +
  geom_abline(intercept=0, slope=1, linetype=1, size=2) +
  theme(text=element_text(size=10)) +
  xlab("Population Predictions (ng/ml)") +
  ylab("Observed Concentration (ng/ml)") + 
  theme_bw() + 
  theme(text=element_text(size=10)) +
  scale_continuous(limits = c(0, NA)) +
  scale_x_continuous(limits = c(0, NA))
plot(pda)

I hope that is what you were after? Cheers, Steph

1 Like

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.

Error in scale_y_continuous(limits = c(0, NA)) + scale_x_continuous(limits = c(0, :

non-numeric argument to binary operator

Thanks

I am including the type of graph that I got with the continuous code.

I was curious as to why I got the graph although I received an error message for both versions of the code?

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!

pda <- ggplot(mtcars, aes(x=cyl, y=mpg)) +
  geom_point() +
  geom_abline(intercept=0, slope=1, linetype=1, size=2) + theme(text=element_text(size=10)) +
  xlab("Population Predictions (ng/ml)") +
  ylab("Observed Concentration (ng/ml)") + 
  theme_bw() + 
  theme(text=element_text(size=10)) +
  scale_y_continuous(limits = c(0, NA), expand = c(0,0)) +
  scale_x_continuous(limits = c(0, NA), expand = c(0,0))
plot(pda)

I made the change but I got this error. Not sure what to do since I tried the code with and without the , and I get the same error.

Error: unexpected ',' in "scale_y_continuous(limits=c(0,NA)) ,"

scale_x_continuous(limits=c(0,NA)) expand=c(0,0)

Error: unexpected symbol in " scale_x_continuous(limits=c(0,NA)) expand"

If I run without the comma I get:

pda<- ggplot(newd, aes(x=PRED,y=DV )) +

  • geom_point() +geom_abline (intercept=0, slope=1,linetype=1,size=2) + theme(text=element_text(size=10))

scale_y_continuous(limits=c(0,NA)) expand=c(0,0) +

Error: unexpected symbol in "scale_y_continuous(limits=c(0,NA)) expand"

scale_x_continuous(limits=c(0,NA)) expand=c(0,0)

Error: unexpected symbol in " scale_x_continuous(limits=c(0,NA)) expand"

In both cases I get the same graph. It is late so I will look up the syntax tomorrow when I am more awake.

Thanks

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.

A more concise expression would be:
expand_limits(x = 0, y = 0)

This replaces the scale_x/y_continuous functions.

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

pda<- ggplot(newd, aes(x=PRED,y=DV )) +

geom_point() +geom_abline (intercept=0, slope=1,linetype=1,size=2) + theme(text=element_text(size=10))

expand_limits (x=0, y=0)

print(pda)

pda +

xlab("Population Predictions (ng/ml)") +

ylab("Observed Concentration (ng/ml)" ) + theme_bw() + theme(text=element_text(size=10))

#SAVE SCATTER PLOT dv vs pred

ggsave("pda_ggsave.png")

print (pda)

dev.off

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

pda<- ggplot(newd, aes(x=PRED,y=DV )) +

geom_point() +geom_abline (intercept=0, slope=1,linetype=1,size=2) + theme(text=element_text(size=10))

expand_limits (x=0, y=0)

print(pda)

pda +

xlab("Population Predictions (ng/ml)") +

ylab("Observed Concentration (ng/ml)" ) + theme_bw() + theme(text=element_text(size=10))

#SAVE SCATTER PLOT dv vs pred

ggsave("pda_ggsave.png")

print (pda)

dev.off

In the code you provided:

pda<- ggplot(newd, aes(x=PRED,y=DV )) +

geom_point() +geom_abline (intercept=0, slope=1,linetype=1,size=2) + theme(text=element_text(size=10))

expand_limits (x=0, y=0)

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).

Ron.

Ron:

This is my new code with the + as you suggest. However, I get the same results with no error but the offset 0 values-see attached.

Thanks

pda<- ggplot(newd, aes(x=PRED,y=DV )) +

geom_point() +geom_abline (intercept=0, slope=1,linetype=1,size=2) + theme(text=element_text(size=10)) +

expand_limits (x=0, y=0)

print(pda)

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.

library(ggplot2)
DF<-data.frame(x=1:5,y=1:5)
ggplot(DF)+geom_point(aes(x=x,y=y)) # unsatisfactory
ggplot(DF)+geom_point(aes(x=x,y=y))+expand_limits(x=0,y=0) # unsatisfactory?
ggplot(DF)+geom_point(aes(x=x,y=y))+
           scale_x_continuous(limits=c(0,NA),expand=c(0,0))+
           scale_y_continuous(limits=c(0,NA),expand=c(0,0)) # whoopee?

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

Are there spaces in your functions? e.g. expand_limits (x=0, y=0) should be expand_limits(x=0, y=0)

@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.

This code works and gives a 0,0 axis that I want.

#3 Simple Scatterplot DV VS Pred (pop pred) FOR D-METHYLPHN

pda<- ggplot(newd, aes(x=PRED,y=DV )) +

geom_point() +geom_abline (intercept=0, slope=1,linetype=1,size=2) + theme(text=element_text(size=10)) +

scale_x_continuous (limits=c(0,NA), expand=c(0,0)) +

scale_y_continuous(limits=c(0,NA), expand=c(0,0))

print(pda)

1 Like

Your last suggestion works fine and gives me the desired plot.

Thanks

The following code as suggested by a responder works.

#3 Simple Scatterplot DV VS Pred (pop pred) FOR D-METHYLPHN

pda<- ggplot(newd, aes(x=PRED,y=DV )) +

geom_point() +geom_abline (intercept=0, slope=1,linetype=1,size=2) + theme(text=element_text(size=10)) +

scale_x_continuous (limits=c(0,NA), expand=c(0,0)) +

scale_y_continuous(limits=c(0,NA), expand=c(0,0))

print(pda)

A post was split to a new topic: Read.table with no header