Re-labelling graph columns in R? [duplicate] - r

This question already has an answer here:
How to remove default axis from the plot produced by boxplot()?
(1 answer)
Closed 8 years ago.
I'm trying to change the names of the column labels on the x axis of a boxplot.
This is my code for the graph:
boxplot(datab~area,ylab="Heather Cover (%)",xlab="Survey Area")
axis(1, at=1:3, lab=c("Hangars", "East", "Runway"))
This works, however, the old column names (1,2,3) remain underneath the new labels. How do I get rid of these?

One thing you can do is remove the x-axis labels using xaxt="n" in the original plot. Let me illustrate using the "mtcar" data.
boxplot(mpg~gear, data=mtcars, xaxt="n")
axis(1, at=1:3, lab=c("Hangars", "East", "Runway"))

Related

2 vectors in one plot [duplicate]

This question already has answers here:
Plotting multiple curves same graph and same scale
(5 answers)
Closed 4 years ago.
I spent a long time trying to figure something out which I thought would be very easy. I have three vectors (or a data frame if you want to make it into one)
date <- c("Q1","Q2","Q3","Q4")
group1 <- c(12,13,16,11)
group2 <- c(9,11,10,9)
Now I want to create one graph with the date along the x-axis, and two horizontal lines representing the 2 groups. For a bit of context, I did a difference-in-difference regression and want to show the average values for treatment and control group around the event. I'm using panel data and already calculated the mean for both groups at each point in time. Here is a sceenshot I took from my so you can see how I want it to look like.
# plot solid line, set plot size, but omit axes
plot(x=seq(date), y=group1, type="l", lty=1, ylim=c(5,20),
axes=F, bty="n", xaxs="i", yaxs="i", main="My Title",
xlab="", ylab="Total Risk-Based Capital Ratio")
# plot dashed line
lines(x=seq(date), y=group2, lty=2)
# add axes
axis(side=1, labels=date, at=seq(date))
axis(side=2, at=seq(5,20,3), las=1)
# add vertical red line
abline(v=2, col="red")
# add legend
par(xpd=TRUE)
legend(x=1.5, y=2, legend=c("solid", "dashed"), lty=1:2, box.lty=0, ncol=2)

How to change the Scale of Y-Axis in R [duplicate]

This question already has answers here:
R Language: How to Set ylim?
(2 answers)
Closed 4 years ago.
I have this graph which I'm plotting
As you can see, the values on the black line which I'm trying to plot is not on the same scale as that of the red line.
Therefore, I'm trying to change the scale of the Y-Axis, so I can essentially "Zoom Out". How do I do this?
What I did till now is:
plot(d,vol1, type="l",xaxt="n", xlab="Date", ylab="Volatility Estimate", main="Nasdaq Pharmaceutical Index")
months= seq(min(d), max(d), "month")
axis(1, months, format(months, "%Y\n%b"))
lines(d, vol1, col="black")
You can add the argument ylim=c(a,b) inside the plot() command, where a is the minimum and b is the maximum of your desired y-axis.

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)

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

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.

Suppress ticks and labels in an r plot [duplicate]

This question already has answers here:
Remove plot axis values
(5 answers)
Closed 6 years ago.
I want to create a plot with ticks and labels above and to the right of the plot using axis(). How do I suppress the ticks and labels that 'automatically' print with the plot() function? Thank you
x<-1:10
y<-1:10
quartz("test")
par(mar=c(10,10,10,10)+0.1)#sets margins of plotting area
par(pty="s")#fixes the aspect ratio to 1:1
#Automatically adds ticks, numbers and axis labels. How can I avoid this?
plot(x,y,typ="n")
#Adds axes above and to the right of plot area...I want these only
axis(side=4,las=2, ylab="y label")
axis(side=3,las=1,xlab="x label")
See ?plot.default:
plot(x, y, type="n", axes=FALSE, frame.plot=TRUE, xlab="", ylab="")

Resources