Set R Plots X Axis to Show at Y=0

Set R plots x axis to show at y=0

You probably want the graphical parameters xaxs and yaxs with style "i":

plot(1:10, rnorm(10), ylim=c(0,10), yaxs="i")

See ?par:

xaxs: The style of axis interval
calculation to be used for the x-axis.
Possible values are "r", "i", "e",
"s", "d". The styles are generally
controlled by the range of data or
xlim, if given. Style "r" (regular)
first extends the data range by 4
percent at each end and then finds an
axis with pretty labels that fits
within the extended range. Style "i"
(internal) just finds an axis with
pretty labels that fits within the
original data range. Style "s"
(standard) finds an axis with pretty
labels within which the original data
range fits. Style "e" (extended) is
like style "s", except that it is also
ensures that there is room for
plotting symbols within the bounding
box. Style "d" (direct) specifies that
the current axis should be used on
subsequent plots. (Only "r" and "i"
styles are currently implemented)

yaxs: The style of axis interval calculation to be used for the y-axis.
See xaxs above.

How to align the x-axis on Y=0 in R?

By default, R extends the axes by 4% on either end around the limits: from ?par,

‘xaxs’ The style of axis interval calculation to be used for the
x-axis. Possible values are ‘"r"’, ‘"i"’, ‘"e"’, ‘"s"’,
‘"d"’. The styles are generally controlled by the range of
data or ‘xlim’, if given.
Style ‘"r"’ (regular) first extends the data range by 4
percent at each end and then finds an axis with pretty labels
that fits within the extended range.
Style ‘"i"’ (internal) just finds an axis with pretty labels
that fits within the original data range.

(yaxs does the same thing for the y-axis).

You can use

plot(mtcars$mpg, ylim=c(0,50), yaxs="i")

How to force R plots y axis to start at y=0 and keep the color?

The reason you get the gray line is that you are calling plot.density when you pass an object class density to plot. plot.density has a zero.line argument which is set to TRUE and plots the gray line using abline(h = 0, lwd = 0.1, col = "gray") by default (see stat:::plot.density for code). You need to set zero.line to FALSE.

plot(density(nums), yaxs="i", 
xlab="", ylab="", main="",
zero.line = FALSE)

Sample Image

You can control the upper ylim too if you want to give some more room at the top than yaxs = "i" would give otherwise. Of course, you still need zero.line = FALSE to not plot the gray zero line.

plot(density(nums), yaxs="i",
xlab="", ylab="", main="",
zero.line = FALSE,
ylim = c(0, 0.5)) # put whatever you want here instead 0.5

An alternative solution would be to cover the gray line with another line:

plot(density(nums), yaxs="i",
xlab="", ylab="", main="")
abline(h = 0)

ggplot with date-x-axis at y=0 and labels at the bottom

See my answer below

ggplot(coe_melt, aes(x=time, y=value, color=score))+
geom_hline(yintercept=0)+
geom_line(size=2)+
scale_color_manual(values=c('blue','magenta','red','green'),
breaks=c('Profitability', 'Growth', 'Safety','Payout'))+
theme_bw()+
theme(legend.position = 'bottom')+
theme(axis.ticks.x = element_blank())+

theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank() )+
theme(panel.border= element_blank())+
theme(axis.line.y = element_line(color="black", size = 0.5))+
expand_limits(y=c(-0.4, 0.8))+
scale_y_continuous(breaks=seq(-0.4, 0.8, 0.2))

How to place the intercept of x and y axes at (0 , 0) and extend the x and y axes to the edge of the plot

By default, axis() computes automatically the tick marks position, but you can define them manually with the at argument. So a workaround could be something like :

curve(x^2, -5, 5, axes=FALSE)
axis(1, pos=0, at=-5:5)
axis(2, pos=0)

Which gives :

Sample Image

The problem is that you have to manually determine the position of each tick mark. A slightly better solution would be to compute them with the axTicks function (the one used by default) but calling this one with a custom axp argument which allows you to specify respectively the minimum, maximum and number of intervals for the ticks in the axis :

curve(x^2, -5, 5, axes=FALSE)
axis(1, pos=0, at=axTicks(1,axp=c(-10,10,10)))
axis(2, pos=0)

Which gives :

Sample Image

set the x-axis to meet y-axis at 0 with lattice

Yes, yaxs doesn't work in lattice. See here for how it is handled.

However, I got this to work by skipping the prepanel and calculating the ylim directly in the barchart call.

