Plot option 'bg' not working - r

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.

Related

Linear function axis scaling in R

I have the following function
C=15
S=seq(0,12,.1)
pA=1
pS=0.5
A <- function(S) (C/pA)-(pA/pS)*S
plot (S, A(S), type="l", col="red", ylim=c(0,15), xlim=c(0,15), xlab="S", ylab="A")
text(8, 11.5, "Function", col = "red", cex=.9)
text(11, 10.2, expression(A==frac(C,p[S])-frac(p[A],p[S])%.%S), cex=.9, col = "red")
grid()
How can I scale the axis so that I can "zoom out" a bit. I am looking for a function similar to ylim but where I could say: ylim=c(0,15, by=1).
Update
It seems your "zoom out" is meant for the grid lines, not for the axis or anything. I'll keep the previous answer below, but this should address it, I think.
# no change yet
plot (S, A(S), type="l", col="red", ylim=c(0,15), xlim=c(0,15), xlab="S", ylab="A")
text(8, 11.5, "Function", col = "red", cex=.9)
text(11, 10.2, expression(A==frac(C,p[S])-frac(p[A],p[S])%.%S), cex=.9, col = "red")
# no call to grid, since we want to control it a little more precisely
abline(h = seq(0, 15), col = "lightgray", lty = "dotted", lwd = par("lwd"))
abline(v = seq(0, 15, by = 5), col = "lightgray", lty = "dotted", lwd = par("lwd"))
You can do just abline(h=seq(...)) and not set col=, lty=, or lwd=, but realize that abline's default values are different from grid's defaults. I used those values to mimic grid's look and feel.
Old answer
I think you mean to change the y axis labels, not the y axis limits (which is all that ylim= can effect).
Using base R graphics:
plot (S, A(S), type="l", col="red", ylim=c(0,15), xlim=c(0,15), xlab="S", ylab="A",
yaxt = "n") # this is new
text(8, 11.5, "Function", col = "red", cex=.9)
text(11, 10.2, expression(A==frac(C,p[S])-frac(p[A],p[S])%.%S), cex=.9, col = "red")
grid()
axis(2, at = 1:15, las = 2)
Explanation:
plot(..., yaxt = "n") means to not plot the y-axis ticks and labels, see ?par.
axis(...) adds an axis to a side. By itself, it will just add a default axis.
at= sets where the ticks and labels are placed.
labels= sets what to put at each tick. If absent (like it is above), it just prints the at value.
las=2 rotates the number so that it is perpendicular to the axis line. I did this to show more of the numbers, otherwise they will mask a bit more.
While this shows every-other, that is affected by the size of the canvas; if you do the same plot in full-screen, it'll show every number.

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

How change the background color of Unicodes Symbols in a R plot?

I am trying to change the background color of a unicode Symbol in a R base plot. Here an example:
plot(x, y, pch = -as.hexmode("2603"), main = "Corte 1", cex = 3, bg = "purple", col = "black")
I was trying to change the background color with the bg command, but it works only for "normal" pch symbols. Do you know if it is possible? Thanks
par(bg="purple")
plot(x, y, pch = -as.hexmode("2603"), main = "Corte 1", cex = 3, col = "black")
If you want more control, check this answer

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)

Add pch symbol in R plot legend

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

Resources