R - Customizing X Axis Values in Histogram - r

I want to change the values on the x axis in my histogram in R.
The computer currently has it set as
0, 20, 40, 60, 80, 100.
I want the x axis to go by 10 as in:
0,10,20,30,40,50,60,70,80,90,100.
I know to get rid of the current axis I have to do this
(hist(x), .... xaxt = 'n')
and then
axis(side = 1) .....
But how do I get it to show the numbers that I need it to show?
Thanks.

The answer is right there in ?axis...
dat <- sample(100, 1000, replace=TRUE)
hist(dat, xaxt='n')
axis(side=1, at=seq(0,100, 10), labels=seq(0,1000,100))

Related

How to change tick-marks for y-axis in base histogram?

I would like to change my Y axis of these histograms to start at 20 and end at 180 AND making it so there is always 20 between each number (20, 40, 80, ...). How should I do it?
I read about yaxis command, but I just dont know how to make it work as I am a total noob in coding (no education in that area).
This is the graph I am working on:
And this is the code I have:
orientation$head_linear <- ifelse(orientation$head > 180, 360 - orientation$head, orientation$head)
orientation$body_linear <- ifelse(orientation$body > 180, 360 - orientation$body, orientation$body)
par(mfrow = c(2,1))
hist(orientation$head_linear, main = NULL, ylim=c(20,180), ylab = NULL, xlab = NULL)
hist(orientation$body_linear, main = NULL, ylim=c(20, 180), ylab = NULL, xlab = " Odchylka od vletového otvoru ")
I have set the limit of Y axis with ylim code, but it doesnt seem to work (I have succesfully used it before in different work).
Maybe you mean the xaxt= argument whith which you can omit the y-axis. If you use it, you may create a custom axis afterwards. Use a seq(from=0, to=360, by=20) for it's at= argument.
par(mfrow=c(2, 1))
hist(orientation$head_linear, main=NULL, yaxt='n', ylab=NULL, xlab=NULL)
axis(side=2, at=seq(from=0, to=360, by=20))
hist(orientation$body_linear, main=NULL, yaxt='n', ylab=NULL, xlab=" Odchylka od vletového otvoru ")
axis(2, seq(0, 360, 20))
Data:
n <- 500
orientation <- data.frame(
head_linear=sample(360, n, replace=TRUE),
body_linear=sample(360, n, replace=TRUE)
)

I can't add axis() to plot when using abline(lm()), why?

I am struggling because I am just trying to add my own x-axis values and tickmarks to a plot which shows also the linear regression.
It seems that OR I add the abline OR I add the axis. I can't do both!
Why?
Example data:
df = data.frame(year = c(1901:2000), total = ceiling(runif(100, 2, 3000)))
This code works ONLY for abline():
plot(df$year, df$total, xaxt='n')
abline(lm(df$total ~ df$year))
axis(1, at = seq(1,100, by = 10), labels = seq(1901, 2000, by = 10)) #this line does not work
This code works only for axis():
plot(df$total, xaxt='n')
abline(lm(df$total ~ df$year)) #this line does not work
axis(1, at = seq(1,100, by = 10), labels = seq(1901, 2000, by = 10))
Any help, please?
thank you
The x-axis does not necessarily go from 1 to 100. The units are defined by the scale of your variable.
In your first example, the x-axis actually goes from 1901 to 2000. You therefore need to define at in that range, otherwise your values fall outside the plot and are invisible. Simply do:
plot(df$year, df$total, xaxt='n')
abline(lm(df$total ~ df$year))
axis(1, at = seq(1901, 2000, by = 10))
The second example your axis call works because you are plotting index on the x-axis, which just counts from 1 to n observations (in this case 100). The abline is not visible, since it is not within the range of that plot.

Change size axis plot in R

Hello how to change size axis plot in R
plot(c(imf[1,]), ylim=c(-100, 100),type="l", col="blue")
And the result is
I want axis is sequence 1:36, not (0, 5, 10, 20, 25, 30, 35) but (1,2,3,4,5,6,7,.....36)
You should axis. But it is optimized to not overlap overlap previously drawn labels, so here I am playing with cex parameter (depends on you window size) to show all labels.
plot(1:36, rnorm(36), axes = FALSE)
axis(1, 1:36, 1:36,cex.axis=0.5)
Suppress the x-axis with xaxt = "n" and add it back with your own axis.
plot(runif(36), type="l",col="blue", xaxt = "n", cex.axis=0.7)
axis(1, at=1:36, labels=1:36,cex.axis=0.7)

Customizing Y axis values in plot

I am trying to get my graph in R to change it's Y axis values.
Code:
plot(tree$NUM,tree$GRA,
main="YSLOW Grades",
xlab="HAR #",
ylab="Grade",
xaxt="n")
axis(1, at = seq(1, 20, by = 1), las=2)
I have figured out how to customize the x axis, but from all my researching I cannot find a way to simply change the Y axis too. Instead of having numbers, I want to customize the graph so that I can but letter grades in like A,B,C and so on. I assume it's a quick fix to do this but I really am clueless and the material seems lacking on the subject.
To clarify, I do not want to change Y axis label or the spacing, I simply want to be able to but letters on the Y axis, regardless of the data coming into it.
Just put yaxt = "n" and put the new y labels with axis(2, ....). Example:
plot(1:20,1:20,
main="YSLOW Grades",
xlab="HAR #",
ylab="Grade",
xaxt="n",
yaxt = "n")
axis(1, at = seq(1, 20, by = 1), las=2)
axis(2, at = seq(1, 20, by = 1), label = rep(c("A", "B"), 10), las=2)

Plotting a histogram with custom breaks

I have a vector like:
K <- rnorm(10000, mean=100)
I want to create a histogram of K with custom breaks (and labels) like <20, 20-50, 50-75, 75-99, =100, >400, etc.
Any ideas?
With base plotting, you might be better off cutting the vector first, and then using barplot or the plot method for tables.
For example:
K <- rnorm(10000, mean=100, sd = 100)
K.cut <- cut(K, c(-Inf, 20, 50, 75, 100, 400, Inf))
plot(table(K.cut), xaxt='n', ylab='K')
axis(1, at=1:6, labels=c('< 20', '20-50', '50-75', '75-100', '100-400', '> 400'))
box(bty='L')
xax <- barplot(table(K.cut), xaxt='n')
axis(1, at=xax, labels=c('< 20', '20-50', '50-75', '75-100', '100-400', '> 400'))
box(bty='L')
Note that by default, cut includes the upper (but not the lower) bound in each bin, so for example the 20-50 bin includes any 50s, but the 20s will be included in the lower adjacent bin.
Try ggplot version:
library(ggplot)
ggplot()+ geom_histogram(aes(K))
Many options are available for tweaking.

Resources