Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Is there any method to keep the curves inside of x and y axis without changing the original data?
With most plotting methods, your plot will stay "inside the lines" unless you set par(xpd = TRUE) (or NA). So, you probably set that option (or passed it to your line plotting command. You can revert to the default by running the line par(xpd = FALSE).
From ?par:
xpd
A logical value or NA. If FALSE, all plotting is clipped to the plot region, if TRUE, all plotting is clipped to the figure region, and if NA, all plotting is clipped to the device region. See also clip.
par(xpd = NA)
plot(1, type = 'n')
abline(h = 1)
p <- par('usr')
do.call('clip', as.list(p))
abline(h = 1.1)
## or equivalently
clip(p[1], p[2], p[3], p[4])
abline(h = .9)
## xpd is still NA
par('xpd')
# [1] NA
Related
This question already has answers here:
How can I plot with 2 different y-axes?
(6 answers)
R: multiple x axis with annotations
(2 answers)
Closed 15 days ago.
I am using native R plot function to genertae graphics. looking to add a double x-axis on same plot. One holds doubles and the other x-axis holds Date object. I am using the following commands but they dont seem to work.
First x-axis:
axis.Date(1,at=seq(min(x$Date),na.rm=TRUE,max(x$Date),na.rm=TRUE,by="2 years"),format ="%Y-%m-%d",col.axis="white", cex=1)
Second x-axis:
axis(1,at=seq(min(f), max(f), by = 0.1), col.axis="white", cex=1)
The parameters for the R native plot:
x11()
par(mfrow=c(1,1),oma = c(0, 0, 2, 0) )
Result is only Dates on x-axis.
Up front: dual axes can easily be mis-used by mis-representing the data and/or ranges. It's easy for eyes to misconstrue correlation or relationships based on imperfect axis decisions. For scatter plots (such as below), I'm not a fan and tend to avoid them ... but I do use them under very controlled circumstances, as they can provide visual correlation of relative trends.
When I must do it, I'm a fan of using color as a way to more strongly tie points (or lines) with particular axes, though of course this does not work as well with color-impaired readers.
Given that preface ...
I believe the easiest way to handle multiple axes in base-R is to use par(new=TRUE) between plots. Here's an example:
par(mar = c(4,4,4,4) + 0.1)
plot(disp ~ mpg, data = mtcars, las = 1)
par(new = TRUE)
dat <- data.frame(dat = Sys.Date() + 0:5, y = 1:6)
plot(y ~ dat, data = dat, ann = FALSE, yaxt = "n", xaxt = "n", pch = 16, col = "red")
axis.Date(3, dat$dat[1], col = "red", line = 1)
axis(4, col = "red", line = 1, las = 1)
Other differentiating techniques include shapes or line-types (if lines) specific to each side, and adding those as clear markers on the secondary axes.
The use of par(new=TRUE) simply allows the next plot command to not reset/clear the canvas before starting over. This means that the subsequent plotting functions have no knowledge of what is existing. From ?par:
'new' logical, defaulting to 'FALSE'. If set to 'TRUE', the next
high-level plotting command (actually 'plot.new') should _not
clean_ the frame before drawing _as if it were on a *_new_*
device_. It is an error (ignored with a warning) to try to
use 'new = TRUE' on a device that does not currently contain
a high-level plot.
It doesn't work well with all plotting mechanisms (certainly nothing grid or ggplot2), and anything that might be sensitive to margins or oma or other parameters should be tested carefully with various ranges of data.
I intentionally used line=1 to "bump out" the top/right axes, another way to set them apart. Frankly, I often do that for the bottom/left (primary) axes as well, it can be aesthetically preferred ... but it's an option and not required for this technique to at least start the process.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Using R's base plots, I know well how to control the margins between R plots and the outer margins of a panel of plots using, respectively:
par(mar=c(1,1,0,0))
par(oma=c(1,1,0,0))
What I am trying to learn is a general way to control the "inner margins" of a plot, which means: how can I increase/decrease the space between the axes and the plot content?
By "general way" I mean something that does not rely on simply manually adding/subtracting an offset to/from the min and max of the variable represented by a given axis. After all, such a solution could lead either to different padding between plots of a same plot panel depending on their scales and differences, or to the need of significant manual adjustments per plot within a plot panel.
Notice that my question is a direct analogous to this other one, but in my case I want to learn how to do it for base R plot instead of ggplot (it does have to be base plot).
As documented in ?plot.window, the default is to extend the range of your data by 4% and use those as the plot limits. You can't change the value 4% to another number, but you can set xlim and ylim explicitly to accomplish the same thing.
For example, if you want a p=50% extension instead, you want the lower limit for x to be min(x) - p*diff(range(x)), and a similar formula for the upper limit, and for y. But if you just use that as the first value in xlim, you'll 50%, followed by another 4%. You could work around this, but using xaxs = "i" will use the exact spec. Putting all of this together, here's an example:
x <- 1:10
y <- 5:14
p <- 0.5
plot(x, y, xlim = c(min(x) - p*diff(range(x)), max(x) + p*diff(range(x))),
xaxs = "i",
ylim = c(min(y) - p*diff(range(y)), max(y) + p*diff(range(y))),
yaxs = "i")
which produces this plot:
You can simply adjust the ranges of your x and y plot limits:
x <- 1:10
y <- 1:10
# Before
plot(x, y)
# After
plot(x, y, xlim = range(x) + c(-1, 1), ylim = range(y) + c(-1, 1))
Output:
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
The function supposed to use colours from the give scale (between low and high) for the border colours if I plot continuous data plot using ggplot2.
> scale_colour_continuous <- function(...) {
> ggplot2::scale_colour_gradient(..., low = "#FFFF00", high = "#3366FF",
> na.value = "#262626", aesthetics = "colour")
Unfortunately, my code above doesn't appear to be working. I find if very interesting, because the same thing for the the fills of the plot works fine with the same arguments (aesthetics = "fill"). What am I missing here?
You don't need to explicitly call ggplot2. If you want to,you could add an if statement that checks if ggplot2 is loaded. This works as required.
my_theme<-function(...){
scale_colour_gradient(..., low = "#FFFF00", high = "#3366FF",
na.value = "#262626", aesthetics = "colour")
}
library(tidyverse)
iris %>%
ggplot(aes(Sepal.Length,Petal.Length,col=Sepal.Length))+
geom_point()+
my_theme()#wanted to make a theme so don't mind the naming.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am drawing a plot in R as follow:
respCSV=read.csv("R1.csv")
respCol=respCSV[["RESP"]]
plot(respCol,type='o')
when it plot the series, the x axis lable are not countinuous, instead of 1,2,3,4,
it is 1,5,10.how to fix that?
If the issue is that the axis labels are too spare, and your x variable is continuous you can make your own. This works on standard calls to plot and sometimes it works on plots based on it, but not always.
plot(x, y, xaxt = 'n') # get rid of the default x-axis
axis(1, at = 1:10, values = 1:10) # put the values you want.
Note that there may be other reasons why your axis labels aren't showing up. If the x-axis isn't continuous, the solution will be different.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am hoping to just change the diagonal plots to have simple outlines so I can view the overlap of the density functions more clearly but am not having much luck. Here is the code I have been using:
plot_rh <- ggpairs(data_rh[,1:6], mapping = ggplot2::aes(color = Condition_name),
lower = list(combo = wrap(ggally_facethist, bins = 10)),
diag = list(continuous = wrap("densityDiag"), mapping = ggplot2::aes(fill=Condition_name)))
Plot with filled density functions:
Changing aes(fill=Condition_name) to aes(color=Condition_name) should result in unfilled lines.
You could also change it to aes(fill=Condition_name), alpha = 0.4 to make the filled densities semi-transparent which may improve the view.