Legend using Pound "#" plus pch 15 [duplicate] - r

This question already has answers here:
R legend pch mix of character and numeric
(5 answers)
Closed 3 years ago.
Stuck getting my simple legend to print the proper symbols, the first needs to be a square which is pch 15, the second needs to be "#"
[square] y=x^2
[hashtag] y=1-x^2
legend("topright", legend=c("y=x^2", "y=1-x^2"),
col=c("red", "blue"),cex=1,pch=c(15,"#"))
Now what is happening the legend sees 1 then 5 where it should be a square & the hashtag, what am I doing wrong?

An approach would be to repeat the legend two times while changing one of them to missing value (NA)
plot(1, 1)
sapply(list(c(15, NA), c(NA, "#")), function(x)
legend("topright", legend = c("y=x^2", "y=1-x^2"),
col=c("red", "blue"),cex=1, pch = x))

Related

2 vectors in one plot [duplicate]

This question already has answers here:
Plotting multiple curves same graph and same scale
(5 answers)
Closed 4 years ago.
I spent a long time trying to figure something out which I thought would be very easy. I have three vectors (or a data frame if you want to make it into one)
date <- c("Q1","Q2","Q3","Q4")
group1 <- c(12,13,16,11)
group2 <- c(9,11,10,9)
Now I want to create one graph with the date along the x-axis, and two horizontal lines representing the 2 groups. For a bit of context, I did a difference-in-difference regression and want to show the average values for treatment and control group around the event. I'm using panel data and already calculated the mean for both groups at each point in time. Here is a sceenshot I took from my so you can see how I want it to look like.
# plot solid line, set plot size, but omit axes
plot(x=seq(date), y=group1, type="l", lty=1, ylim=c(5,20),
axes=F, bty="n", xaxs="i", yaxs="i", main="My Title",
xlab="", ylab="Total Risk-Based Capital Ratio")
# plot dashed line
lines(x=seq(date), y=group2, lty=2)
# add axes
axis(side=1, labels=date, at=seq(date))
axis(side=2, at=seq(5,20,3), las=1)
# add vertical red line
abline(v=2, col="red")
# add legend
par(xpd=TRUE)
legend(x=1.5, y=2, legend=c("solid", "dashed"), lty=1:2, box.lty=0, ncol=2)

Reducing Legend Size in R matplot [duplicate]

This question already has an answer here:
Reduce size of legend area in barplot
(1 answer)
Closed 4 years ago.
I was trying to create a chart using below code in R:
myplot<-function(data,rows,Colm){
Data<-data[rows,Colm,drop=F]
matplot(t(Data), type = "b", pch=15:18, col = c(1:4, 6))
legend("bottomleft",inset = 0.01, legend = Players[rows],col = c(1:4, 6),
pch=15:18, horiz = F)
}
myplot(Games)
and got the below plot:enter image description here
Can anyone help me how to reduce the legend size?
The answer can be found here: Reduce size of legend area in barplot
The key is the cex argument in the legend function call.
The default is the equivalent of 1. Larger than the default size is greater than 1. From your question, you probably want to put the value as .5 and experiment from there until it is the size you want.

Shading a region under a PDF [duplicate]

This question already has answers here:
Shading a kernel density plot between two points.
(5 answers)
Closed 5 years ago.
I've used the following to plot a Weibull distribution W(2,3):
Weibull=function(x){
(2*x/9)*exp(-(x^2)/9)
}
xx=seq(from=0,to=10,length.out=1000)
xx1=Weibull(xx)
plot(xx,xx1,type="l")
polygon(xx, xx1, col = "red", border = NA)
PDF plot
I want to shade the region between (4,10) only. How would I go about doing this?
xx2=seq(from=4,to=10,length.out=1000)
yy2=c(Weibull(xx2),0)
xx2=c(xx2,4)
plot(xx,xx1,type="l")
polygon(xx2, yy2, col = "red", border = NA)

R histogram axes too small for the dataset [duplicate]

This question already has an answer here:
R - Customizing X Axis Values in Histogram
(1 answer)
Closed 7 years ago.
I'm learning R and I'm trying out the hist() histogram function. My code is here(and pasted below), and when I run it, the axes A) Do not connect at the origin, and B) they don't extend far enough for the dataset. I've looked and haven't found anything, the properties xlim, ylim, axes=FALSE, none of these solutions work.
bluegill = read.table(file="lab2.csv", header="true", sep=",")
attach(bluegill)
hist(Length, main="", xlab="Length (mm)", ylab="Number of individuals", col="gray")
and then this is the resulting chart, the max length is 220 in the data set, and the x axis only goes to 200.
A simple solution could be this:
bluegill = read.table(file="lab2.csv", header="true", sep=",")
attach(bluegill)
hist(Length, main="", xlab="Length (mm)", ylab="Number of individuals", col="gray", xaxt = "n") ##no x axis
Please notice the new option that has been added, xaxt = "n", which removes the x axis completely. You could then add the x axis late with another command. e.g.
axis(1, at = seq(0, 200, 20))
The first option is 1, which means x axis.
The second option stands for the points that will be shown in the plot (excuse my English).

Re-labelling graph columns in R? [duplicate]

This question already has an answer here:
How to remove default axis from the plot produced by boxplot()?
(1 answer)
Closed 8 years ago.
I'm trying to change the names of the column labels on the x axis of a boxplot.
This is my code for the graph:
boxplot(datab~area,ylab="Heather Cover (%)",xlab="Survey Area")
axis(1, at=1:3, lab=c("Hangars", "East", "Runway"))
This works, however, the old column names (1,2,3) remain underneath the new labels. How do I get rid of these?
One thing you can do is remove the x-axis labels using xaxt="n" in the original plot. Let me illustrate using the "mtcar" data.
boxplot(mpg~gear, data=mtcars, xaxt="n")
axis(1, at=1:3, lab=c("Hangars", "East", "Runway"))

Resources