How to remove only the left axis labels in R cdplot? - r

The attached example produces a conditional density plot. In the example, I can use ylab = NA to remove the "x" axis label and if I use yaxlabels = "n" both the left and right tick labels are removed.
How can I suppress only the left labels (i.e. "0" and "1") and keep the right-hand probability scale?
# Example
Forest = c(1,0,1,1,1,0,1,1,0,1)
change = c(-1, 3, 1, 4, 1, 1, -1, 1, -1, 6)
mydata = data.frame(Forest,change)
x = factor(mydata$Forest)
cdplot(x~mydata$change, ylab = NA, xlab = NA)

As you said, you can use yaxlabels = "n" to remove the y-labels on both sides.
Than just add the right side axis back via axis(4).
Forest = c(1,0,1,1,1,0,1,1,0,1)
change = c(-1, 3, 1, 4, 1, 1, -1, 1, -1, 6)
mydata = data.frame(Forest,change)
x = factor(mydata$Forest)
cdplot(x~mydata$change, ylab = NA, xlab = NA, yaxlabels = "n")
axis(4)

Related

label annotation on top of bar using barplot in r

Need to move rotated labels on the top of the bar using barplot. What parameter am I missing here?
code:
df<- data.frame(a=strrep(letters[1:20], 10) , b=runif(20, min=1, max=30))
df<- df[order(df$b, decreasing = TRUE),]
row.names(df)<- df$a
par(mar=c(15, 5, 3, 2)+ 0.2)
x<- barplot(df$b, c(2, 4, 1, 6), ylim = c(0, 30), ylab="statistics", col = heat.colors(20), xaxt="n")
label = row.names(df)
text(cex=0.2, x=x, y=-1.25, label, xpd=TRUE, srt=45)
You need to change the position of y-axis in text with a suitable offset. (I have used + 1 here).
x <- barplot(df$b, c(2, 4, 1, 6), ylim = c(0, 30), ylab="statistics",
col = heat.colors(20), xaxt="n")
label = row.names(df)
text(cex=0.2, x=x, y=df$b + 1, label, xpd=TRUE, srt=45)

Create one-dimensional plot in R with text labels

I want to create a simple one-dimensional plot in R ranging from 0 to ten, with a scale (small lines for integer values 1,2, etc.), a slightly higher line for 5 (the median) and slightly higher than all of the other for 0 and 10. Then I want to fill this plot with a few points representing values like 2, 4, 5, 6, 8, and a text above each one of them with corresponding labels (like "party voted", "closest party", "individual 1", "expert", "individual 2"). It can be smaller labels, like "PV", "CP", etc.
I would like to have control over shape and color (say in data-points 4 and 6 I have a circle filled in black but in position 2 I have a not filled square, in position 5 I have a green circle filled-in, and in position 8 I have a black triangle, also filled). I would like to have 0, 10 and 5 marked in the labels as well.
A very rough representation of what I am trying to draw is in the image below (it has all the elements I want, at least).
In this Stack Overflow question there is some code on one-dimension plot, I have tried to adapt it to what I need but didn't get to it
I've assumed from the link in the question that you are looking for a base R solution.
There may be more efficient solutions but this seems to get you where you want.
I've avoided the need for arrows by forcing the labels to run over two lines and reducing the text size on the plot so they do not overlap.
You could manage this with arrows if need be, but this seems it will need a lot of extra code.
# data
df <- data.frame(desc = c("Party voted", "Closest party", "Individual 1", "Expert", "Individual 2"),
score = c(2, 4, 5, 6, 8),
y = 1)
# add line break to labels
df$desc <- gsub("\\s", "\n", df$desc)
plot(df$score,
df$y,
# type = "o",
xlim = c(0, 10),
pch = c(1, 21,21,21, 24),
col = c("black", "black", "green", "black", "black"),
bg = c("black", "black", "green", "black", "black"),
cex = 1.5,
xaxt = "n", #remove tick marks
yaxt = "n",
ylab = '', # remove axis labels
xlab = '',
bty = "n") # remove bounding box
axis(side = 1,
0:10,
pos = df$y,
labels = FALSE,
tck = 0.02)
axis(side = 1,
0:10,
pos = df$y,
labels = c(0, rep("", 4), 5, rep("", 4), 10),
tck = -0.02)
axis(side = 1,
c(0, 5, 10),
pos = df$y,
labels = FALSE,
tck = 0.05)
axis(side = 1,
c(0, 5, 10),
pos = df$y,
labels = FALSE,
tck = -0.05)
text(x = df$score,
y = df$y,
labels = df$desc,
pos = 3,
offset = 1,
cex = 0.75)
Created on 2021-04-28 by the reprex package (v2.0.0)

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.

Adding a second row of x-axis values in a different format in R

I'm wondering how I could add another row of x-axis values right below (along side) the current x-axis values (i.e., 0 to 1) that begin from (ie., 0% to 100%)?
To summarize in 3 steps:
(A): the second row of axis values need NOT have tick marks. (B):, the second row of axis values need to appear exactly below each corresponding value of the first x-axis values. (C):, the second row of axis values must show a "%" sign next to them.
plot(1, ty='l', ann = F, axes = F, xlim = c(0, 1) )
axis(1, at = round(seq(0, 1, len = 9), 2), font = 2, cex.axis = 1.2 )
Look up pos, tck, and tick of axis
par(mar = c(10,3,3,3))
plot(1, 1, type = 'l', ann = F, xlim = c(0, 1), xaxt = "n")
axis(1, at = round(seq(0, 1, len = 9), 2), font = 2, cex.axis = 1.2 ) #First axis
axis(1, at = round(seq(0, 1, len = 9), 2),
labels = paste(100* round(seq(0, 1, len = 9), 2),"%",sep=""),
pos = par("usr")[3] - 1 * 0.1 * (par("usr")[4] - par("usr")[3]),
tick = FALSE, font = 2, cex.axis = 1.2) #2nd axis labels

Find optimal width for left margin in R plot

I would like to write a plot function for my specific purposes and put the y labels on the left margin. The length of these labels, however, can differ dramatically and depends on the model terms the user comes up with. For this reason, I would like to measure the width of the longest label and set the left margin width accordingly. I found the strwidth function, but I don't understand how to convert its output unit to the unit of the mar argument. An example:
label <- paste(letters, collapse = " ") # create a long label
par(mar = c(5, 17, 4, 2) + 0.1) # 17 is the left margin width
plot(1:2, axes = FALSE, type = "n") # stupid plot example
# if we now draw the axis label, 17 seems to be a good value:
axis(side = 2, at = 1, labels = label, las = 2, tck = 0, lty = 0)
# however, strwidth returns 0.59, which is much less...
lab.width <- strwidth(label) # so how can I convert the units?
You can use mai instead of mar to specify a distance in inches
(instead of "lines").
par(mai = c(1, strwidth(label, units="inches")+.25, .8, .2))
plot(1:2, axes=FALSE)
axis(side = 2, at = 1, labels = label, las = 2, tck = 0, lty = 0)
You can compute the conversion factor between lines and inches
by dividing mar by mai.
inches_to_lines <- ( par("mar") / par("mai") )[1] # 5
lab.width <- strwidth(label, units="inches") * inches_to_lines
par(mar = c(5, 1 + lab.width, 4, 2) + 0.1)
plot(1:2, axes=FALSE)
axis(side = 2, at = 1, labels = label, las = 2, tck = 0, lty = 0)

Resources