How to plot filled boxed around line and point in legend? - r

Some data
dummy.dt <- data.frame(c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1000))
plot(dummy.dt, type="n", xlab="x" , ylab="y", xaxt = "n", log = "y")
legend("top", inset=.02, title="legend",
c("1", "2"),
pch = c(1, 1),
lty = c(1, 1),
col=c("blue", "orange"),
fill=c(rgb(red = 200, green = 200, blue = 200, maxColorValue = 255, alpha = 50), rgb(red = 100, green = 100, blue = 0, maxColorValue = 100, alpha = 50))
)
The result looks like this:
The result I´d like to receive would be a filled box all around the lines.
How to manipulate the size of the filled box that is created by fill?

In the first alternative, we keep the original length of the line segments and the corresponding 'long boxes' are made from lines. Create one legend with thick lines (the 'boxes'). Add a second legend on top with the thin lines and points:
plot(1)
# "filled boxes" made of lines
legend("top", inset = 0.02, legend = 1:2, title = "legend",
lty = 1, lwd = 10, box.col = "white",
pch = NA,
col = c("grey90", "yellow"))
# lines and points
legend("top", inset = 0.02, legend = 1:2, title = "legend",
lty = 1, lwd = 1, bty = "n",
pch = 1,
col = c("blue", "orange"))
A second possibility is to decrease the length of the line segments using seg.len. Then boxes are made from points. Please note that we need to specify the same lwd and seg.len in both legend calls, i.e. also in the call for the 'boxes' where lty = 0.
plot(1)
# "filled boxes" made of points
legend("top", inset = 0.02, legend = 1:2, title = "legend",
lty = 0, lwd = 1, seg.len = 1,
pch = 15, pt.cex = 2,
col = c("grey90", "yellow"))
# lines & points
legend("top", inset = 0.02, legend = 1:2, title = "legend",
lty = 1, lwd = 1, seg.len = 1,
pch = 1, bty = "n",
col = c("blue", "orange"))

It's not clear why you need fillat all. If you leave it out you will get only the lines plus the point character distinguished by color:
legend("top", inset=0.2, title="legend",
c("1", "2"),
pch = c(1, 1),
lty = c(1, 1),
col=c("blue", "orange")
# fill=c(rgb(red = 200, green = 200, blue = 200, maxColorValue = 255, alpha = 50),
# rgb(red = 100, green = 100, blue = 0, maxColorValue = 100, alpha = 50)
)

Related

How can I add axis labels to a plot with custom axes?

I want to add axes labels to the plot generated by the following code. Specifically, I would want an x-axis label that reads "Fibril Diameter (nm)" and a y-axis label that reads "Density". Any idea how I could accomplish this? Thanks!
den1 = density(CDE1$V1)
den2 = density(CDE1$V2)
col1 = hsv(h = 0.65, s = 0.6, v = 0.8, alpha = 0.5)
col2 = hsv(h = 0.85, s = 0.6, v = 0.8, alpha = 0.5)
op = par(mar = c(3, 3, 2, 2))
plot.new( )
plot.window(xlim = c(25,65), ylim = c(0, 0.14))
axis(side = 1, pos = 0, at = seq(from = 25, to = 65, by = 5), col = "gray20",
lwd.ticks = 0.25, cex.axis = 1, col.axis = "gray20", lwd = 1.5)
axis(side = 2, pos = 25, at = seq(from = 0, to = 0.14, by = 0.02), col = "gray20",
las = 2, lwd.ticks = 0.5, cex.axis = 1, col.axis = "gray20", lwd = 1.5)
polygon(den1$x, den1$y, col = col1, border ="black",lwd = 2)
polygon(den2$x, den2$y, col = col2, border ="black",lwd = 2)
text(52, 0.10, labels ="CDET", col =col1, cex = 1.25,font=2)
text(35, 0.03, labels ="SDFT", col =col2, cex = 1.25,font=2)
par(op)
title(main="Gestational Day 100/283")
Here's a picture of what the code generates so far...
Image
You can add your axis labels using title()
title(main="Gestational Day 100/283",
xlab="Fibril Diameter (nm)",
ylab="Density")
or with mtext() which will make it easier for you to fine-tune their exact positioning:
mtext("Fibril Diameter (nm)", side=1, line=2)
mtext("Density", side=2, line=2)
In either case, you will probably need to increase your bottom and left plot margins so that the labels are actually visible, e.g. like this:
op = par(mar=c(4.5, 4.5, 2, 2))

Space in legend between symbol and text in R

