How can I increase the length of plot tick marks? Here is a small example:
r <- as.POSIXct(round(range(time), "mins"))
plot(time, x, t="l", xaxt = "n")
axis.POSIXct(1, at = seq(r[1], r[2], by = "min"), format = "%H:%M:%S")
which gives
As you can see, all the ticks are the same size. Is there a way to automatically increase the length of those ticks that are signed?
When creating a very specific axis layout, you typically need to add the axis after drawing the plot. Since you didn't have a reproducible example, I've created my own data set.
Create a plot, but don't display the axis
plot(1:10, axes=FALSE, frame=TRUE)
Add in the x-scale. In this example, values 1,2,3, ...., 10. The argument tck specifies the tick length:
##The tck value should be smaller here
axis(1, 1:10, tck=-0.05)
Now add in an additional scale for "in-between" values. I've set labels="", so we don't print any values:
axis(1, seq(0.5, 9.5, 1), labels=rep("", 10), tck=-0.01)
This gives:
Related
I have a data frame with the first column named "Date." It has values like "2016-01-01, 2016-01-02 ..." etc. The second column is named "precipBulk," and it just has decimal values (ex. 3.36, 1.57, etc.). The third column is named "abundance," and it also has decimal values. I want to graph both "abundance" and "precipBulk" on one graph(Like the image), but I want the x-axis to have intervals with every month instead of every other month like it is now. I know there's a way to do it in ggplot2 using "scale_x_date()" but I can't graph both of the y values in one graph with ggplot2 for some reason. Is there a way to do it without using ggplot2? if not, any tips on how I would graph dual y-axis to achieve this with ggplot2?
Graph link https://i.stack.imgur.com/SZXgT.png `
Small portion of data frame https://i.stack.imgur.com/PvTED.png
To make the graph, I did:
x = frame$Date
y1 = frame$precipBulk
y2 = frame$abundance
plot(x,y1, type = "l",ylab="Bulk Precipitation",xlab="Month",col="blue", main = "Precipitation vs Mosquito Abundance (OSBS 2016)", cex.main = 1)
par(new = TRUE)
plot(x, y2, type = "l",yaxt="n",xaxt="n",ylab="",col="red")
axis(side = 4)
legend('topleft', c("Precipitation", "Mosquito Abundance"), col= c("blue", "Red"),lty=c(1,1), adj = c(0,0.6), cex= 0.75)
You need to turn the x-axis off (as you did) and then add it manually, perhaps reducing the size if necessary so that the axis tick labels fit, otherwise, R will decide for you.
x <- seq(as.Date("2017-01-01"), as.Date("2018-01-01"), "day")
plot(x, rnorm(length(x)), xaxt="n")
at <- seq(min(x), max(x), "month")
axis(side=1, at=at, labels=format(at, "%b"), cex.axis=0.7)
I'm trying to use R to do a barplot. Values I'm plotting range from 0 to 5.0, but are decimal values (such as 4.87) so I don't want to just use the default Y axis, because it just goes up in increments of 1.
I've created a custom Y axis, which works, but if I set the maximum value greater than about 4.5, it cuts off the tickmark at the top of the axis. This looks untidy so I want a way to ensure this tickmark will always appear, but I don't want to shorten my axis as it looks stupid if I do this.
My R code is as follows:
# Bar plot of mean SUS question scores
barplot(meanSUSQuestions$Mean,
main="Mean SUS Question Scores",
cex.main="0.8",
cex.axis="0.8",
cex.lab="0.8",
#names=c("q1", "q2", "q3","q4","q5","q6","q7","q8","q9","q10"),
names=c(1:10),
yaxt="n",
col="red")
axis(2, cex.axis="0.8", at=seq(0, 5, 0.5)) # Create custom Y axis
mtext(text="Mean Score", side=2, line=2, cex=0.8)
mtext(text="Question", side=1, line=2, cex=0.8)
The bar plot that this produces looks like this:
As you can see from the picture, the top tickmark is missing.
How can I get this top tickmark to appear?
barplot generates the image height based on the data. The range of your manual y-axis is considerably larger than the plot area and is thus cut off.
The easiest way to solve the issue in your specific case is to add an yaxp = c(0, 5, 11) to barplot instead of yaxt = "n" and axis.
A self-contained example:
# Bad
x <- 1:5
barplot(x, yaxt = "n") #, add = TRUE)
axis(2, at = seq(0, 6, 2)) # Create custom Y axis
# Good
barplot(x, yaxp = c(0, 6, 2))
Is it possible to increase the values in the X-axis with 1? For example - 1,2,3,4,5 etc.
Right now I use this:
xlim=c(1,16)
And the result is:
Which doesn't look nice, the ideal would be to have a sequential increase with 1 - from 1 to 16, since I have 16 values for the X-axis.
xlim can be finely controlled with axis. To make it clear, I will reproduce one plot without axis control and one instead where we performed a modification on the scale.
x <- rnorm(100, 10, 2)
y <- rnorm(100, 10, 2)
par(mfrow = c(1, 2))
Plot 1 is produced without axis control
plot(x, y, main = "Plot 1")
In Plot 2 we set a demonstrative xlim and ylim that produce a scale from 0 to 20 for both axes. We can more finely tune it with axis: to make an example, I create a scale by 1 for axis x and by 5 for axis y
plot(x, y, xlim = c(0, 20), ylim = c(0, 20), main = "Plot 2")
axis(1, at=seq(0, 20, 1))
axis(2, at=seq(0, 20, 5))
That's not all. axis allow a really fine work on your plot axis with its arguments.
axis(side, at=, labels=, pos=, lty=, col=, las=, tck=, ...)
side
an integer indicating the side of the graph to draw the axis (1=bottom, 2=left, 3=top, 4=right)
at
a numeric vector indicating where tic marks should be drawn
labels
a character vector of labels to be placed at the tickmarks (if NULL, the at values will be used)
pos
the coordinate at which the axis line is to be drawn. (i.e., the value on the other axis where it crosses)
lty
line type
col
the line and tick mark color
las
labels are parallel (=0) or perpendicular(=2) to axis
tck
length of tick mark as fraction of plotting region (negative number is
outside graph, positive number is inside, 0 suppresses ticks, 1 creates gridlines) default is -0.01
Hello how to change size axis plot in R
plot(c(imf[1,]), ylim=c(-100, 100),type="l", col="blue")
And the result is
I want axis is sequence 1:36, not (0, 5, 10, 20, 25, 30, 35) but (1,2,3,4,5,6,7,.....36)
You should axis. But it is optimized to not overlap overlap previously drawn labels, so here I am playing with cex parameter (depends on you window size) to show all labels.
plot(1:36, rnorm(36), axes = FALSE)
axis(1, 1:36, 1:36,cex.axis=0.5)
Suppress the x-axis with xaxt = "n" and add it back with your own axis.
plot(runif(36), type="l",col="blue", xaxt = "n", cex.axis=0.7)
axis(1, at=1:36, labels=1:36,cex.axis=0.7)
I have a question regarding the command plot().
Is there a way to fully eliminate the x-axis and replace it with own values? I know that I can get rid of the axis by doing
plot(x,y, xaxt = 'n')
and then add an axis with
axis(side = 1 etc.)
However, when I add the axis, obviously it still refers to the data plotted as 'x'.
I would only like to plot the 'y'-values and add the x-axis my own in the sense of just "drawing" the x-axis with own values specified. Is there any way to do that?
The background of this question is that my two data frames differ in their length and therefore I cannot plot them.
Not sure if it's what you mean, but you can do this:
plot(1:10, xaxt = "n", xlab='Some Letters')
axis(1, at=1:10, labels=letters[1:10])
which then gives you the graph:
You could set labels = FALSE inside axis(...) and then print the labels in a separate command using text(...). This option would allow you to rotate the text in case you need it.
lablist<-as.vector(c(1:10))
axis(1, at=seq(1, 10, by=1), labels = FALSE)
text(seq(1, 10, by=1), par("usr")[3] - 0.2, labels = lablist, srt = 45, pos = 1, xpd = TRUE)
Detailed explanation here