How to replace axis in gap.barplot in R - r

I've been trying to figure out how to replace the x-axis in gap.barplot in R. First, I have an issue with labeling:
Attached is my code:
Samples Conc stdlo stdhi
SampA 5000 0 0
SampB 100 0 11
SampC 80 0 20
rm(list=ls())
library(plotrix)
C.dat <- read.csv("G:/...../C.csv", head = TRUE)
C.lab = C.dat$Samples
C.conc = C.dat$Conc
C.lostd = C.dat$stdlo
C.histd = C.dat$stdhi
par(mar=c(6,6,5,2))
barplot = gap.barplot(C.conc, gap = c(200,1000), xlab = "Samples",
ylab ="C Conentration (pg/mL)", main = "C in X and Y", las = 2,
xlim = c(0,4), ytics = c(0,1000,1500,5100), cex.lab = 1.5)
mtext("SampA", side = 1, at= 1.0, cex=1.0)
mtext("SampB", side = 1, at= 2.0, cex=1.0)
mtext("SampC", side = 1, at= 3.0, cex=1.0)
arrows(barplot,C.conc-0 ,barplot,C.conc+C.histd,code=2,angle=90,length=.1)
My biggest issue is when I stick in axes = FALSE in the gap.barplot parameters, it gives me a warning and no plot is produced. I want to get rid of the "1 2 3" axes label and the tick marks.
Also, if anyone has any idea how to move the y-axis label a bit more to the left, that would be nice.
Any suggestions?

You may try this. I call your data frame df.
I added xaxt = "n" to the gap.barplot call.
From ?par:
xaxt: A character which specifies the x axis type. Specifying "n" suppresses plotting of the axis.
Then axis is used to add an x axis with labels at positions at, but with no ticks (tick = FALSE). The label for the y axis is added with mtext
library(plotrix)
par(mar=c(6,6,5,2))
gap.barplot(df$Conc, gap = c(200,1000),
xlab = "Samples", ylab ="", main = "C in X and Y", las = 2,
xlim = c(0, 4), ytics = c(0, 1000, 1500, 5100), cex.lab = 1.5,
xaxt = "n")
axis(side = 1, at = seq_along(df$Sample), labels = df$Sample, tick = FALSE)
mtext("C Concentration (pg/mL)", side = 2, line = 4)

Related

R double Y axis seperating

