Plot with colored data points - r

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.

Related

Bar Plot : Legend and Axis issues

I am trying to Plot the K-S Test result. Basically b\w the Significant and Insignificant variables. I need to Plot the legend, and the axis values in visible manner. If you can see the below plot, the Y- Axis values are not visible. Also the graph's Legend displays a dotted line. I need a solid squares or circle for Legend.
barplot(matrix, main="KS-Significance Test",
xlab="Proportion b/w InSig vs Sig",
ylab = 'Combinations',
yaxt="n",ylim=c(0,5),
cex.axis=0.8,cex.lab = 0.8,font = 2, horiz=TRUE,
col=c("darkgreen","darkred"))
legend('topright',legend= c("Insignificant","Significant"),
col= c("darkgreen","darkred"), lty = c(11,11),cex=0.8))
Please find the matrix data below by clicking the hyperlink.
input data matrix image
Also attaching the image of my barplot.
Use fill instead of col and do not specify a line type lty if you do not want lines but box shapes -- i.e. wider lines that match your barplots :)
legend('topright',legend= c("Insignificant","Significant"),
fill = c("darkgreen","darkred"), cex=0.8)
Otherwise, specify a line width lwd, here I use a solid line lty=1 and lwd=10 to get darker and wider lines.
legend('topright',legend= c("Insignificant","Significant"),
col = c("darkgreen","darkred"), lty = 1, lwd = 10, cex = 0.8)
Thanks Everyone. I have found the answer. Legend issues are also resolved.
here is the code.
barplot(matrix, main="KS-Significance Test",
xlab="Proportion b/w InSig vs Sig",
ylab = 'Combinations',
ylim=c(0,270),
cex.axis=0.8,cex.lab = 0.8,font = 4,
names.arg = c("Cohort 2014-2015", "Cohort 2014-2016", "Cohort 2015-2016","Cohort 2015-2017","Cohort 2016-2017"),
#horiz=TRUE,
col=c("darkgreen","darkred"),
legend = c("Insignificant","Significant"))
This completely satisfies my plot.

Calculating the appropriate inset value for legends automatically

Is it possible to obtain the appropriate value of inset automatically so that the left corner of legend will always be just outside of the top right corner of plot?
In the plot below I had to try several values for inset manually. It would be nice to not have to do it manually since I have to make multiple plots.
graphics.off()
windows(width = 5, height = 5)
set.seed(42)
par(mar = c(5,5,1,10))
plot(rnorm(50,15,5), rnorm(50,15,3),
xlim = c(0,30), ylim = c(5,25),
pch = 19, col = c("red","blue"))
par(xpd = TRUE)
legend("topright", inset = c(-.80, 0),
pch = 19, col = c("red","blue"),
legend = c("LEGEND 1","Second Legend"))
After the plot call, before adding the legend, use par("usr")* to extract the coordinates of the plotting region.
Then, instead of positioning the legend using a 'keyword' and inset, use x and y with the top-right coordinates of the plotting region obtained from par("usr"). Adjust x with a suitable coefficient.
coord <- par("usr")
legend(x = coord[2] * 1.05, y = coord[4],
pch = 19, col = c("red", "blue"),
legend = c("LEGEND 1", "Second Legend"))
And just for fun, a more convoluted alternative.
After plotting, call legend with position topright, but without plotting it to the device (plot = FALSE), and assign it to an object.
Extract the left x and the top y coordinate of the legend box, and its width (see Value section in ?legend), to be used in x and y in legend:
leg <- legend("topright", pch = 19, col = c("red", "blue"),
legend = c("LEGEND 1", "Second Legend"),
plot = FALSE)
legend(x = (leg$rect$left + leg$rect$w) * 1.05, y = leg$rect$top,
pch = 19, col = c("red", "blue"),
legend = c("LEGEND 1", "Second Legend"))
*From ?par
usr A vector of the form c(x1, x2, y1, y2) giving the extremes of the user coordinates of the plotting region.
The position calculations that are made when inset parameter(s) have been specified, are in fact based on par("usr") (see line 188-199 in the legend code).

Points Scale in R barplot [duplicate]

This question already has answers here:
How can I plot with 2 different y-axes?
(6 answers)
Closed 6 years ago.
i'm having troubles in a multi axis barplot. I have an X,Y axis with bars and dots in the same graph. The point is that I have to shown both of them in different scales
While I can shown both (bars and dots) correctly, the problem comes when I try to set different scales in left and right axis. I dont know how to change the aditional axis scale, and how to bind the red dots to the right axis, and the bars to the left one.
This is my code and what I get:
labels <- value
mp <- barplot(height = churn, main = title, ylab = "% churn", space = 0, ylim = c(0,5))
text(mp, par("usr")[3], labels = labels, srt = 45, adj = c(1.1,1.1), xpd = TRUE, cex=.9)
# Population dots
points(popul, col="red", bg="red", pch=21, cex=1.5)
# Churn Mean
media <- mean(churn)
abline(h=media, col = "black", lty=2)
# Population scale
axis(side = 4, col= "red")
ylim= c(0,50)
ylim= c(0,5)
What I want is to have left(grey) axis at ylim=c(0,5) with the bars bound to that axis. And the right(red) axis at ylim=c(0,50) with the dots bound to that axis...
The goal is to represent bars and points in the same graph with diferent axis.
Hope I explained myself succesfully.
Thanks for your assistance!
Here is a toy example. The only "trick" is to store the x locations of the bar centers and the limits of the x axis when creating the barplot, so that you can overlay a plot with the same x axis and add your points over the centers of the bars. The xaxs = "i" in the call to plot.window indicates to use the exact values given rather than expanding by a constant (the default behavior).
set.seed(1234)
dat1 <- sample(10, 5)
dat2 <- sample(50, 5)
par(mar = c(2, 4, 2, 4))
cntrs <- barplot(dat1)
xlim0 <- par()$usr[1:2]
par(new = TRUE)
plot.new()
plot.window(xlim = xlim0, ylim = c(0, 50), xaxs = "i")
points(dat2 ~ cntrs, col = "darkred")
axis(side = 4, col = "darkred")

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

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