Why I can change the x axis? R - r

I tried to run the following script to change the x axis. I followed other answers given in Stack Overflow; however I don’t get what I’m looking for. The original x axis goes from 0 to 1 but I want to change it from 0 to 20.
x<-c(0,0.1,0.2,0.25,0.30,0.35,0.40,0.60,0.90,1)
y<-c(0,0.014,0.41165,0.258,0.57,0.57,0.1803,0.5844,0.10185,0.085)
da<-cbind(x,y)
dat=data.frame(da)
plot(y~x,data=dat,xaxt="n")
pas=c(0,1,2,3,4,5,6,7,8,10,11,12,13,14,15,16,17,18,19,20)
axis(1, at=1:20,labels=pas)
This script produces a figure with just a 0 at the last mark. I need a x axis from 0 to 20. Thanks

I am not sure why you are trying to do this but the following would work.
x <- c(0,0.1,0.2,0.25,0.30,0.35,0.40,0.60,0.90,1)
y <- c(0,0.014,0.41165,0.258,0.57,0.57,0.1803,0.5844,0.10185,0.085)
dat <- data.frame(x = x, y = y)
x.range <- c(0,20)
x.labels <- x.range[1]:x.range[2]
pas <- seq(0, 1, length.out = length(x.labels))
plot(y ~ x, data = dat, xaxt = "n", xlim = c(0,1))
axis(1, at = pas, labels = x.labels, cex.axis=0.65)

You may have missed the xlim/ylim.
Did you mean y axis? Then just use :
plot(y~x,data=dat,ylim=c(0,20))
plot(y~x,data=dat,xlim=c(0,20))# you need to remove the 'xaxt="n"' to see the x-axis
Although either way I am not sure this is the best idea given the range of the data.
Using ggplot2
library(ggplot2)
qplot(x,y,data=dat)+ scale_x_continuous(labels=c("0"="fake0","0.25"="fake5","0.5"="fake10",".75"="fake15","1"="fake20"))# replace 'fake#' with anything
Although I am not sure why are you trying to do this.

Related

Two Dimensional Function 3D Plot

I am attempting to plot a two-dimensional function of x and y below in R. I am not good at coding, so it's possible I'm missing something easy, but I got it to work just fine yesterday. It was displaying the 3D plot perfectly and I was even messing with different colors and point sizes for a while with no issues. I save, exit, and then I open the file today and for some reason I am getting an error. I didn't change anything and I have no clue why it won't work now.
library(rgl)
set.seed(27)
x <- runif(100000,-1,1)
y <- runif(100000,-1,1)
## Two-dimensional function of interest
example <- function(x, y){
f_xy <- x^2 + 2*(y^2) - 0.3*cos(3*pi*x) - 0.4*cos(4*pi*y) + 0.7
return(f_xy)
}
example(x,y)
plot3d(
x = x, y = y, z = example(x,y),
type = "p",
size = 1.3,
xlab = "X",
ylab = "Y",
zlab = "f(x,y)",
)
I get the following error message:
Error in example(x, y) :
dims [product 4] do not match the length of object [100000]
I have narrowed it down to the cosine function being the problem I think. If I mess with
f_xy <- x^2 + 2*(y^2) - 0.3*cos(3*pi*x) - 0.4*cos(4*pi*y) + 0.7
and get rid of the 3pi for x and the 4pi for y then it gives me a plot, but it's obviously not the one I need since I have now changed the equation.
Any ideas on how I can fix this?
I just figured out how to use cospi(3*x) instead of cos(3*pi*x) and now it works. I honestly have no idea why it worked yesterday without me doing this.

Scaling X Axis Values

I am trying to plot the function described below. However, when I do plot it, the x-axis shows 0 up to 1000, probably due to my definition of x0, instead of 0 up to 10. However, if I just use 1:10 as my x-values, the plot is not smooth at all. My question is: how do I set the x-axis to give me the real values that correspond to the function at that point?
PS: I have seen solutions where people replace the x-axis fully with their own values, but I am looking for a method that just uses the actual values of x.
eq <- function(x){
4*x^3 - 72*x^2 + 288*x
}
x0 <- seq(1, 10, by = 0.01)
plot(eq(x0),type='l',main="Plot of 4*x^3 - 72*x^2 + 288*x", xlab = "x", ylab = "4*x^3 - 72*x^2 + 288*x")
You have missed your x0 values. R deduced the x values based on the number of elements in eq(x0) (A sequence 1:1000 was used by default). By the way, you can improve your labels with expression
plot(x = x0,
y = eq(x0),
type='l',
main=expression(paste("Plot of ", 4*x^3 - 72*x^2 + 288*x)),
xlab = expression(x),
ylab = expression(4*x^3 - 72*x^2 + 288*x))
It works fine

R-package beeswarm generates same x-coordinates