I was wondering if it is possible to seperate two plots from eachother (both should be on the same plot, using double Y axis). So the double plot should be split into two but without actually plotting them seperate - par(mfrow(1,2)).
I was trying to imitate it with layout plot, or with latticeExtra, ggplot but no success.
I have two different dataset one for the exchange rate one for the logaritmic returns.
par(mar=c(4,4,3,4))
plot(rates$EURHUF~rates$Date, type="l", ylab="Rate", main="EUR/HUF", xlab="Time")
par(new=TRUE)
plot(reteslog$EURHUF~rateslog$Date, type="l", xaxt="n", yaxt="n", ylab="", xlab="", col="red")
axis(side=4)
mtext("Log return", side=4, line=3)
legend("topleft", c("EUR/HUF Rates","EUR/HUF Logreturns"), col=c("black", "red"), lty=c(1,1))
So far I am here, I just don't know how to seperate them or scale them (maybe using margin, or layout?)
Thank you very much guys for helping
I have a solution to this that isn't too outlandish, and is entirely in base, which is nice. For it to work, you just need to be able to force all of your data onto the same scale, which usually isn't a hassle.
The idea is that once your data is on the same scale, you can plot it all normally, and then add in custom axes that show the respective scales of the different data.
set.seed(1986)
d01 <- sample(x = 1:20,
size = 200,
replace = TRUE)
d02 <- sample(x = 31:45,
size = 200,
replace = TRUE)
# pdf(file = "<some/path/to/image.pdf>",
# width = 4L,
# height = 4L) # plot to a pdf
jpeg(file = "<some/path/to/image.jpeg>") # plot to a jpeg
par(mar=c(3.5, 3.5, 2, 3.5)) # parameters to make things prettier
par(mgp=c(2.2, 1, 0)) # parameters to make things prettier
plot(x = 0,
y = 0,
type = "n",
xlim = c(1, 200),
ylim = c(1, 50),
xlab = "Label 01!",
ylab = "Label 02!",
axes = FALSE,
frame.plot = TRUE)
points(d01,
pch = 1,
col = "blue") # data 01
points(d02,
pch = 2,
col = "red") # data 02
mtext("Label 03!",
side = 4,
line = 2) # your extra y axis label
xticks <- seq(from = 0,
to = 200,
by = 50) # tick mark labels
xtickpositions <- seq(from = 0,
to = 200,
by = 50) # tick mark positions on the x axis
axis(side = 1,
at = xtickpositions,
labels = xticks,
col.axis="black",
las = 2,
lwd = 0,
lwd.ticks = 1,
tck = -0.025) # add your tick marks
y01ticks <- seq(from = 0,
to = 1,
by = 0.1) # tick mark labels
y01tickpositions <- seq(from = 0,
to = 50,
by = 5) # tick mark positions on the y01 axis
axis(side = 2,
at = y01tickpositions,
labels = y01ticks,
las = 2,
lwd = 0,
lwd.ticks = 1,
tck = -0.025) # add your tick marks
y02ticks <- seq(from = 0,
to = 50,
by = 5L) # tick mark labels
y02tickpositions <- seq(from = 0,
to = 50,
by = 5) # tick mark positions on the y02 axis
axis(side = 4,
at = y02tickpositions,
labels = y02ticks,
las = 2,
lwd = 0,
lwd.ticks = 1,
tck = -0.025) # add your tick marks
dev.off() # close plotting device
A few notes:
Sizing for this plot was originally set for a pdf, which unfortunately cannot be uploaded here, however that device call is included as commented out code above. You can always play with parameters to find out what works best for you.
It can be advantageous to plot all of your axis labels with mtext().
Including simple example data in your original post is often much more helpful than the exact data you're working with. As of me writing this, I don't really know what your data looks like because I don't have access to those objects.

How to specify label length in R for axis parameter. My label names are truncated

In R, I've created a plot with a separate axis on the left to specify horizontal labels for the y-axis of the plot. However, the labels are truncated. That is, the complete name is not shown, only the last 9 characters of the name is shown. I use the R axis command to create the axis and the labels = names parameter to specify the names. names is a vector of character label names which vary in length.
plot(x = c(0,mx),y=c(1,n),yaxt = 'n', xlab = "Dollars - 100,000's", cex.axis = .65, typ = 'n', ylab = '', cex.lab = .8)
axis(side = 2, at = seq(1,n), labels = names, las = 2, cex.axis = .65)
Here's what the plot looks like:
Plot with truncated names
You can modify the margin of your plot by passing the mar argument to par function:
mar.default <- c(5,4,4,2) + 0.1
par(mar = mar.default + c(0, 4, 0, 0))
plot(x = c(0,mx),y=c(1,n),yaxt = 'n', xlab = "Dollars - 100,000's", cex.axis = .65, typ = 'n', ylab = '', cex.lab = .8)
axis(side = 2, at = seq(1,n), labels = names, las = 2, cex.axis = .65)
Without a reproducible example of your dataset, I can't guarantee that it will work right away, maybe you will have to adjust it on your own.
Otherwise, you can provide a reproducible example of your data: How to make a great R reproducible example

R is plotting y axis upside down. How to flip rightside up?

