Add pch symbol in R plot legend - r

I have one time series which is represented by a black line and one which is represented by a red curve. Then I have single points which have the pch symbol of 8 in R. These are stars. See the following plot:
Currently I have the following legend:
legend("bottomleft",
legend=c("log loss","daily VaR","exceedance"),
bty = "n",lwd=2, cex=1.2,y.intersp=1.4, col=c("black","red","blue"), lty=c(1,1,1))
But I don't want to have a blue line in the legend for exceedance, but just the stars in the plot. I have to use the pch=8. I just want to have the stars in the legend, not the stars with a line. So not these solutions: R legend issue, symbols of points are masked by lines

Try this. You set lty to display only first two lines, and pch to display only the last point.
plot(1:10, rnorm(10) * 1:10)
legend("bottomleft", legend = c("entry1", "entry2", "something cpl different"), bty = "n",
lwd = 2, cex = 1.2, col = c("black", "blue", "red"), lty = c(1, 1, NA), pch = c(NA, NA, 8))

Related

Plot in R not recognizing pch numbers

I am trying to plot in base R with the regular plot() fcn. However, when passing a vector of which pch to use, it will not plot the pch, it will only plot the number '1' instead of the shape of the pch I am calling.
Generating some data (my real data has over 400 rows for both the loads and meta objects:
loads <- data.frame(PC1 = c(11.32, 13.18, 12.82, 24.70), PC2 = c(-23.05, -24.71, -20.28, 10.09))
row.names(loads) <- c("100_A", "100_B", "100_C", "100_Orig")
meta <- data.frame(pch = c(17, 17, 17, 16), color = c("red", "red", "blue", "blue"))
row.names(meta) <- row.names(loads)
To plot:
x <- loads[, 1] ; y <- loads[, 2]
pch <- meta$pch
col <- meta$color
plot(x, y,
col = col, pch = pch, cex = 2, lwd = 4,
xlab = paste("PC1"), ylab = paste("PC2"))
Now, this will graph the correct color (red and blue) in the order I have them in the vector; the real issue becomes the plotting the pch. Instead of a circle (pch = 16) or a triangle (pch = 17) it's plotting a red or blue number 1 instead! I have included a pic of what my data is actually doing.
Thinking that the pch vector I am passing cannot have quotes around it, I have removed the quotes with the following code:
pch <- meta$pch
pch <-as.vector(noquote(pch))
class(pch)
[1] "character"
However, this generates the same results (getting a number 1 plotted). Interestingly, when use this code, it works fine. It turns all my colors to blue, and I get nice blue circles.
plot(x, y,
col = "blue, pch = 16, cex = 2, lwd = 4,
xlab = paste("PC1"), ylab = paste("PC2"))
This tells me that the plot function isn't recognizing my long vector composed of pch 16 and 17's mixed in.
Alternatively, if I use the rep function to generate my pch vector, a test shows it works fine. But I have over 400 rows. I cannot manually type rep for each pch. I will be here for eternity typing that out.
Any suggestions on what to do?????
Try defining the col as character and the pch as numeric like this:
plot(x, y,
col = as.character(col), pch = as.numeric(pch), cex = 2, lwd = 4,
xlab = paste("PC1"), ylab = paste("PC2"))

Plot with colored data points

Does anyone know how I can specify in the legend of my plot the colored data points?
with(data, plot(Math,FamilySize, pch=21, bg = StudyTime, cex=0.8))
legend('topright', pch = 19, legend = c(1,2,3,4), cex = 1, col = data$StudyTime)
The values in the category are from 1-4 and the data points should be in four different colors. However, I am getting an incorrect legend because it does not equal to the colors in my points.

how to adjust the position of color legends in heatmap.2

I am trying to do an unbiased Hierarchical clustering with the following code
col.cell <- c("purple","orange", "green", "blue")[sampleinf$subtype]
heatmap.2(as.matrix(hm3),col=rev(morecols(50)),trace="none", main="Top 500 most variable genes across samples",ColSideColors=col.cell,scale="row")
legend("topright",
legend = unique(sampleinf$subtype),
col = col.cell,
lty= 1.5, lwd = 2,
cex=.6)
How can i make the color legend more visible. Now its superimposing with the dendrogram.
To plot outside the range of 0 to 1, you need to use par(xpd=TRUE)
legend(x = #,y= #, xpd = TRUE, legend("topright",
legend = unique(sampleinf$subtype),
col = col.cell,
lty= 1.5, lwd = 2,
cex=.6)

How to make R legend with 2 columns?

I want to make a legend on my graph, which is generated by plot() function. The original legend() function will generate a list which has only 1 column. How can I make a legend which has 2 columns?
I could not find a way to do that within a single call to legend for standard plots.
Here's an option, drawing two separate legends: one with lines and points, one with labels. x.intersp can be used to tweak distance between labels and lines.
plot(cumsum(runif(n = 100)))
# draw legend with lines and point but without labels and box. x.intersp controls horizontal distance between lines
L = legend(x = 'bottom', legend = rep(NA,4), col=1:2, lty=c(1,1,2,2), ncol=2, bty='n', x.intersp=0.5, pch=c(1,2,1,2), inset=0.02)
# use position data of previous legend to draw legend with invisble lines and points but with labels and box. x.intersp controls distance between lines and labels
legend(x = L$rect$left, y = L$rect$top, legend = c('Group A', 'Group B'), col=rep(NA,2), lty=c(1,1), ncol=1, x.intersp = 3, bg = NA)
Check this:
library(lattice)
myPCH <- 15:17
Data <- rnorm(50)
Index <- seq(length(Data))
xyplot(Data ~ Index,
pch = myPCH, col=1:2,
key = list(space = "right", adj=1,
text = list(c("a", "b", "c"), cex=1.5),
points = list(pch = myPCH),
points = list(pch = myPCH,col=2)))
It looks like Victorp answered this in the comments of the original post. The ncol argument in the legend function works for me:
legend(locator(1), legend=c("name1","name2", "name3", "name4"), lty=2, col=c("black", "blue", "dark green", "orange"), ncol=2)
enter image description here

Plot option 'bg' not working

I have a very basic piece of R code here, as well as the plot it produces. Why is the dot not filled in red?
plot(1, 1, col="blue", bg="red", cex=4)
You need to use one of pch = 21 through 25 for bg to work
plot(1, 1, col = "blue", pch = 21, bg = "red", cex = 4)
From the points help file:
bg
background (fill) color for the open plot symbols given by pch = 21:25.

Resources