R - adding legend to plot where par(mfrow = c(2, 1)) - r

I'm trying to add a legend to a plot which is the top plot of a two plot figure. However, the legend is getting massively stretched out so that nothing can be seen and it obscures the end of the plot. Here is the code to reproduce the problem:
cars = c(1, 3, 6, 4, 9)
par(mfrow = c(2, 1), mar=c(5,5,2,2))
plot(cars)
legend("topright", legend=c("label1", "label2"))
And here is what it looks like:
Is there some way to fix this?

Related

Rotate y axis TEXT labels in plot.zoo

I would like to rotate the labelling of the y-labs to horizontal and can't find an answer without ggplot.
Is there a way to rotate them in plot.zoo?
The labels I mean are those ones called Series 1:5 and I have outlined them in red.
data <- xts(matrix(rnorm(1000), ncol = 5), order.by = as.Date(1:200))
plot.zoo(data)
Use las=1 like this:
plot.zoo(data, las = 1)
Update
The question later clarified that it was referring to the ylab. plot.zoo uses mtext for that and hard codes it; however, we could hack it using trace:
library(xts)
trace(plot.zoo,
quote(mtext <- function(...) graphics::mtext(..., cex = 0.7, las = 1)))
plot.zoo(data, oma = c(6, 5, 5, 0))
untrace(plot.zoo)

Aligning grid lines in R, bReeze package

I am trying to get grid lines work properly in the image below. Using the bReeze package to plot the power curves of the turbines with:
library(bReeze)
pc=pc("Vestas_V90_1.8MW.wtg")
plot(pc)
The output plot is:
but assigning grid lines to the plot with the help of:
grid()
gives the image below:
Any suggestions on how to fix the distorted grid lines?
If you don't give some arguments (e.g., mar, xlim, ylim),
plot(pc) uses par(mar = c(5, 5, 1, 5) and treats data.ranges as xlim and ylim. By using these properties, you can use grid().
pc.data = pc("Vestas_V90_1.8MW.wtg")
plot(pc.data)
par(mar = c(5, 5, 1, 5), new=T) # set par() and order to overlay
plot(pc.data[[1]], pc.data[[2]], type="n", ann=F, axes=F) # nothing but setting xy-cordinates
grid(NULL) # here, the same xy-coordinates are reproduced
# If you want to adjust grid lines to right y-axis, use berow code
:
par(mar = c(5, 5, 1, 5), new=T) # plot(pc) uses right ylim=c(0,1)
plot(pc.data[[1]], pc.data[[2]], ylim=c(0,1), type="n", ann=F, axes=F)
grid(NULL) # the xy(right)-coordinates are reproduced
# If you plot pc.object having single y-axis, use mar = c(5, 5, 1, 1)

Plotting figures using 'par(mfrow=c())' in R

I have 3 figures of which I would like to plot in the same place in R. I would like to have 2 columns, which would make the 3rd figure plotted alone in the second row. Using par(mfrow=c(2,2)) functions in R, is there a way to have the bottom figure plotted in the centre of the plot, as opposed to underneath the top figure?
I don't think you can do this using par(mfrow = ...)
However, you can use layout().
Try this:
par(mai=rep(0.5, 4))
layout(matrix(c(1,2,3,3), ncol = 2, byrow = TRUE))
plot(1:10)
plot(1:20)
plot(1:30)
So you can see the idea is to create a matrix where each cell indicates which graph to plot. You can extend the logic as follows:
par(mai=rep(0.5, 4))
layout(matrix(c(1,1, 2,2, 0, 3,3, 0), ncol = 4, byrow = TRUE))
plot(1:10)
plot(1:20)
plot(1:30)

Adding images to leafs of dendrogram, instead of text in R

I'm trying to achieve a radial dendrogram, similar to the one below in R (see this post). However, instead of the text on the leafs, I'd like to have an image, for example:
Any idea how I could go about this?
While difficult with the circle dendrogram you show, you can do this with a vertical dendrogram with
grid::grid.raster(mypng, x=.9, y=.7, width=.5)
rasterImage(as.raster(object), 5, 5, 6, 6)
Example
library(dendextend)
dend <- as.dendrogram(hclust(dist(USArrests[1:5,])))
image <- as.raster(matrix(0:1, ncol = 5, nrow = 3))
par(mar=c(10,3,3,0))
plot(dend)
rasterImage(as.raster(image), 1-0.5, -200, 1+0.5, -100, xpd=TRUE)

Shade part of an R plot

I am trying to make a plot in R that has a portion of the plot grey to emphasize this area. Unlike other examples, I don't want to color an area under a plot, but instead color an area on a plot starting at one area and going to the end of the graph. When I try to use rect() or polygon() it obscures the plots I want to emphasize.
For example:
x_mean <- c(1, 2, 3, 4)
y_mean <- c(1, 1, 1, 1)
y_max <- c(4, 4, 4, 4)
y_min <- c(-4, -4, -4, -4)
x_shade <- c(2, 3, 4)
y_max_shade <- c(4, 4, 4)
y_min_shade <- c(-4, -4, -4)
plot(x=rep(x_mean, 3), y=c(y_mean, y_max, y_min), bty='n', type="n" )
arrows(x0=x_mean, y0=y_min, x1=x_mean, y1=y_max, length=0)
points( x=x_mean, y=y_mean, pch=16)
This will plot 4 lines on the graph. How do I draw a grey box in the background from the 2nd line to the end of the plot?
Just so that you're left with more than just a comment, here's a possible solution:
plot(x=rep(x_mean, 3), y=c(y_mean, y_max, y_min), bty='n', type="n" )
rect(2,-4,4,4,col = rgb(0.5,0.5,0.5,1/4))
arrows(x0=x_mean, y0=y_min, x1=x_mean, y1=y_max, length=0)
points( x=x_mean, y=y_mean, pch=16)
Note that I also demonstrated how to use alpha blending in the color specification (using rgb). This can also be useful for this sort of thing. Try moving the rect line to the end, and notice that the results still look ok, because the fill color is partially transparent.
I've found this answer to be pretty great for shading background parts of R.
Some context:
panel.first = rect(c(1,7), -1e6, c(3,10), 1e6, col='green', border=NA)
The first two arguments c(1,7) are the starting values for the shaded rectangle, and following arguments c(3,10) are where the shading ends. This creates a shaded region from 1-3 and 7-10.

Resources