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])
}
Related
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))
I'm working with starwars dataset (dplyr package), and I want to make a graph where the independent variable is the height of the characters, and the dependent variable is their body mass. Furthermore, I also want to discern species by colors:
library(dplyr)
starwars
par(mar = c(5.3, 4.3, 4.3, 8.3), xpd = TRUE)
plot(starwars$mass ~ starwars$height, ylim = c(0, 200),
col = as.factor(starwars$species), bty = "l")
legend("topright", inset = c(-0.2, 0), legend = as.factor(starwars$species),
cex = 0.50, col = as.factor(starwars$species),
ncol = 2, pch = 16)
Figure
Note that some species are repeated in the legend. How to exclude these repetitions?
Include only the unique values in legend :
plot(starwars$mass ~ starwars$height, ylim = c(0, 200),
col = as.factor(starwars$species), bty = "l")
legend("topright", inset = c(-0.2, 0),
legend = as.factor(unique(starwars$species)),
cex = 0.50, col = as.factor(unique(starwars$species)),
ncol = 2, pch = 16)
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)
)
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:
So I've created a barplot for some data I have with the following code:
plot <- barplot(data,
beside = TRUE,
col = c("red", "blue", "green"),
space = c(0, 0.4),
width = 0.2,
xlim = c(0, 2),
ylim = c(0, 1.1),
legend = c("KNN", "MF1", "MF2"),
args.legend = list(x = 2.7, y = 1.2),
yaxt = 'n',
xpd = TRUE,
srt = 90
)
text(x = plot,
y = data + 0.05,
labels = as.character(round(data, digits = 2)),
srt = 90,
xpd = TRUE
)
I think the plot looks neat, but... It does not fit the region (I think at least). Are there any ways to keep the width of the bars in picture 1 and still show all 8 groups? My solution so far is to reduce the width of the bars as shown in picture 2.
Picture 1
Picture 2