plot axis labels with multiple colours - r

I'm making plots like the one generated with the following code:
var1 <- sort(runif(10, 0, 1), decreasing = TRUE)
var2 <- sort(runif(10, 0, 1))
plot(var1, pch = 20, ylab = c("Var 1", "Var 2"))
points(var2, pch = 20, col = "grey")
Is there a way, with just the R graphics package, to place a black circle before Var 1 and a grey circle before Var 2 in the y axis label, to avoid having to insert a legend? Or alternatively, a way to use different text colours (black for Var 1 and grey for Var 2) in the y axis? I tried using col.lab = c("black","grey"), but it says Error in plot.window(...) : graphical parameter "col.lab" has the wrong length.
Many thanks in advance,
Márcia

I'm not sure how to add the point to the label, but an easy way to labe with color can be done in the following way:
var1 <- sort(runif(10, 0, 1), decreasing = TRUE)
var2 <- sort(runif(10, 0, 1))
plot(var1, pch = 20, ylab = "")
points(var2, pch = 20, col = "grey")
mtext("Var 1", side=2, line=2)
mtext("Var 2", side=2, line=3, col="grey")

Would something like this work for you? It's a bit busy on the left axis, but I think it shows what you are asking about.
> var1 <- sort(runif(10, 0, 1), decreasing = TRUE)
> var2 <- sort(runif(10, 0, 1))
> plot(var1, ylim = range(c(var1, var2)), pch = 20, ylab = "", axes = FALSE)
> points(var2, pch = 20, col = "grey")
> labs <- round(sort(c(var1, var2)), 1)
> axis(1)
> axis(2, at = sort(c(var1, var2)), labels = labs)
> sapply(var1, function(x) points(-0.1, x, pch = 20))
> sapply(var2, function(x) points(-0.1, x, pch = 20, col = "grey"))
> box()

Related

adding text to a plot at specified location

I want to add labels to each single line in the plot below:
a <- 1:2000
b <- a - a[1]
plot(1, type = "n", xlab = "Scale parameter", ylab = "No. of days", xlim = c(0, 90), ylim = c(0, 150))
shape.range <- seq(from = 2, to = 10, by = 1)
scale.range <- seq(from = 10, to = 70, by = 1)
for(sh in seq_along(shape.range)){
sh.ref <- shape.range[sh]
for(sc in seq_along(scale.range)){
sc.ref <- scale.range[sc]
p <- 1 - exp(-(b/sc.ref)^sh.ref)
p.l <- which.max(p >= 0.97)
points(sc.ref, p.l, cex = 0.5, pch = 19)
# text(80, # how to insert the value of y here such that the label ends up at the end of the each line, labels = paste0(sh.ref))
}
}
Not everything needs to be ggplots. Base graphics is much easier to tweak sometimes.
Just add these lines after your code.
text(20,100,"Text left")
text(60,20,"Text right")

R. How to avoid lines connecting dots in dotplot

I made a plot using plot() using RStudio.
x = X$pos
y = X$anc
z = data.frame(x,y)
#cut in segments
my_segments = c(52660, 106784, 151429, 192098, 233666,
273857, 307933, 343048, 373099, 408960,
441545, 472813, 497822, 518561, 537471,
556747, 571683, 591232, 599519, 616567,
625727, 633744)
my_cuts = cut(x,my_segments, labels = FALSE)
my_cuts[is.na(my_cuts)] = 0
This is the code:
#create subset of segments
z_alt = z
z_alt[my_cuts %% 2 == 0,] = NA
#plot green, then alternating segments in blue
plot(z, type="p", cex = 0.3,pch = 16,
col="black",
lwd=0.2,
frame.plot = F,
xaxt = 'n', # removes x labels,
ylim = c(0.3, 0.7),
las = 2,
xlim = c(0, 633744),
cex.lab=1.5, # size of axis labels
ann = FALSE, # remove axis titles
mgp = c(3, 0.7, 0))
lines(z_alt,col="red", lwd=0.2)
# adjust y axis label size
par(cex.axis= 1.2, tck=-0.03)
If you see, some black dots are separated, but other black dots have red connecting lines. Does anyone know how to remove these annoying lines?. I just want black and red dots. Many thanks
there is no need to call the points in a second function. you can try to directly set the color in the plot function using a color vector.
# create some data as you have not provided some
set.seed(123)
df <- data.frame(x=1:100,y=runif(100))
# some sgment breaks
my_segments <- c(0,10,20,50,60)
gr <- cut(df$x, my_segments,labels = FALSE, right = T)
gr[is.na(gr)] <- 0
# create color vector with 1 == black, and 2 == red
df$color <- ifelse(gr %% 2 == 0, 1, 2)
# and the plot
plot(df$x, df$y, col = df$color, pch = 16)
The problem here is that you are using lines to add your z_alt. As the name of the function suggests, you will be adding lines. Use points instead.
z <- runif(20,0,1)
z_alt <- runif(20,0.8,1.2)
plot(z, type="p", col="black", pch = 16, lwd=0.2, ylim = c(0,1.4))
points(z_alt, col = "red", pch = 16, lwd = 0.2)