library(lattice)
order=data.frame(Order=rep(paste0("order",1:4),times=2),
Area=rep(paste0("Area ",1:2),each=4),
Count=c(224122,2091,45867,32816,71548,309,22564,10686),
Stdev=c(37263,253,4450,4563,2046,25,315,987))

panel.err=function(x, y, subscripts, groups, Stdev, box.ratio, ...){
d <- 1/(nlevels(groups)+nlevels(groups)/box.ratio)
g <- (as.numeric(groups[subscripts])-1); g <- (g-median(g))*d
panel.arrows(as.numeric(x)+g,y-Stdev[subscripts], as.numeric(x)+g, y+Stdev[subscripts],
code=3,angle=90, length=0.025)
}

barchart(Count~Order,#tells it what x and y are for the plot
groups=Area,#tells it the subdivision in x
data=order,#tells it where to get the info from
Stdev=order$Stdev,
auto.key=list(points=FALSE,rectangles=TRUE,columns=2, title="Area",cex.title=1),#adds the key how to spread the legend and the title of it
main="Order per Area", #this create the title of the graph
#scales=list(x=list(rot=0))), #rotate the x-axis labels
par.settings=list(superpose.polygon=list(col=grey.colors(2))),
ylim=c(0,max(order$Count+order$Stdev)*1.04),
panel=function(x, y, subscripts, groups, Stdev, box.ratio, ...){
panel.barchart(x, y, subscripts=subscripts,
groups=groups, box.ratio=box.ratio, ...)
panel.err(x, y, subscripts=subscripts,
groups=groups, box.ratio=box.ratio, Stdev=order$Stdev)
}
)

I added 4% to the top limit so the error bar doesn't fall on the bounding box. I used zero for the lower limit rather than some other minimum value in the data since that is more appropriate for bar charts.

Force the origin to start at 0

xlim and ylim don't cut it here. You need to use expand_limits, scale_x_continuous, and scale_y_continuous. Try:

df <- data.frame(x = 1:5, y = 1:5)
p <- ggplot(df, aes(x, y)) + geom_point()
p <- p + expand_limits(x = 0, y = 0)
p # not what you are looking for

Sample Image

p + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0))

Sample Image

You may need to adjust things a little to make sure points are not getting cut off (see, for example, the point at x = 5 and y = 5.

R studio set x axis by year values

Use xaxt="n' to disable the automatic axis labeling, and axis(.) to add your own.

plot(yval, type="l", col="red", xaxt="n")
axis(1, c(0,12,24,36,48,60), 2015:2020)

Sample Image

(BTW, I changed 32 to 36, I thought it would align better :-)

x axis not displaying correctly

If you want axes that are at x=0 and y=0, you can add them manually in the base graphics. Here is some example code. The location of text and tick marks might have to be modified.

Sample Image

eq = function(x)
{ a=(sin(5*x)+cos(7*x))^2
b= 5 * (1/sqrt(2*pi*0.05)) * exp(-x^2/(2*0.05))
1-a-b
}


# basic plot without axes
plot(y=eq(-10:10)
,x=c(-10:10)
,xaxt='n'
,yaxt='n'
,type='l'
,col='red'
,xlab=''
,ylab=''
)
# grid
grid()

# adding thicker horizontal and vertical lines at axis y=0, x=0
abline(h=0,lwd=2,col='black')
abline(v=0,lwd=2,col='black')

# adding text and ticks for x axis, must be modified based on plot
text(x=-0.7,y=seq(1,-8,-1)[-2],seq(1,-8,-1)[-2])
points(x=seq(-10,10,1)[-11],y=rep(0,20),pch='|')

# adding text and ticks for y axis, must be modified based on plot
text(x=c(seq(-10,10,1))[-11],y=-0.4,c(-10:10)[-11])
points(x=rep(0,9),y=seq(-8,1,1)[-9],pch='―')

# adding text for 0-0 point
text(x=-0.3,-0.2,0)

How to specify the actual x axis values to plot as x axis ticks in R

You'll find the answer to your question in the help page for ?axis.

Here is one of the help page examples, modified with your data:

Option 1: use xaxp to define the axis labels

plot(x,y, xaxt="n")
axis(1, xaxp=c(10, 200, 19), las=2)

Option 2: Use at and seq() to define the labels:

plot(x,y, xaxt="n")
axis(1, at = seq(10, 200, by = 10), las=2)

Both these options yield the same graphic:

Sample Image


PS. Since you have a large number of labels, you'll have to use additional arguments to get the text to fit in the plot. I use las to rotate the labels.



Related Topics



Leave a reply



Submit