How to label only specific ticks in a base R plot? - r

How to label only specific ticks in a base R plot? In the following example I would like to plot ten ticks, but only the first and the last one should be labelled.
plot(1 : 10, xaxt = "n", xlab = 'Some Letters')
axis(1, at = c(1, 10), labels = letters[1 : 2])

Related

Hiding tick marks and tick labels of minimum and maximum limits of axes for base R plots

Suppose I make a barplot and a scatterplot using the following lines of code:
x <- c(1, 2, 3)
xlabs <- c("A", "B", "C")
y <- c(4, 5, 6)
barplot(x,
xlab = "X",
names.arg = xlabs,
ylim = c(0, 3),
ylab = "Y")
plot(x, y,
xlim = c(1, 3),
xlab = "X",
ylim = c(4, 6),
ylab = "Y")
How can the axes tick marks and tick labels for the minimum and maximum limits only be hidden? Here is what I intend to accomplish:
There are a couple of options. First, you generally want to drop the default axis labels using xaxt='n' or yaxt='n'. Next, you could manually specify the locations of the tick marks in axis() or use the pretty() function with dropping the first and last values (used head and tail in example) to get some nice tick mark locations. It might require some manual fine tuning.
x <- c(1, 2, 3)
xlabs <- c("A", "B", "C")
y <- c(4, 5, 6)
barplot(x,
xlab = "X",
names.arg = xlabs,
ylim = c(0, 3),
ylab = "Y",
yaxt='n' # drops the y axis and tick marks
)
# draw a vertical line that will cover the whole vertical axis, if desired
abline(v=par('usr')[1],lwd=2)
# pretty will give nice tickmark locations, and head and tail drop first and last
# add the 0 to x vector because barplot starts at 0
axis(side=2,tail(pretty(c(0,x)),-1))
plot(x, y,
xlim = c(1, 3),
xlab = "X",
ylim = c(4, 6),
ylab = "Y",
xaxt='n', # no default x axis with ticks
yaxt='n' # no default y axis with ticks
)
# add tickmark and axis locations manually for each axis
# using the pretty function for where the tick marks normally would be, then head and tail to drop first and last
axis(side=1,tail(head(pretty(c(x)),-1),-1))
axis(side=2,tail(head(pretty(c(y)),-1),-1))

formatting the x-axis exponential plot in R as a^x?

I have generated this plot in R with some strange numbers format in the x-axis:
enter image description here
I want to have in the x-axis the numbers in the format (ax) as 2^6, 6^6, 10^6. this would simplify the x-axis to get data in all points. Please do you have any suggestions?
Here my code :
data=read.csv("my_file.csv",row.names = 1)
plot(genes~Prot,cex=1.5,data, function(x) 10^x, xlab="Proteome
size(codons)",ylim=c(0,30), ylab="Genes in pathway")
abline(lm(prot~genes,data),lty=2, lwd=3,col="black")
Use xaxt = 'n' as an argument to plot to turn off the x-axis labelling. Then use the Axis function to set tick marks and label as required.
# Generating some data
power <- seq(1, 6, length.out = 20)
Prot = 10^power
genes <- runif(20, min = 5, max = 30)
# plotting
plot(x= Prot, y= genes, cex=1.5, xlab="Proteome size(codons)", ylab="Genes in pathway", xaxt = 'n', log = 'xy')
Axis(at = c(2^6, 6^6, 10^6), side = 1, labels = c('2^6', '6^6', '10^6'), las = 1)

How to Remove Tick Marks on Boxplot

I am trying to remove the x-axis tick marks from my boxplot, but keep the labels associated with the tick marks. Is this possible in base R?
colors <-c("lightskyblue3", "gray78","gold1", "wheat1")
boxplot(avgscore~module, data=microbox,
names=c("Cultural Diversity","UDL","Differentiated", "Instruction","Classroom Management"),ylim = range(2.5,4.5), ylab="Average Score",
# main="Distribution of Average Score by Module",#
col=(c("lightskyblue3", "gray78","gold1", "wheat1")))
First suppress the x-axis with xaxt = "n" and then add axis with tick = FALSE
graphics.off()
b = boxplot(mpg~cyl, mtcars, names = c("four", "six", "eight"), xaxt = "n")
axis(side = 1, at = seq_along(b$names), labels = b$names, tick = FALSE)

R Boxplot two rows at x-axis

I try to draw a boxplot with two rows of numbers on the x-axis.
Where #1 is the default and I want it to be like #2. Is it possible to add another row and give it unique ticks and intervalls?
So far I tried the axis() function and was trying to find out if it is possible to use rows from excel as axis input.
boxplot(data, yaxp=c(-2,1,6), ylim=c(-2, 1), names=c(1:40), main = "header",
xlab="x axis title", ylab="y axis title")
It is possible with axis(). You can choose your own labels. The line argument changes the distance from the original ticks.
boxplot(mpg ~ cyl,
data = mtcars,
main = "Car Milage Data",
xlab = "Number of Cylinders", ylab = "Miles Per Gallon")
axis(side = 1, line = 1, at = c(1, 2, 3), labels = c("A", "B", "C"), tick = F)

Getting plot errors when creating custom axes

I have an issue with the creation of custom axes in the base plotting system in R, I have the following data frame for which I want to plot a trend to show the changes for each year:
year <- c(2000, 2002, 2005, 2009)
values <- c(7332967, 5332780, 5135760, 3464206)
x <- data.frame(year, values)
## year values
## 1 2000 733296
## 2 2002 533278
## 3 2005 513576
## 4 2009 346420
My first attempt is:
plot(x$year, x$value,
xlab = "Year",
ylab = "Value",
type = "b")
However, that gives me a skewed x and y axis for the four values I have in the data frame. I would like for the x axis to only contain the four values under the "year" column and y axis to only contain the four values under the "values" column.
For this purpose I tried to create custom x and y axis but that resulted in errors:
plot(x$year, x$value,
type = "b",
xaxt = "n",
yaxt = "n",
xlab = "Year",
ylab = "Values",
axis(1, at = 1:nrow(x), labels = x$year),
axis(2, at = 1:nrow(x), labels = x$value))
"Error in plot.window(...) : invalid 'xlim' value"
and:
plot(x$year, x$value,
type = "b",
xaxt = "n",
yaxt = "n",
xlab = "Year",
ylab = "Values",
axis(1, at = 1:nrow(x), labels = x$year),
axis(2, at = 1:nrow(x), labels = x$value),
xlim = c(min(data_plot$year), max(data_plot$year)),
ylim = c(min(data_plot$Emissions), max(data_plot$Emissions)))
"Error in strsplit(log, NULL) : non-character argument"
I am quite new to R and tried searching for solutions on various sites, however, nothing seems to solve the issue so any help provided would be much appreciated.
axis is a separate function, not an argument to plot, so try the following:
# First make some extra space on the left for the long numeric axis labels
par(mar=c(5, 6, 1, 1))
# Now plot the points, but suppress the axes
plot(x$year, x$values, xaxt='n', yaxt='n', xlab='Year', ylab='', type='b')
# Add the axes
axis(1, at=x$year, labels=x$year, cex.axis=0.8)
axis(2, at=x$values, labels=x$values, las=1, cex.axis=0.8)
# Add the y label a bit further away from the axis
title(ylab='Value', line=4)

Resources