I am trying to get my graph in R to change it's Y axis values.
Code:
plot(tree$NUM,tree$GRA,
main="YSLOW Grades",
xlab="HAR #",
ylab="Grade",
xaxt="n")
axis(1, at = seq(1, 20, by = 1), las=2)
I have figured out how to customize the x axis, but from all my researching I cannot find a way to simply change the Y axis too. Instead of having numbers, I want to customize the graph so that I can but letter grades in like A,B,C and so on. I assume it's a quick fix to do this but I really am clueless and the material seems lacking on the subject.
To clarify, I do not want to change Y axis label or the spacing, I simply want to be able to but letters on the Y axis, regardless of the data coming into it.
Just put yaxt = "n" and put the new y labels with axis(2, ....). Example:
plot(1:20,1:20,
main="YSLOW Grades",
xlab="HAR #",
ylab="Grade",
xaxt="n",
yaxt = "n")
axis(1, at = seq(1, 20, by = 1), las=2)
axis(2, at = seq(1, 20, by = 1), label = rep(c("A", "B"), 10), las=2)
Related
I have data sets containing daily precipitation and discharge data. Now I would like to plot everything in one plot. All data sets are of length 61, so they can share the same x axis. The discharge data should be plotted the "normal" way, meaning that the y axis starts at the bottom and is placed on the left side. The precipitation data should be plotted "from the top", meaning that the y axis is reversed and placed on the right side.
Here is some code for a minimal reproducible example:
precipitation <- runif(61, min=0, max=25)
discharge <- runif(61, min=370, max=2610)
The result should approximately look like this:
Anybody with an idea how to achieve this?
EDIT: thanks pascal for the answer that implies the usage of ggplot2.
I also found a way by myself to do it with Base R, in case it could help anybody in the future:
precipitation <- runif(61, min=0, max=25)
discharge <- runif(61, min=370, max=2610)
# plot with Base R
par(mar = c(5, 5, 3, 5), xpd = TRUE)
plot(precipitation, type= "l", ylim= c(0,80), ylab= "Precipitation [mm/day]", main= "Comparison",
xlab= "Day", col= "blue")
par(new = TRUE)
plot(discharge, type= "l", xaxt = "n", ylim= rev(c(0,5000)), yaxt = "n", ylab = "", xlab = "", col= "red", lty= 2)
axis(side = 4)
mtext("Discharge [m³/s]", side = 4, line = 3)
The ggplot2 way looks a bit fancier of course.
ggplot2 can be used to make plots with a second, inverted axis. One has to specify sec.axis in scale_y_continuous(). I'm using a transformation ((100-x)*100) for your data and apply it to the axis as well, so that it fits. This can be changed to any numbers.
ggplot() +
geom_line(aes(y=precipitation, x=1:61), col="orange") +
geom_line(aes(y=100-discharge/100, x=1:61), col="blue") +
scale_y_continuous(name="rain", sec.axis=sec_axis(~(100-.)*100, name= "discharge"))
When I do a boxplot diagram with the R boxplotfunction, this function prints the y-axis automatically.
library(datasets)
boxplot(cars[c('speed', 'dist')],
col = "lightgray")
In the ?boxplot I found the ylim parameter that change the y-axis limits, but not change the scale. So I tried to use the axis function to divide the scale from 0 to 120 every 10:
axis(4, at = seq(0, 120, 10)). But I'm not getting a satisfactory result.
I can't see where I'm making mistakes. Could someone help with this question?
Thanks in advance.
library(datasets)
boxplot(cars[c('speed', 'dist')], col = "lightgray", ylim = range(0:120), yaxs = "i")
axis(4, at=seq(0, 120, 10))
The y-axis is on the right-hand side as you wanted I believe.
You could use ggpubr instead. It let's you treat it as a gg object.
librabry(ggpubr)
library(reshape2)
df <- melt(cars)
p <- ggpubr::ggboxplot(data = df, x = "variable", y = "value", width = 0.8) +
ggtitle("Plot of car") +
xlab("my-xalabel") + ylab("my-ylabel")
>p
If you want in log scale:
p + ggpubr::yscale("log2", .format = TRUE)
I am answering because the OP said in a comment that my comment did the job. I will also explain the code here.
There are two tricks to consider:
First plot without the yaxis by setting argument yaxt = "n".
Then plot the axis number 2, with the labels always perpendicular to the axis. This is done with las = 2.
So the final code is the following.
library(datasets)
boxplot(cars[c('speed', 'dist')],
col = "lightgray", yaxt = "n")
axis(2, at = seq(0, 120, 10), las = 2)
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
I'm trying to move the x-axis labeling and tick marks above the plot on top. Here's my code.
ucsplot <- plot(ucs, depth, main= "Depth vs. UCS", xlab = "UCS (psi)", ylab="Depth (ft)", type="l", col="blue", xlim=c(0, max(dfplot[,3]+5000)), ylim = rev(range(depth)))
ucsplot
How do I get the x-axis labeling and tick marks to appear only on top, instead of the bottom? Also, how do I get the title to not sit right on top of the numbers right above the tick marks? Also, how do I get the chart to start not offset a little bit to the right? As in the zero and starting numbers are in the corners of the plot and not offset.
Seems the OP is looking for a plot where x-axis is at top. The data has not been provided by OP. Hence using a sample dataframe, solution can be displayed as:
df <- data.frame(a = 1:10, b = 41:50)
plot(a ~ b, data = df, axes = FALSE, xlab = NA, ylab = NA)
axis(side = 2, las = 1)
axis(side = 3, las = 1)