Reputation: 23
When I plot a sequence in a line plot in R using transparent color settings I keep getting these dots on the line. Do you have an idea why this is and how I could solve the problem?
s <-seq(1,100, by = 0.1)
plot(s, type="l",col=scales::alpha("black",0.3), lwd=3)
I tried using the lines function which resulted in the same problem.
Also, it is interesting to see how the the dots change with e.g. s <-seq(1,100, by = 0.5)
and disappear with s <-seq(1,100, by = 1)
sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS 14.3.1
Thank you for your answers.
Upvotes: 2
Views: 193
Reputation: 5776
It is the line end style that is at play here.
The "line"-plot is drawn as a series of individual lines:
(0,0) -> (1, 0.1)
(1, 0.1) -> (2, 0.2)
(2, 0.2) -> (3, 0.3)
...
At each end of the line, the default is to draw a "rounded line cap". You can see this at each extreme of the line. Two overlapping rounded caps, give the illusion of a small circle (or point).
These points are visible, because of your partial transparency.
A solution could be to try the two other options "butt" or "square":
plot(s, type="l",col=scales::alpha("black",0.3), lwd=3, lend=1)
plot(s, type="l",col=scales::alpha("black",0.3), lwd=3, lend=2)
See the lend
parameter in ?par
.
Upvotes: 4
Reputation: 73562
You can set value v=0
in rainbow
. No need for scales::alpha
package or customizing a function (if it's just black).
> plot(s, type="l", col=rainbow(n=1, v=0, alpha=.3), lwd=3)
Note: I am on Linux.
Data:
s <- seq(1, 100, by=0.1)
Upvotes: 0
Reputation: 44957
This looks like a bug in some graphics drivers, but not others. In MacOS I see it if I use quartz()
, pdf()
, png()
or jpeg()
devices. I don't see it in X11()
, the default device in RStudio, or the devices in the ragg
package.
So the solution is to use a device that doesn't have the bug.
Upvotes: 4