How to align the x-axis on Y=0 in R? [duplicate] - r

This question already has answers here:
Set R plots x axis to show at y=0
(2 answers)
Closed 3 years ago.
I observed that the x axes of a plot doesn't cross the y axes at 0.
Why?
How can I fix that?
Example:
plot(mtcars$mpg, ylim=c(0,50))

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

Related

Strange x-axis in histogram for discrete variable in R

I have some discrete data that I'm trying to plot in a histogram in R. I'm using the built in hist() function, which works fine most of the times for the data I have. However, when it comes to a discrete variable it looks somewhat strange (unfortunately I cannot add the picture). I interpret it as "since the bin for 0 and 1 children must fit between 0 and 1 it determines the width of all bins and thus the "from 1.5 to 2" result". How can I put the numbers on the x-axis centered underneath each bin instead?
Thanks in advance!
You might want to consider drawing the axis in a second step:
Prevent x-axis with xaxt="n":
hist(cars$speed, xaxt="n")
Draw x-axis and adjust label position with hadj=:
axis(1, at=seq(0,25,5), hadj=2.5, labels = c("",seq(5,25,5)))

How to create a break in the y-axis (a broken axis) in ggplot? [duplicate]

This question already has answers here:
Using ggplot2, can I insert a break in the axis?
(10 answers)
Closed 4 years ago.
I need to create a break in the y-axis, so I can show very low values, skip 90% of the y-axis and then show the very high values. How would I do this in r with ggplot?
Code is not needed, this is more of a conceptual question. I want something like below.
The functions scale_x_discrete() and scale_y_discrete() are used to customize discrete x and y axis, respectively. Just define your own breaks.
scale_x_discrete(name, breaks, labels, limits)
scale_y_discrete(name, breaks, labels, limits)
name : x or y axis labels
breaks : control the breaks in the guide
(axis ticks, grid lines, …). Among the possible values, there are :
NULL : hide all breaks waiver() : the default break computation a character or numeric vector specifying which breaks
to display
labels : labels of axis tick marks. Allowed values are : NULL for no labels waiver() for the default labels character
vector to be used for break labels
limits : a character vector indicating the data
range
Reference this link

How to change the Scale of Y-Axis in R [duplicate]

This question already has answers here:
R Language: How to Set ylim?
(2 answers)
Closed 4 years ago.
I have this graph which I'm plotting
As you can see, the values on the black line which I'm trying to plot is not on the same scale as that of the red line.
Therefore, I'm trying to change the scale of the Y-Axis, so I can essentially "Zoom Out". How do I do this?
What I did till now is:
plot(d,vol1, type="l",xaxt="n", xlab="Date", ylab="Volatility Estimate", main="Nasdaq Pharmaceutical Index")
months= seq(min(d), max(d), "month")
axis(1, months, format(months, "%Y\n%b"))
lines(d, vol1, col="black")
You can add the argument ylim=c(a,b) inside the plot() command, where a is the minimum and b is the maximum of your desired y-axis.

How to make a scatter-plot if I have more than one treatment? [duplicate]

This question already has answers here:
How to create a line plot with groups in Base R without loops?
(3 answers)
Closed 9 years ago.
here is my question.
I would like to make a scatterplot which (x,y) pairs are connected by line.
I have three treatments A,B,and C
A<-c(103.4,102.5,101.4,101.0,98.8)
B<-c(102.9,101.6,101.4,100.3,99.6)
C<-c(103.9,103.1,102.3,100.4,97.6)
These 3 variables are going to be plotted on the y axis (Temperature)
And I also have a variable for the x axis (Minutes)
M<-c(15,30,45,60,75)
I just would like to know how to make a plot using different lines and symbols for the 3 different treatments.
Thanks so much!
png() # default file name is "Rplot001.png" in your working directory
matplot(M, cbind(A,B,C), type="b",
xlab= "Minutes", ylab=expression(Temperature~degree*F),
xlim=range(M)+c(-5,5), ylim=range(c(A,B,C)) +c(-1,1) )
dev.off()
# See ?legend and ?title for further annotation

How do I create a histogram in R with logarithmic x and y axes? [duplicate]

This question already has answers here:
Histogram with Logarithmic Scale and custom breaks
(7 answers)
Closed 10 years ago.
So I have a vector of integers, quotes, which I wish to see whether it observes a power law distribution by plotting the frequency of data points and making both the x and y axes logarithmic. However, I am not quite sure how to accomplish this in R. I can currently create a histogram using
hist(quotes, breaks = max(quotes))
But the axes are all linear.
There's probably a better way to do this, but this (basically) works:
data = rnorm(1000,0,1)
r <- hist(log(data))
plot(r$breaks[-1],log(r$counts))
EDIT: Better solution:
r <- hist(data)
plot(r$breaks[-1], r$counts, log='xy', type='h')
# or alternatively:
barplot(r$counts, log="y", col="white", names.arg=r$breaks[-1])
The barplot version doesn't have a transformed x axis for reasons that will become clear if you try it with the x axis transformed.

Resources