In R I often add legends to my plots like this
legend("topright",c("a=1","b=1"),lwd=c(1,2))
However, what I want to do is produce a plot which contains nothing but that legend. How do I do it? (Preferably without using package such as ggplot)
You can generate a new, empty plot frame using frame() or plot.new()
plot.new()
legend("topright",c("a=1","b=1"),lwd=c(1,2))
Use the type='n' parameter as in:
plot(x,y,type='n')
See ?plot.default for details. If you will want to add some text/points/lines to the plot afterward you may want to provide the x and y parameters, and/or the ylim and xlim parameters in order to set up the plotting region.
You can also drop the axes with the argument axes=F, and you can set the xlab,ylab, and main to NA, if you really want a blank plot.
Related
If you call function hist on r, you will note that the box that usually surrounds plotting region doesn't appear, instead, only rulers indicating plot scale appear on the bottom and on the left. If you use r a lot you may probably have noticed this already. my question is: there is some graphical parameter or workaround to make this happen on any other plot of basic r (like in a scatterplot, a line plot, a qq plot or whatever)?
The only parameter I found was axes, but setting it to FALSE makes it disappear not only the box, but also the rulers.
You are looking for box().
op <- par(mfrow=c(1, 2))
hist(mtcars$mpg, sub="w/o box")
hist(mtcars$mpg, sub="w/ box")
box() ## <-- this
par(op)
the answer is bty graphical parameter:
x= matrix(rnorm(100), ncol= 2)
plot(x, bty= 'n')
I'm plotting my data but the legend doesn't show up. I've included the legend command line and can't find any solution as I think the command is correct.
I use the data from stock market.
library(quantmod)
library(TTR)
tckrs<-c("GOOG","FB")
getSymbols(tckrs,from="2019-01-01")
AdG<-Ad(GOOG)
AdF<-Ad(FB)
DrG<-dailyReturn(AdG)[-1,]
DrF<-dailyReturn(AdF)[-1,]
portfolio<-cbind(DrG,DrF)
colnames(portfolio)
names(portfolio)[1]<-"DrG"
names(portfolio)[2]<-"DrF"
tsRainbow<-rainbow(ncol(portfolio))
plot(x=portfolio,ylab="Return",main="Portfolio Returns (Single)",col=tsRainbow,screens=1)
legend(x="bottom",legend=c("GOOG","FB"),lty=1,col=tsRainbow)
#I also tried
myColors<-c("red","darkgreen")
plot(x=portfolio[,"DrG"],xlab="Time",ylab="Stock Returns",main="Stock Returns",ylim=c(-0.15,0.1),major.ticks= "years",minor.ticks=FALSE,col="red")
lines(x=portfolio[,"DrF"],col="darkgreen")
legend(x="bottom",legend=c("DrG","DrF"),lty=1,col=myColors)
The plot does come out but there is no legend at all from those two commands.
The problem is that the xts package does strange things in its plots. You are supposed to call addLegend instead of legend. For your example, replace the legend call with
addLegend("bottom", c("GOOG", "FB"), lty=1, lwd=c(2,1),
col = c("red", "darkgreen"), bty="o")
I'll start with a simple example. Code below results with boxplot with y-axis labels not covering the whole range of data.
x<-c(0.5, 1:5, 5.5)
boxplot(x)
I can easily fix that with ylim:
boxplot(x, ylim=range(pretty(x)))
Is there a way to make this way of calculating ylim default for all the plot functions in basic R (like barplot(), plot() etc.)? Maybe some clever par() or options() assigment?
Thanks for any help!
I try to use base R to plot a time series as a bar plot and as ordinary line plot. I try to write a flexible function to draw such a plot and would like to draw the plots without axes and then add universal axis manually.
Now, I hampered by strange problem: same ylim values result into different axes. Consider the following example:
data(presidents)
# shorten this series a bit
pw <- window(presidents,start=c(1965))
barplot(t(pw),ylim = c(0,80))
par(new=T)
plot(pw,ylim = c(0,80),col="blue",lwd=3)
I intentionally plot y-axes coming from both plots here to show it's not the same. I know I can achieve the intended result by plotting a bar plot first and then add lines using x and y args of lines.
But the I am looking for flexible solution that let's you add lines to barplots like you add lines to points or other line plots. So is there a way to make sure y-axes are the same?
EDIT: also adding the usr parameter to par doesn't help me here.
par(new=T,usr = par("usr"))
Add yaxs="i" to your lineplot. Like this:
plot(pw,ylim = c(0,80),col="blue",lwd=3, yaxs="i")
R start barplots at y=0, while line plots won't. This is to make sure that you see a line if it happens that your data is y=0, otherwise it aligns with the x axis line.
I have three different boxplots,
k1<-boxplot(decreased$Group.1)
k2<-boxplot(unchanged$Group.1)
k3<-boxplot(created$Group.1)
Is there any way I can make side-by-side boxplot with it or do I have to combine the columns for table together and use ~ to find out side by side?
It can happen but you will need to play with the xlim, ylim, at and add arguments.
See this example:
boxplot(1:10, xlim=c(1,6), ylim=c(0,20), at=1.5)
boxplot(2:10, add=TRUE, at=3.5)
boxplot(3:20, add=TRUE, at=5.5)
So, you need to add the x-limits and y-limits on the first plot along with the location of where to plot the first barplot (specified by at). Then consecutive barplots need the location (i.e. again at) and also the add=TRUE argument.