Is is possible to add a title to scatter.smooth?

I use often scatter.smooth function but I wonder if it is possible to add a title or main argument directly to this function. I read the description of the function but have not found the possibility. I know that there are other ways to do this but I want to keep this one if possible.
d <- data.frame(x = sample(20, 500, prob=c(1:10, 10:1), replace = TRUE),
y = sample(20, 500, prob=c(1:10, 10:1), replace = TRUE),
z = rnorm(500, 20, 4))
mo <- lm(y ~ z, d)
fig <- function(x) {
scatter.smooth(fitted(x), residuals(x, type = "response"), col = "red")
abline(0, 0, lty = 2)
legend("topright", legend = c("loss", "0-0"), lty = c(1, 2))
}
fig(mo)
You look at the help page of scatter.smooth, you see that the ... argument is passed on to plot. Therefore, you can us any argument that plot accepts. Also main=.
You can also add a title to any graph using mtext which adds text to the figure margins.
So, you can do:
fig(mo)
mtext("My title", side=3, line=1)
Or modify your fig function:
fig <- function(x, ...) {
scatter.smooth(fitted(x), residuals(x, type = "response"),
col = "red", ...)
abline(0, 0, lty = 2)
legend("topright", legend = c("loss", "0-0"), lty = c(1, 2))
}
fig(mo, main="My title")
Just add main to the smooth function:
scatter.smooth(x, y, ylab = "Yname", xlab = "Xname", main = "Title")
It works

Line Graph Overlaying bar graph in base r

