Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing ylim (axis limits) drops data falling outside range. How can this be prevented?

Tags:

r

ggplot2

df <- data.frame(age=c(10,10,20,20,25,25,25),veg=c(0,1,0,1,1,0,1))
g=ggplot(data=df,aes(x=age,y=veg))
g=g+stat_summary(fun.y=mean,geom="point")

Points reflect mean of veg at each age, which is what I expected and want to preserve after changing axis limits with the command below.

g=g+ylim(0.2,1)

Changing axis limits with the above command unfortunately causes veg==0 subset to be dropped from the data, yielding

"Warning message: Removed 4 rows containing missing values (stat_summary)"

This is bad because now the data plot (stat_summary mean) omits the veg==0 points. How can this be prevented? I simply want to avoid showing the empty part of the plot- the ordinate from 0 to .2, but not drop the associated data from the stat_summary calculation.

like image 397
Alex Holcombe Avatar asked Apr 13 '10 00:04

Alex Holcombe


People also ask

How to Set the y axis limit in MATLAB?

Set y-Axis Limits for Specific AxesCall the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2 . Plot data into each axes. Then set the y-axis limits for the bottom plot by specifying ax2 as the first input argument to ylim .

What does XLIM () do while plotting?

matplotlib.pyplot.xlim() Function The xlim() function in pyplot module of matplotlib library is used to get or set the x-limits of the current axes.

What does the function XLIM () return?

xlim([xmin xmax]) sets the axis limits in the current axes to the specified values. xlim('mode') returns the current value of the axis limits mode, which can be either auto (the default) or manual .


1 Answers

you can do that by specifying the coord system:

df <- data.frame(age=c(10,10,20,20,25,25,25),veg=c(0,1,0,1,1,0,1))
g=ggplot(data=df,aes(x=age,y=veg))
g=g+stat_summary(fun.y=mean,geom="point")
g+coord_cartesian(ylim=c(0.2,1)) #do not use +ylim() here
like image 67
kohske Avatar answered Sep 19 '22 06:09

kohske