Plot specific values on x axis using plot() in R [duplicate] - r

This question already has answers here:
How to specify the actual x axis values to plot as x axis ticks in R
(4 answers)
Closed 9 years ago.
Let's say I have some points:
a=c(1.234, 23.332, 3.433, 34.53)
b=c(112, 234, 221, 23)
I would like to plot them by having "a" on the x axis and "b" on the y axis.
plot(a,b)
This will give the resulting plot, but what I would like to do is rather than display some interval on the x axis I would like to display the exact numbers that I have, i.e. the values of a.
Is this possible?

Jup.
plot(a,b, xaxt="n")
axis(side=1, at=a)
or maybe
axis(side=1, at=round(a,2))
for aesthetical reasons.

Related

Mussy lines occur when using "plot()" in R [duplicate]

This question already has an answer here:
Ordering of points in R lines plot
(1 answer)
Closed 3 years ago.
I used plot(x, y, type="p") to draw a scatter plot, and it seems right (Figure 1). However, when using plot(x, y, type="l") to draw a line, there are some mussy lines (Figure 2). Why didn't it a "single" line?
Looks like your x vector needs to be sorted, when using line plots, the order in which your points are submitted is very important as the lines are drawn connecting one point to the next one.
y <- y[order(x)]
x <- x[order(x)]
# now you can make your plot
plot(x, y, type="l")

how to customize x-axis in a plot for R? [duplicate]

This question already has answers here:
How to change x-axis tick label names, order and boxplot colour using R ggplot?
(2 answers)
Closed 4 years ago.
I would like to customize my own x-axis using plot in R. What i want is that the x-axis would display 40-52, then from 1-40 again, something in the attachment shown below. My data is from 2015 week 40 to 2018 week 4, and I have tried something like 2017_40 to 2018_4, but this will make the graph look really cramped.
Use xaxt='n' in your plot to suppress printing the x-axis, then use axis to print whatever you want.
x = 40:92
y = sin(x)
plot(x,y, ylim=c(-2,2), type='l', xaxt='n')
xlab = ifelse(x>52, x-52,x)
axis(side=1, at=40:92, labels=xlab)

How to align the x-axis on Y=0 in R? [duplicate]

This question already has answers here:
Set R plots x axis to show at y=0
(2 answers)
Closed 3 years ago.
I observed that the x axes of a plot doesn't cross the y axes at 0.
Why?
How can I fix that?
Example:
plot(mtcars$mpg, ylim=c(0,50))
By default, R extends the axes by 4% on either end around the limits: from ?par,
‘xaxs’ The style of axis interval calculation to be used for the
x-axis. Possible values are ‘"r"’, ‘"i"’, ‘"e"’, ‘"s"’,
‘"d"’. The styles are generally controlled by the range of
data or ‘xlim’, if given.
Style ‘"r"’ (regular) first extends the data range by 4
percent at each end and then finds an axis with pretty labels
that fits within the extended range.
Style ‘"i"’ (internal) just finds an axis with pretty labels
that fits within the original data range.
(yaxs does the same thing for the y-axis).
You can use
plot(mtcars$mpg, ylim=c(0,50), yaxs="i")

How to make a scatter-plot if I have more than one treatment? [duplicate]

This question already has answers here:
How to create a line plot with groups in Base R without loops?
(3 answers)
Closed 9 years ago.
here is my question.
I would like to make a scatterplot which (x,y) pairs are connected by line.
I have three treatments A,B,and C
A<-c(103.4,102.5,101.4,101.0,98.8)
B<-c(102.9,101.6,101.4,100.3,99.6)
C<-c(103.9,103.1,102.3,100.4,97.6)
These 3 variables are going to be plotted on the y axis (Temperature)
And I also have a variable for the x axis (Minutes)
M<-c(15,30,45,60,75)
I just would like to know how to make a plot using different lines and symbols for the 3 different treatments.
Thanks so much!
png() # default file name is "Rplot001.png" in your working directory
matplot(M, cbind(A,B,C), type="b",
xlab= "Minutes", ylab=expression(Temperature~degree*F),
xlim=range(M)+c(-5,5), ylim=range(c(A,B,C)) +c(-1,1) )
dev.off()
# See ?legend and ?title for further annotation

How do I create a histogram in R with logarithmic x and y axes? [duplicate]

This question already has answers here:
Histogram with Logarithmic Scale and custom breaks
(7 answers)
Closed 10 years ago.
So I have a vector of integers, quotes, which I wish to see whether it observes a power law distribution by plotting the frequency of data points and making both the x and y axes logarithmic. However, I am not quite sure how to accomplish this in R. I can currently create a histogram using
hist(quotes, breaks = max(quotes))
But the axes are all linear.
There's probably a better way to do this, but this (basically) works:
data = rnorm(1000,0,1)
r <- hist(log(data))
plot(r$breaks[-1],log(r$counts))
EDIT: Better solution:
r <- hist(data)
plot(r$breaks[-1], r$counts, log='xy', type='h')
# or alternatively:
barplot(r$counts, log="y", col="white", names.arg=r$breaks[-1])
The barplot version doesn't have a transformed x axis for reasons that will become clear if you try it with the x axis transformed.

Resources