I've inherited this R code that plots a simple line graph. However, it does it so that the y axis values are plotted downwards below 0 (plots it in the 4th quadrant with 0 at the top and +3600 at the bottom). I want to plot the data right-side up (1st quadrant) so the y axis data goes from 0 up to +3600 at the top like a typical grade-school plot.
I've tried ylim = rev(y) but it returns an error...
I've also tried flipping the seq() command but no luck there.
list.vlevel = numeric(9) # placeholder
plot(
rep(0, length(list.vlevel)),
seq(1, length(list.vlevel)),
type = "n",
xlim = biaslim,
axes = F,
main = paste(list.var.bias[vv], list.score.bias[vv]),
xlab = "",
ylab = ""
)
abline(h = seq(1, length(list.vlevel)),
lty = 3,
col = 8)
axis(2,
labels = list.vlevel,
at = seq(length(list.vlevel), 1, -1),
las = 1)
axis(1)
box()
legend(
x = min(biasarray.var.runhour),
y = length(list.vlevel),
legend = expname,
lty = 3,
lwd = 3,
col = expcol
)
for (exp in seq(length(expname), 1, -1)) {
lines(
biasarray.var.runhour[exp, ],
seq(length(list.vlevel), 1, -1),
col = expcol[exp],
lwd = 3,
lty = 3
)
}
abline(v = 0, lty = 3)
The plot should end up in the first quadrant with yaxis values increasing from 0 upwards to +###.
The axis(2, ...) line draws the y axis. You can see that is the labels follow a descending sequence: seq(length(list.vlevel), 1, -1). seq(1, length(list.vlevel))
Similarly, inside lines(), probably you need to make the same change from seq(length(list.vlevel), 1, -1) to ``seq(1, length(list.vlevel))`
That's as much as we can tell with the info you've provided - can't run any of yoru code without values for all the constants you use, e.g., biasarray.var.runhour, list.var.bias, vv, etc.

R draw lines over two different scales

I have built two graphs in one (two different y axis but plotting on the same graph). I want to show the connection between the values on the left and the values on the right (Do they stay consistently > 0 or <0 or change?)
Now what I need is to link the two sides of the graph with a line to see if it decreases/increases. So I want a corresponding dot on the left to be linked to the corresponding dot on the right by a line.
But because the y axis values on the left and the right are different, I am not figuring out how this can work.
Here is my code to build the graph:
## Plot first set of data and draw its axis
plot(rep(1, length(DEG)), DEG, xlim = c(0,4), xaxt = "n",
ylim = c(-5, 5), col = "black", xlab = "", ylab = "")
axis(1, at = c(1))
## Allow a second plot on the same graph
par(new = TRUE)
## Plot the second plot and put axis scale on right
plot(rep(3, length(DMG)), DMG, axes = F, xlim = c(0, 4), xaxt = "n",
ylim = c(-80, 80), col = "black", xlab = "", ylab = "")
axis(1, at = c(3)))
axis(side = 4)
abline(h = 0, col = "red")
Does anyone have an idea? I tried the basic line:
lines(x$DEG[x$Genes == "FEX_0000936"], x$DMG[x$Genes == "FEX_0000936"],
type="o", pch=22, col="seagreen3")
Here is my graph perhaps it is clearer when seeing it:
Thanks for your help.

Why the y axis in my barplot doesn't show completely?

I don't know how to change the code so that the y-axis in the barPlot shows completely? I expect it to show up to 10 as I have a 9.2 in my data points but it shows only up to 8. Any idea what's the hack to this?
Here's the code:
And here's what it shows:
just set the ylim = c(0, 10) like the way you changed the xlim
Whatever the axis that the groups go on isn't drawn by default, so a vertical bar plot won't have x-axis; horizontal won't have y-axis. You can add that of course. Use the return value of barplot:
par(mfrow = c(2, 1))
bp <- barplot(c(8, 5), width = .5, main = 'Feature Exploration', xlim = c(0,4), ylim = c(0, 10),
ylab = 'Errors (%)', xlab = 'ML Models', col = c('gray27','orangered4'))
## this will draw the x-axis but at points 1, 2, 3, ... which is not
## where the centers of your bars are plotted; you get that info in bp
axis(1)
bp <- barplot(c(8, 5), width = .5, main = 'Feature Exploration', xlim = c(0,4), ylim = c(0, 10),
ylab = 'Errors (%)', xlab = 'ML Models', col = c('gray27','orangered4'))
## so try again with a fancy axis, bp is a matrix containing the centers
## of the plotted bars
axis(1, at = bp, labels = c('Model1','Model2'), lwd = 0, lwd.ticks = 1, tcl = -.5)

Resources