I have written the following code below. I would like to overlay a bar graph with a line graph. The code I have does it all but with just one problem. I would like the points on the line graph to be in the center of the bar graph, i.e. they should shift to the left a little bit. where Im I missing it? If this can be done in ggplot as well I would be happy too. but even base r would do
par(mai = c ( 1 , 2, 1, 1), omi = c(0, 0, 0, 0))
yy <- c(31,31,31,50,50,61,69,75,80,88,94,101,108,115,121,124,125,125,125,126,127)
name1 <- c ("15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35")
xx <- barplot(yy, ylab = "", names.arg = name1, ylim = c(0, 140),col="steelblue")
text(xx, yy + 3, labels = as.character(yy),srt=45)
mtext(2,text="",line=2)
par(new = T)
xx2 <- c(15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35)
yy2 <- c(379,474,579,725,922,1181,1473,1846,2316,2962,3688,4786,6069,7605,9504,10680,11074,11074,11074,11483,11484)
plot(xx2, yy2, xlim = c(14, 36), ylim = c(0, 14000),type ="n" , axes = F, xlab ="", ylab ="",col="blue",main="")
lines(xx2, yy2, lwd = 2,col="red",lty=1)
points(xx2, yy2, pch = 18, cex = 1,col="red")
text(xx2, yy2 + 4 , labels = as.character(yy2),srt=90)
par(new = T)
par(mai = c ( 1 , 1, 1, 1))
axis(2)
mtext(2,text="",line=2.5)
mtext("",side=1,col="black",line=2)
grid()
It can be quote tricky to get things to line up if you use barplot and a standard plot(). I recommend only calling plot once. In order to do this, you will need to rescale your yy2 values to the same scale as yy. Here's how you might do that
par(mai = c ( 1 , 2, 1, 1), omi = c(0, 0, 0, 0))
yy <- c(31,31,31,50,50,61,69,75,80,88,94,101,108,115,121,124,125,125,125,126,127)
name1 <- c ("15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35")
#draw bar plot
xx <- barplot(yy, ylab = "", names.arg = name1, ylim = c(0, 140),col="steelblue")
text(xx, yy + 3, labels = as.character(yy),srt=45)
mtext(2,text="",line=2)
xx2 <- xx #c(15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35)
yy2 <- c(379,474,579,725,922,1181,1473,1846,2316,2962,3688,4786,6069,7605,9504,10680,11074,11074,11074,11483,11484)
#transform data
yy2tx <- yy2/14000 * max(pretty(yy))
#draw line data
lines(xx2, yy2tx, lwd = 2,col="red",lty=1)
points(xx2, yy2tx, pch = 18, cex = 1,col="red")
text(xx2, yy2tx, labels = as.character(yy2),srt=90)
#draw axis for transformed data
par(mai = c ( 1 , 1, 1, 1))
axis(2, at=pretty(c(0,14000))/14000*max(pretty(yy)), labels=pretty(c(0,14000)))
grid()
This produces the following plot
The problem is that you have different x scale due to the different margins of the two plots.
Unless you want to find xx2 by hand... another solution to consider is to use a right y axis instead.
yy <- c(31,31,31,50,50,61,69,75,80,88,94,101,108,115,121,124,125,125,125,126,127)
name1 <- c ("15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35")
xx <- barplot(yy, ylab = "", names.arg = name1, ylim = c(0, 140),col="steelblue")
text(xx, yy + 3, labels = as.character(yy),srt=45)
mtext(2,text="",line=2)
par(new = T)
yy2 <- c(379,474,579,725,922,1181,1473,1846,2316,2962,3688,4786,6069,7605,9504,10680,11074,11074,11074,11483,11484)
plot(xx+0.5, yy2, "l", lwd = 2,col="red",lty=1,
axes=F, ylim=c(0, 14000), xlim=c(min(xx), max(xx)+1))
points(xx+0.5, yy2, pch = 18, cex = 1,col="red")
axis(4)
text(xx+0.5, yy2 + 4 , labels = as.character(yy2),srt=90)

How to change color in plot and the table of labels in plot?

plot(donnees.test$y,esvr1.pred,xlab = "Predicted Vlues", ylab = "Actual Values",type="p", yaxs="i",ylim=c(2,18),xaxs="i",xlim=c(2,18))
points(donnees.test$y,esvr1.pred, col=1,pch =19)
points(donnees.test$y,esvr1.pred, col=2,pch =20)
points(donnees.test$y,ANFIS, col=6,pch =3)
points(donnees.test$y,NN, col=4,pch =4)
#points(donnees.test$y,esvr1.pred, col=3)
abline(a = 0, b = 1, col = 3)
abline(a = 0, b = 1.25, col = 2)
text(9,14, "+25% Line", col = 2, adj = c(-.1, -.1))
abline(a = 0, b = 0.75, col = 2)
text(14.2,10, "-25% Line", col = 2, adj = c(-.1, -.1))
leg.txt <- c("SVR_rbf", "SVR_poly","ANFIS","NN")
legend(list(x = 2,y = 17.95), legend = leg.txt, col = 1:6, pch = c(19,20,3,4))
I changed the color of ANFIS that i want but in the table of labels the color of ANFIS did not changed. how should i changed it?
And if i wanna to add some text above of the plot in the plot tab what code i should add to my source?
To add a title to your plot, use the argument
main="the title here"
inside your plot command, or if you're adding it afterwards, use
title("the title here").
Your problem with the colours in your legend is that you have four items in your legend:
("SVR_rbf", "SVR_poly","ANFIS","NN"),
but you are giving the plot command six colours:
col = 1:6.
Try doing
col=1:4
instead.

Resources