Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: change background color of plot for specific area only (based on x-values)

Tags:

plot

r

How do I change the background color for a plot, only for a specific area? For example, from x=2 to x=4?

Bonus question: is it also possible for a combination of x and y coordinates? (for example from (1,2) to (3,4))?

Many thanks!

like image 454
Abdel Avatar asked Jan 05 '12 10:01

Abdel


People also ask

How do you color a plot by a variable in R?

One of the ways to add color to scatter plot by a variable is to use color argument inside global aes() function with the variable we want to color with. In this scatter plot we color the points by the origin airport using color=origin. The color argument has added colors to scatterplot with default colors by ggplot2.

How do I color a specific point in R?

If, however, you would rather click on the points you want to change the color of you can do this by using 'identify' along with the 'points' command to replot over those points in a new color. identify is also very easy to use in R Studio.

How do I change the background color in R?

In the menu bar, open the “Tools” menu. From the drop down menu, choose “Global Options”. In the pane on the left hand side of the options window, click “Appearance”. To import a theme, click on the “Add…” button.

How do you change the background color on a subplot?

You can simply do axarr[1,0]. set_facecolor('grey') to change the axis color for any particular axis manually.


1 Answers

This can be achieved by thinking about the plot somewhat differently to your description. Basically, you want to draw a coloured rectangle between the desired positions on the x-axis, filling the entire y-axis limit range. This can be achieved using rect(), and note how, in the example below, I grab the user (usr) coordinates of the current plot to give me the limits on the y-axis and that we draw beyond these limits to ensure the full range is covered in the plot.

plot(1:10, 1:10, type = "n", axes = FALSE) ## no axes
lim <- par("usr")
rect(2, lim[3]-1, 4, lim[4]+1, border = "red", col = "red")
axis(1) ## add axes back
axis(2)
box()   ## and the plot frame

rect() can draw a sequence of rectangles if we provide a vector of coordinates, and it can easily handle the case for the arbitrary x,y coordinates of your bonus, but for the latter it is easier to avoid mistakes if you start with a vector of X coordinates and another for the Y coordinates as below:

X <- c(1,3)
Y <- c(2,4)
plot(1:10, 1:10, type = "n", axes = FALSE) ## no axes
lim <- par("usr")
rect(X[1], Y[1], X[2], Y[2], border = "red", col = "red")
axis(1) ## add axes back
axis(2)
box()   ## and the plot frame

You could just as easily have the data as you have it in the bonus:

botleft <- c(1,2)
topright <- c(3,4)
plot(1:10, 1:10, type = "n", axes = FALSE) ## no axes
lim <- par("usr")
rect(botleft[1], botleft[2], topright[1], topright[2], border = "red",
     col = "red")
axis(1) ## add axes back
axis(2)
box()   ## and the plot frame
like image 193
Gavin Simpson Avatar answered Oct 21 '22 02:10

Gavin Simpson