Is the a way to reduce the default space between a symbol and the related text in legend in R? I only found how to change space between legend items but not between symbol and text.
par(lwd=1,mai=c(0,0,0,0))
plot.new()
legend(x="left", inset =0,
c("Simulated by the model"),
lty=c(1,NA),pch=c(NA,"o"),lwd=c(2,3), col=c("black","red"), box.col=NA,horiz=TRUE,cex=1.5,text.width = c(0.3,0.3))
We can use parameter x.intersp inside legend to adjust the amount of white-space between legend symbol and text.
Here are a some examples:
par(lwd = 1, mai = c(0,0,0,0), mfrow = c(2, 2))
prm <- c(0.1, 1.0, 2.0, 3.0)
for (i in 1:length(prm)) {
plot.new()
legend(
x = "left",
inset = 0,
c(sprintf("x.intersp = %2.1f", prm[i])),
lty = c(1, NA),
pch = c(NA, "o"),
lwd=c(2, 3),
col = c("black", "red"),
box.col = NA,
horiz = TRUE,
cex = 1.5,
text.width = c(0.3, 0.3),
x.intersp = prm[i])
}

How to remove the zero labels in Histogram plot in R?

Any tips to remove the zero labels in between the histogram bars?
hist(links$Survey_Duration, breaks = seq(0,50,5), main = "Survey Duration",
labels = TRUE, border = "black",
xlab = "Survey", ylim = c(0, 15), col = "gray", las = 1, xaxt='n')
axis(side=1, at=seq(0,50,5), labels=seq(0,50,5))
abline(v = mean(links$Survey_Duration), col = "royalblue", lwd = 1.5)
abline(v = median(links$Survey_Duration), col = "red", lwd = 1.5)
legend(x = "topright", c("Mean", "Median"), col = c("royalblue","red"),
lwd = c(1.5,1.5))
How about this?
# modify data so there's zero in one of the bins
mtcars$mpg <- ifelse(mtcars$mpg >= 25 & mtcars$mpg <= 30, NA, mtcars$mpg)
# save plot parameters
h <- hist(mtcars$mpg, plot = FALSE)
# produce plot
plot(h, ylim = c(0, 14))
# add labels manually, recoding zeros to nothing
text(h$mids, h$counts + 1, ifelse(h$counts == 0, "", h$counts))
A slightly different answer using the labeling in hist instead of adding text afterwards.
You do not provide your data, so I will use some data that is handy to illustrate.
The labels argument can specify the individual labels
H1 = hist(iris$Sepal.Length, breaks = 3:8, plot=FALSE)
BarLabels = H1$counts
BarLabels[BarLabels == 0] = ""
hist(iris$Sepal.Length, breaks = 3:8, labels = BarLabels)
Thanks #Daniel Anderson, it Ok now (Thumbs Up)
links$Survey_Duration <- ifelse(links$Survey_Duration > 15 &
links$Survey_Duration <= 25,
NA,
links$Survey_Duration)
h <- hist(links$Survey_Duration, breaks = seq(0,50,5), plot = FALSE)
plot(h, ylim = c(0, 14), main = "Survey Duration", xlab = "Time", col = "gray", las = 1)
text(h$mids, h$counts + 1, ifelse(h$counts == 0, "", h$counts))
axis(side=1, at=seq(0,50,5), labels=seq(0,50,5))
abline(v = mean(links$Survey_Duration), col = "royalblue", lwd = 1.5)
abline(v = median(links$Survey_Duration), col = "red", lwd = 1.5)
legend(x = "topright",
c("Mean", "Median"),
col = c("royalblue","red"),
lwd = c(1.5,1.5))

R boxplot; center the axis labels under the tick marks

