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.
Related
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)
Background:
I have a polygon() that doesn't look as professional as I need it to be (see my R code blow).
I usually use two general techniques to make ploygon()s come out the way I want them to be. First, in my curve, I use lwd = larger than 1. Second, in my polygon, I remove the border of my polygon() by border = NA.
In this case below, for some reasons, I can NOT use the first technique.
Question:
I was wondering what else I can do in addition to removing the border=NA in my polygon() to improve my polygon?
curve(dnorm(x), -3, 3, bty="n", ann=F, axes=F, col="blue")
xs <- seq(-0.5, 0.5, len=1000)
polygon(c(xs[1], xs, xs[1000]), c(0, dnorm(xs), 0), col='grey', border = NA)
I don't know how much better this is, but maybe add curve after the polygon
curve(dnorm(x), -3, 3, bty="n", ann=F, axes=F, col=NA)
xs <- seq(-0.5, 0.5, len=1000)
polygon(c(xs[1], xs, xs[1000]), c(0, dnorm(xs), 0), col='grey', border = NA)
curve(dnorm(x), -3, 3, col='blue', add = TRUE, n = 1e4)
#n = 1e4 helps plot curve with more points (default in 101), making it smoother
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)
Is it possible to set background of a plot to be coloured in such a way:
col = c("grey", "white")
par(bg = rep(col, length.out = 5))
So that grey and white lines would repeat length.out number of times?
I'm not quite sure what you are after. If you want colour rectangles, then use rect
add_bg = function() rect(-3:2, -3, -2:3, 3, col=1:2)
plot(rnorm(10), rnorm(10), xlim=c(-3, 3), ylim=c(-3, 3),
bg=3,pch=21,cex=2, panel.first=add_bg())
to get a stripped effect. If you just want vertical lines, then use grid
no=10
## For horizontal lines set ny=no, nx=0
plot(rnorm(10), rnorm(10), xlim=c(-3, 3), ylim=c(-3, 3),
bg=3,pch=21,cex=2, panel.first=grid(ny=0,nx=no, col=1:2))
In both examples, I used the panel.first argument. When the graph is drawn the grid/rectangles are placed behind the points.
I am embarrassed to ask this simple question, but has been in kicking my mind for several days whenever I create a plot:
plot (x = 1:10, y = rnorm (10, 5, 2))
grid (10,10, lty = 6, col = "cornsilk2")
I want to position the grids right at where axis are labelled, i.e. at 2, 4, 6, 8, 10 in x axis and similarly 3, 4, 5, 6, 7, 8 in y axis.
I want to automate the process as whenever the plot size changes the default label behaviour changes. See the following plot:
From ?grid description of the nx and ny arguments:
When NULL, as per default, the grid aligns with the tick marks on the
corresponding default axis (i.e., tickmarks as computed by axTicks)
plot (x = 1:10, y = rnorm (10, 5, 2))
grid (NULL,NULL, lty = 6, col = "cornsilk2")
For reference, there is a way to control the grid and axes parameters directly from the plot() command, if we are not defining a custom tick interval:
plot(x = 1:10, y = rnorm(10, 5, 2), xlim=c(1, 10), ylim=c(1, 10), panel.first=grid())
The plot.default() documentation gives more information about these parameters.
When using a custom ticks interval, the easiest is to draw the grid using abline:
plot(x = 1:10, y = rnorm(10, 5, 2), xaxp=c(1, 10, 10), yaxp=c(1, 10, 10), axes=FALSE)
axis(1, 1:10)
axis(2, 1:10)
abline(h=1:10, v=1:10, col="gray", lty=3)
More information about custom tick intervals in this thread and here for grid alignment.
For posterity, here is the long-winded way of doing it manually:
plot (x = 1:10, y = rnorm (10, 5, 2))
grid (lty = 6, col = "cornsilk2")
xaxp <- par("xaxp")
yaxp <- par("yaxp")
abline(v=seq(xaxp[1], xaxp[2], (xaxp[2]-xaxp[1])/xaxp[3]), lty=6, col = "cornsilk2")
abline(h=seq(yaxp[1], yaxp[2], (yaxp[2]-yaxp[1])/yaxp[3]), lty=6, col = "cornsilk2")
The answer provided here is much more straightforward, although you may dislike the lack of "free space" at each end of the axes. In brief,
The problem is that grid is putting nx grid lines in the user space,
but plot is adding 4% extra space on each side. You can take control
of this. Adding xaxs="i", yaxs="i" to your plot will turn off the
extra space. But then your upper right point will be cut off, so you
need to change the xlim and ylim values and change nx to match