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)
Related
I am doing quarterly analysis, for which I want to plot a graph. To maintain continuity on x axis I have turned quarters into factors. But then when I am using plot function and trying to color it red, the col argument is not working.
An example:
quarterly_analysis <- data.frame(Quarter = as.factor(c(2020.1,2020.2,2020.3,2020.4,2021.1,2021.2,2021.3,2021.4)),
AvgDefault = as.numeric(c(0.24,0.27,0.17,0.35,0.32,0.42,0.38,0.40)))
plot(quarterly_analysis, col="red")
But I am getting the graph in black color as shown below:
Converting it to a factor is not ideal to plot unless you have multiple values for each factor - it tries to plot a box plot-style plot. For example, with 10 observations in the same factor, the col = "red" color shows up as the fill:
set.seed(123)
fact_example <- data.frame(factvar = as.factor(rep(LETTERS[1:3], 10)),
numvar = runif(30))
plot(fact_example$factvar, fact_example$numvar,
col = "red")
With only one observation for each factor, this is not ideal because it is just showing you the line that the box plot would make.
You could use border = "red:
plot(quarterly_analysis$Quarter,
quarterly_analysis$AvgDefault, border="red")
Or if you want more flexibility, you can plot it numerically and do a little tweaking for more control (i.e., can change the pch, or make it a line graph):
# make numeric x values to plot
x_vals <- as.numeric(substr(quarterly_analysis$Quarter,1,4)) + rep(seq(0, 1, length.out = 4))
par(mfrow=c(1,3))
plot(x_vals,
quarterly_analysis$AvgDefault, col="red",
pch = 7, main = "Square Symbol", axes = FALSE)
axis(1, at = x_vals,
labels = quarterly_analysis$Quarter)
axis(2)
plot(x_vals,
quarterly_analysis$AvgDefault, col="red",
type = "l", main = "Line graph", axes = FALSE)
axis(1, at = x_vals,
labels = quarterly_analysis$Quarter)
axis(2)
plot(x_vals,
quarterly_analysis$AvgDefault, col="red",
type = "b", pch = 7, main = "Both", axes = FALSE)
axis(1, at = x_vals,
labels = quarterly_analysis$Quarter)
axis(2)
Data
set.seed(123)
quarterly_analysis <- data.frame(Quarter = as.factor(paste0(2019:2022,
rep(c(".1", ".2", ".3", ".4"),
each = 4))),
AvgDefault = runif(16))
quarterly_analysis <- quarterly_analysis[order(quarterly_analysis$Quarter),]
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])
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)
I need to draw a 2 line R program plot with data points on one line and a minimum Y value set to -6. The following data is the .csv input file:
> cat verifyRecording.csv
Time,MaxDBK,DBChange
07:30,20,2
07:35,21,1
07:40,22,0
07:45,23,-1
07:50,24,-2
07:55,32,-5
08:00,19,3
Below is an R program that takes the above .csv input and outputs a bar chart with 2 lines. I only need the 2 lines so it needs the barplot converted to a 2 line (plot) chart. Then add the d1$DBChange data points on the DBChange line and set the minimum Y value to -6.
#!/usr/bin/Rscript
d1 <- read.csv(file="verifyRecording.csv",head=T,sep=",")
# Provide an image size which will ensure the x labels display
png(filename="verifyRecording.png", width=1024, bg="white")
# Replace the barplot function with a plot function.
# Fix the Y values be to actually show -6 as the minimum value.
mp <- barplot(d1$MaxDBK, ylim=c(-6,50), main="Sound Recording started: 05/21/2017 7:25 AM", xlab="Time in 24hr", ylab="Sound in Decibals", border='blue')
axis(1,at=mp,labels=d1$Time)
lines(mp,d1$DBChange,type="o",pch=19,lwd=2,col="red")
lines(mp,d1$MaxDBK,type="o",pch=19,lwd=2,col="blue")
# Display the DBChange data values on the d1$DBChange line points.
##### points(d1$Time, d1$DBChange, type="l", col="red")
legend("topright", c("Recommended_DB_Change","Max_DB_Volume"), lty=c(1,1), lwd=c(2.5,2.5), col=c("red","blue"))
dev.off()
You can achieve a plot with only the lines if you start with an empty plot, instead of a barplot. This way, you just add the elements you want in it.
# Empty plot ("type = "n")
# xaxt = "n" in place to allow labelling with axis
# Set y axis limits
plot(1:nrow(d1), d1$MaxDBK, type = "n", ylim = c(-6, 50), xaxt = "n",
main = "Sound Recording started: 05/21/2017 7:25 AM",
xlab = "Time in 24hr",
ylab = "Sound in Decibals")
# Lines added
lines(d1$Time, d1$MaxDBK,type = "o", pch = 19, lwd = 2, col = "blue")
lines(d1$Time, d1$DBChange,type = "o", pch = 19, lwd = 2, col = "red")
# Label added in x axis. Changed at value to show properly
axis(side = 1, labels = d1$Time, at = d1$Time)
# Label each point with the values in d1$DBChange
text(d1$Time, d1$DBChange, labels = d1$DBChange, pos = 3)
I am plotting an axis with
a <- factor(letters[seq( from = 1, to = 26 )])
b <- 1:26
plot(a, b, axes = FALSE)
axis(1, at = a, labels = a, las = 2)
How can I get all the tick marks but only every nth label, e.g. (every 7th):
You can call axis twice, first to draw all tick marks and then again to put the labels:
c = a[seq(1, length(a),7)]
axis(1, at=a, labels = FALSE)
axis(1, at=c, labels = c, las = 2)