R Boxplot two rows at x-axis - r

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)

Related

"col" argument in plot function not working when a factor value is used for x - axis

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),]

Plot with a normal and a reversed y axis

I have data sets containing daily precipitation and discharge data. Now I would like to plot everything in one plot. All data sets are of length 61, so they can share the same x axis. The discharge data should be plotted the "normal" way, meaning that the y axis starts at the bottom and is placed on the left side. The precipitation data should be plotted "from the top", meaning that the y axis is reversed and placed on the right side.
Here is some code for a minimal reproducible example:
precipitation <- runif(61, min=0, max=25)
discharge <- runif(61, min=370, max=2610)
The result should approximately look like this:
Anybody with an idea how to achieve this?
EDIT: thanks pascal for the answer that implies the usage of ggplot2.
I also found a way by myself to do it with Base R, in case it could help anybody in the future:
precipitation <- runif(61, min=0, max=25)
discharge <- runif(61, min=370, max=2610)
# plot with Base R
par(mar = c(5, 5, 3, 5), xpd = TRUE)
plot(precipitation, type= "l", ylim= c(0,80), ylab= "Precipitation [mm/day]", main= "Comparison",
xlab= "Day", col= "blue")
par(new = TRUE)
plot(discharge, type= "l", xaxt = "n", ylim= rev(c(0,5000)), yaxt = "n", ylab = "", xlab = "", col= "red", lty= 2)
axis(side = 4)
mtext("Discharge [m³/s]", side = 4, line = 3)
The ggplot2 way looks a bit fancier of course.
ggplot2 can be used to make plots with a second, inverted axis. One has to specify sec.axis in scale_y_continuous(). I'm using a transformation ((100-x)*100) for your data and apply it to the axis as well, so that it fits. This can be changed to any numbers.
ggplot() +
geom_line(aes(y=precipitation, x=1:61), col="orange") +
geom_line(aes(y=100-discharge/100, x=1:61), col="blue") +
scale_y_continuous(name="rain", sec.axis=sec_axis(~(100-.)*100, name= "discharge"))

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)

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

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

Boxplot of 2 groups in R with graphics::boxplot

I have 2 groups of data (top and bottom) that I have been able to make boxplots for separately, but cannot get them to show in the same graph. I want them side by side for comparison for each gene tested (12 total). So, I would like to have the x-axis labeled with the gene tested, and the y-axis with the ddCt values, with 2 boxplots (1 for the top, 1 for the bottom) for each gene. I have the code for each one separately that works below:
# boxplot of first group
boxplot(Top25[-1], main = "Top Performers Gene Expression Relative to 16S", ylab = "ddCt Values", xlab = "Biofilm Gene", cex.axis = 0.75)
# boxplot of second group
boxplot(Bottom25 [-1], main = "Bottom Performers Biofilm Gene Expression Relative to 16S", ylab = "ddCt Values", xlab = "Biofilm Gene", cex.axis = 0.75)
Any suggestions for what I may try? I've tried to "melt" them together following another ggplot2 question and got an error saying "object melt was not found". Thanks for any help!
Here is an example. The main idea is to use add = TRUE to add boxplot to an existing plot and specify the horizontal position of boxplot with at
#DATA
set.seed(42)
top = rnorm(40)
bottom = rnorm(40)
#Create Empty Plot
plot(1, 1, type = "n", xlim = c(0, 3), ylim = range(c(top, bottom)),
ann = FALSE, xaxt = "n")
#Add boxplots to existing plot
boxplot(x = top, at = 1, add = TRUE)
boxplot(x = bottom, at = 2, add = TRUE)
axis(1, at = c(1, 2), labels = c("top", "bottom"))
You can do this:
Top25 <- data.frame(gene=1:40, exp=rnorm(40, 2))
Bottom25 <- data.frame(gene=41:80, exp=rnorm(40, -2))
boxplot(list(Top25[, -1], Bottom25[, -1]), names=c("Top25", "Bottom25"))

Resources