I am working on a script where I need to calculate the coordinates for a beeswarm plot without immediately plotting. When I use beeswarm, I get x-coordinates that aren't swarmed, and more or less the same value:
But if I generate the same plot again it swarms correctly:
And if I use dev.off() I again get no swarming:
The code I used:
n <- 250
df = data.frame(x = floor(runif(n, 0, 5)),
y = rnorm(n = n, mean = 500, sd = 100))
#Plot 1:
A = with(df, beeswarm(y ~ x, do.plot = F))
plot(x = A$x, y=A$y)
#Plot 2:
A = with(df, beeswarm(y ~ x, do.plot = F))
plot(x = A$x, y=A$y)
dev.off()
#Plot 3:
A = with(df, beeswarm(y ~ x, do.plot = F))
plot(x = A$x, y=A$y)
It seems to me like beeswarm uses something like the current plot parameters (or however it is called) to do the swarming and therefore chokes when a plot isn't showing. I have tried to play around with beeswarm parameters such as spacing, breaks, corral, corralWidth, priority, and xlim, but it does not make a difference. FYI: If do.plot is set to TRUE the x-coordinates are calculated correctly, but this is not helpful as I don't want to plot immediately.
Any tips or comments are greatly appreciated!
You're right; beeswarm uses the current plot parameters to calculate the amount of space to leave between points. It seems that setting "do.plot=FALSE" does not do what one would expect, and I'm not sure why I included this parameter.
If you want to control the parameters manually, you could use the functions swarmx or swarmy instead. These functions must be applied to each group separately, e.g.
dfsplitswarmed <- by(df, df$x, function(aa) swarmx(aa$x, aa$y, xsize = 0.075, ysize = 7.5, cex = 1, log = ""))
dfswarmed <- do.call(rbind, dfsplitswarmed)
plot(dfswarmed)
In this case, I set the xsize and ysize values based on what the function would default to for this particular data set. If you can find a set of xsize/ysize values that work for your data, this approach might work for you.
Otherwise, perhaps a simpler approach would be to leave do.plot=TRUE, and then discard the plots.

abline will not put line in correct position

I am quite new to programming/R and I'm having a very unusual problem. I've made a scatterplot and I would like to simply put the x y axis at 0 on the plot. However, when I use abline they are slightly off. I managed to get them to 0 using trial and error, but trying to plot other lines becomes impossible.
library('car')
scatterplot(cost~qaly, reg.line=FALSE, smooth=FALSE, spread=FALSE,
boxplots='xy', span=0.5, xlab="QALY", ylab="COST", main="Bootstrap",
cex=0.5, data=scat2, xlim=c(-.05,.05), grid=FALSE)
abline(v = 0, h = 0)
This gives lines which are slightly to the left and below 0.
here is an image of what this returns:
(I can't post an image since I'm new apparently)
I found that these values put the lines on 0:
abline(v=0.003)
abline(h=3000)
Thanks in advance for the help!
Using #Laterow's example, reproduce the issue
require(car)
set.seed(10)
x <- rnorm(1000); y <- rnorm(1000)
scatterplot(y ~ x)
abline(v=0, h=0)
scatterplot seems to be resetting the par settings on exit. You can sort of check this with locator(1) around some point, eg, for {-3,-3} I get
# $x
# [1] -2.469414
#
# $y
# [1] -2.223922
Option 1
As #joran points out, reset.par = FALSE is the easiest way
scatterplot(y ~ x, reset.par = FALSE)
abline(v=0, h=0)
Option 2
In ?scatterplot, it says that ... is passed to plot meaning you can use plot's very useful panel.first and panel.last arguments (among others).
scatterplot(y ~ x, panel.first = {grid(); abline(v = 0)}, grid = FALSE)
Note that if you were to do the basic
scatterplot(y ~ x, panel.first = abline(v = 0))
you would be unable to see the line because the default scatterplot grid covers it up, so you can turn that off, plot a grid first then do the abline.
You could also do the abline in panel.last, but this would be on top of your points, so maybe not as desirable.

Drawing an interval on the graph using vertical line in R ?

install.packages("devtools")
library(devtools)
devtools::install_github("google/CausalImpact")
library(CausalImpact)
set.seed(1)
x1 <- 100 + arima.sim(model = list(ar = 0.999), n = 100)
y <- 1.2 * x1 + rnorm(100)
y[71:100] <- y[71:100] + 10
data <- cbind(y, x1)
pre.period <- c(1, 70)
post.period <- c(71, 100)
impact <- CausalImpact(data, pre.period, post.period)
plot(impact, "cumulative")
Say i want the graph to show an interval from 71-100 with the x scales starting at 1 from the first dotted line any ideas on how to do this?
Does anyone have any idea how to add a second vertical dotted line depicting an interval on the graph? Thanks.
You can use abline() to add lines to a graph, with the argument v = 70 setting a vertical line at x = 70. I'm not sure how to restart the x-scale from that point however - it doesn't seem like something that would be possible but perhaps someone else knows how.
You can reset the axes using this.
In your initial plot command, set xaxt = "n" This ensures that the plot function does not mark the axes.
You can then draw the abline(v=70) as mentioned above.
Then use axis(1,at=seq(60,80,by=1),las=1) 1 stands for x-axis and in the at attribute, mention the x limits you want. I've put in 60 to 80 as an example.

Resources