I plotted a dataframe (16700 obs. of 6 variables) using the following code:
labels <–c("X2137_Fe20","X2137_FeXS","vtc1_Fe20",
"vtc1_FeXS","VTC1c_Fe20","VTC1c_FeXS") #labels x axis
[1]col <- c("chartreuse3", "chocolate2", "chartreuse3", "chocolate2",
"chartreuse3", "chocolate2") #colors
#Plot
boxplot(CVtable,
outline = FALSE,
ylim = c(-0.5,70),
main="CV Replicas",
ylab="RSD(%)",
range = 1.5,
width = c(9,9,9,9,9,9),
plot = TRUE,
col = col,
par (mar = c(5,4.5,5,0.5) + 0.1),
par(cex.lab=2),
par(cex.axis=1.7),
notch = TRUE,
labels = labels)
dev.off()
This is the result
I like this box plot, but there are a couple of things I would like to adjust. I need to keep this font size for the x axis labels, but as you can see the labels are too big and part of them is missed. The solution is to rotate them 45 degrees, but I do not manage to find an easy code to insert in my script.
I tried to delete the original axes (axes=FALSE), then setting new ones by
boxplot(CVtable,
outline = FALSE,
ylim = c(0.5,70),
ylab="RSD(%)",
range = 1.5,
width = c(9,9,9,9,9,9),
plot = TRUE,
col = col,
par (mar = c(5,4.5,5,0.5) + 0.1),
notch = TRUE,
par(cex.lab=1.7),
axes=FALSE)
axis(1, at = c(1,2,3,4,5,6), labels = F, tick = 2, line = NA,
pos = -1, outer = F, font = 3, lty = "solid",
lwd = 2, lwd.ticks = 3, col = NULL, col.ticks = NULL,
hadj = NA, padj = 0)
axis(2, at = c(0,10,20,30,40,50,60,70) , labels =
c(0,10,20,30,40,50,60,70), tick = 2, line = NA,
pos = 0.5, outer = FALSE, font = 1, lty = "solid",
lwd = 2, lwd.ticks = 3, col = NULL, col.ticks = NULL,
hadj = NA, padj = 0, par(cex.lab=1.5))
text(x=c(1,2,3,4,5,6),
y=par()$usr[3]-0.1*(par()$usr[4]-par()$usr[3]),
labels=labels, srt=45, adj=1, xpd=TRUE, par(cex.lab=2))
and this is the output: img2
Well, I do not know how to center my labels under the tick marks and how to extend the x axis to the origin of the graph (left) and to the end of the last box (right). Moreover, the argument par(cex.lab=2) to fix the x axis labels font size seems no longer working in that string.
Any good suggestion?
PS: this is my 1st post, if any needed info is missed, please leave a comment and I will reply as soon as I can. Thank you!
I made it myself:
#RSD
boxplot(CVtable[1:6],
outline = FALSE,
ylim = c(0.5,70),
ylab="RSD(%)",
range = 1.5,
width = c(9,9,9,9,9,9),
plot = TRUE,
col = col,
par (mar = c(7,4.5,1,0.5) + 0.1),
notch = TRUE,
par(cex.lab=1.7),
axes=FALSE)
axis(1, at = c(0.5,1,2,3,4,5,6,7), labels = F, tick = 2, line = NA,
pos = -1, outer = F, font = 3, lty = "solid",
lwd = 2, lwd.ticks = 3, col = NULL, col.ticks = NULL,
hadj = NA, padj = 0)
axis(2, at = c(0,10,20,30,40,50,60,70) , labels =
c(0,10,20,30,40,50,60,70), tick = 2, line = NA,
pos = 0.5, outer = FALSE, font = 4, lty = "solid",
lwd = 2, lwd.ticks = 3, col = NULL, col.ticks = NULL,
hadj = NA, padj = 0, par(cex=1.4))
text(x=c(0.7,1.7,2.7,3.7,4.7,5.7),
y=par()$usr[3]-0.14*(par()$usr[4]-par()$usr[3]),
labels=labels, srt=45, adj=0.6, xpd=TRUE, cex=1, font=2)
dev.off()
This is the result:

Align plot with barplot in R

I want to display two plots with the same x-values above each other. But the plots don't align.
How can I align them?
Code:
dat <- data.frame(d = LETTERS[1:5], c = c(39, 371, 389, 378, 790), r = c(39,
332, 18, -11, 412))
par(mfrow=c(2,1))
plot(dat$c, type = "s", ylim = c(0, max(dat$c)), xlab = "", ylab = "", axes = FALSE, col = "#4572a7", lwd = 2)
axis(1, at = c(1:length(dat$c)), labels = dat$d, lty = 0)
axis(2, lty = 0, las = 1)
barplot(dat$r, names.arg = dat$d, col = "#008000", border = NA, axes = FALSE)
axis(2, lty = 0, las = 1)
abline(h = 0, col = "#bbbbbb")
We need to get the x-coordinates of the center of each bar and use those coordinates as the x-values of the first plot. We also need to set the same xlim values for each plot:
# Get x coordinates of center of each bar
pr = barplot(dat$r, names.arg = dat$d, col = "#008000", border = NA, axes = FALSE,
plot=FALSE)
par(mfrow=c(2,1))
# Apply the x coordinates we just calculated to both graphs and give both
# graphs the same xlim values
plot(pr, dat$c, type = "s", ylim = c(0, max(dat$c)), xlab = "", ylab = "", axes = FALSE,
col = "#4572a7", lwd = 2, xlim=range(pr) + c(-0.5,0.5))
axis(1, at = pr, labels = dat$d, lty = 0)
axis(2, lty = 0, las = 1)
barplot(dat$r, names.arg = dat$d, col = "#008000", border = NA, axes = FALSE,
xlim=range(pr) + c(-0.5,0.5))
axis(2, lty = 0, las = 1)

Resources