I want to remove labels and axis from X axis, however adding new ticks.
plot(1:10, ylab = "")
at1 <- seq(1, 10, 0.1)
axis(side = 1, at = at1, labels = FALSE)
I could not get rid of y labels.
see ?par You need the xaxt argument
plot(1:10, ylab = "", xaxt='n')
I am not certain what you want, but this removes the x label and uses the tick marks you are generating with at1:
plot(1:10, ylab = "", xlab="")
at1 <- seq(1, 10, 0.1)
axis(side =1, at1, labels = F)
I took the suggestion by GSee to remove the y tick marks if you also want to do that.
plot(1:10, xlab = "", ylab = "", yaxt='n')
at1 <- seq(1, 10, 0.1)
axis(side =1, at1, labels = F)
Related
I have the plot:
plot(Combined$TIMESTAMP, # Draw first time series
Combined$GPP_NT_VUT_REF,
type = "l",
col = 2,
ylim = c(0, 15),
xlab = "",
ylab = expression(paste("GPP [gC m"^"-2 "," day "^"-1]"))) +
lines(Combined$TIMESTAMP, # Draw second time series
Combined$GPP_WRF_mean,
type = "l",
col = 3) +
legend("topright", # Add legend to plot
c("OBS", "WRF"),
lty = 1,
col = 2:4)
which produces the timeseries graph with the name of x values equal to gen, feb, mar and I want to convert my x values in J,F,M,A,M,J...
I tried with:
axis(1, at=1:12, labels=month.name, cex.axis=0.5)
but it doesn't work - any help?
Keep the first letter of month.name or from month.abb:
month.1st.letter <- sub("(^[[:upper:]]).*$", "\\1", month.abb)
Then use this vector as the labels argument.
axis(1, at = 1:12, labels = month.1st.letter, cex.axis = 0.5)
Edit
Start by removing the plus signs from the code, base R graphics do not add up. Then, in the plot instruction include xaxt = "n". And plot the axis right after it.
plot(Combined$TIMESTAMP, # Draw first time series
Combined$GPP_NT_VUT_REF,
type = "l",
col = 2,
ylim = c(0, 15),
xaxt = "n", # here, no x axis
xlab = "",
ylab = expression(paste("GPP [gC m"^"-2 "," day "^"-1]")))
axis(1, at = 1:12, labels = month.1st.letter, cex.axis = 0.5)
I want to plot my points on a graph and then show the density distribution on the x-axis and on the y-axis at the same time.
I'm able to do it on the x axis but not on the y axis.
par(mfrow=c(1,1))
plot(rnorm(100))
par(new=TRUE)
plot(density(rnorm(100,10,123)), ann = FALSE, xlab = "", ylab ="",xaxt='n', yaxt='n')
par(new=TRUE)
plot(density(rnorm(100, 10,12)), col = "red", ann = FALSE, xlab = "", ylab ="",xaxt='n', yaxt='n')
There is no reason you can't.
set.seed(0)
d1 <- density(rnorm(100, 10, 123))
d2 <- density(rnorm(100, 10, 130))
## shared x, y, range / limit
xlim <- c(min(d1$x[1], d2$x[1]), max(d1$x[512], d2$x[512])) ## default: n = 512
ylim <- c(0, max(d1$y, d2$y))
## conventional plot
plot(d1$x, d1$y, type = "l", xlim = xlim, ylim = ylim)
lines(d2$x, d2$y, col = 2)
## rotated plot
plot(d1$y, d1$x, type = "l", xlim = ylim, ylim = xlim)
lines(d2$y, d2$x, col = 2)
Remarks:
never use par(new = TRUE); set xlim and ylim yourself;
customize the plot with title, axis display yourself.
I have created a plot in R and my own custom x and y axes. I would like the x axis to be displayed in a reverse order (1-0 by -.02). I have read numerous posts and threads that suggest using xlim and reverse range but I just can't seem to make it work. Once plotted I am also converting the axes labels to percentages by multiplying by 100 (as you will see in the code). Here is what I have so far;
plot(roc.val, xlab = "Specificity (%)", ylab = "Sensitivity (%)", axes = FALSE)
axis(2, at = seq(0,1,by=.2), labels = paste(100*seq(0,1, by=.2)), tick = TRUE)
axis(1, at = seq(0,1,by=.2), labels = paste(100*seq(0,1, by=.2)), tick = TRUE)
How can I reverse the x axis scale so that the values begin at 100 and end at 0 with increments of 20?
I think this creates a plot in which the y-axis is in reverse order:
x <- seq(-4, 4, length = 10)
y <- exp(x) / (1 + exp(x))
plot(x,y, ylim = rev(range(y)))
This removes the axis values:
x <- seq(-4, 4, length = 10)
y <- exp(x) / (1 + exp(x))
plot(x,y, ylim = rev(range(y)), labels = FALSE)
I guess you can assign the axis values you want then with a variation of your lines:
axis(2, at = seq(0,1,by=.2), labels = paste(100*seq(0,1, by=.2)), tick = TRUE)
axis(1, at = seq(0,1,by=.2), labels = paste(100*seq(0,1, by=.2)), tick = TRUE)
df <- data.frame(x=seq(0,1, length.out=50), y=seq(0, 1, length.out=50))
plot(df)
df$x1 <- (max(df$x) - df$x)/ (max(df$x) - min(df$x))
plot(df$x1, df$y, axes=F, xlab = "Specificity (%)", ylab = "Sensitivity (%)")
axis(2, at = seq(0,1,by=.2), labels = paste(100*seq(0,1, by=.2)), tick = TRUE)
axis(1, at = seq(0,1,by=.2), labels = paste(100*seq(1,0, by=-.2)), tick = TRUE)
Adapting Mark Miller's answer to solve a similar problem (I found this topic by looking for the solution) and I found a variation of his solution in https://tolstoy.newcastle.edu.au/R/help/05/03/0342.html.
Basically if you want to reverse the X-axis values in the plot, instead of using ylim=rev(range(y)), you can use xlim=rev(c(-4,4)).
x <- seq(-4, 4, length = 10)
y <- exp(x) / (1 + exp(x))
par(mfrow=c(1,2))
plot(x, y, ylim=range(y), xlim=c(-4, 4))
plot(x, y, ylim=range(y), xlim=rev(c(-4, 4)))
plot1
And if you want to keep the x-axis values in the true order, you can use this:
par(mfrow=c(1,1))
plot(x, y, ylim=range(y), xlim=c(-4, 4), axes=FALSE)
par(new=TRUE)
plot(-100, -100, ylim=range(y), xlim=c(-4, 4), axes=FALSE, xlab="", ylab="", main="")
axis(1, at = seq(-4,4,by=1), labels = seq(-4,4,by=1), tick = TRUE)
axis(2, at = seq(0,1,by=.2), labels = paste(100*seq(0,1, by=.2)), tick = TRUE)
plot2
I'm posting this solution because I needed something very straightforward to solve my problem. And the solution for it needed the plot with the X-axis value in the correct order (and not reversed).
First, check out the ggplot2 library for making beautiful and extendable graphics. It is part of the Tidyverse approach to R and a gamechanger if you have not been exposed to it.
For example, to solve your issue using ggplot, you simply add the term scale_x_reverse() to your graphic.
See: http://ggplot.yhathq.com/docs/scale_x_reverse.html
how to
Combine a bar chart and line in single plot in R (from different data sources)?
Say I have two data sources as:
barData<-c(0.1,0.2,0.3,0.4) #In percentage
lineData<-c(100,22,534,52,900)
Note that they may not be in the same scale.
Can I plot both barData and LineData in one plot and make them good looking ?
I cant use ggplot in this case so this is not a duplicated question..
Something like the following:
Maybe this helps as a starting point:
par(mar = rep(4, 4))
barData<-c(0.1,0.2,0.3,0.4) * 100
y <- lineData<-c(100,22,534,900);
x <- barplot(barData,
axes = FALSE,
col = "blue",
xlab = "",
ylab = "",
ylim = c(0, 100) )[, 1]
axis(1, at = x, labels = c("Julia", "Pat", "Max", "Norman"))
ats <- c(seq(0, 100, 15), 100); axis(4, at = ats, labels = paste0(ats, "%"), las = 2)
axis(3, at = x, labels = NA)
par(new = TRUE)
plot(x = x, y = y, type = "b", col = "red", axes = FALSE, xlab = "", ylab = "")
axis(2, at = c(pretty(lineData), max(lineData)), las = 2)
mtext(text="Lines of code by Programmer", side = 3, line = 1)
box()
I made a graph in which a barplot and a line plot are combined. The problem is that the scale of my secondary y-axis isn't how it should be.
This is the code I used:
barplot <- barplot(covpatient[[1]]$cov, names.arg = covpatient[[1]]$exon, xlab = covpatient[[1]]$gene[1] , ylab = "read depth" , border = gray.colors(length(unique(covpatient[[1]]$exon)))[as.factor(covpatient[[1]]$exon)])
par(new = TRUE)
lines(x = barplot, y = covpatient[[1]]$amplicon, bty = "n")
axis(side = 4, at = pretty(range(covpatient[[1]]$amplicon)))
And this is how my plot looks like:
The values of the lines plot are OK, but you see that the y-axis is not fully expanded. I want it to look the same as the y-axis on the left
Can someone help me with this?
Without a reproducible example and a clear question all answers will at best be gueswork but have a look at the following:
x <- 1:10
y <- c(1, 3, 5, 6, 2, 7, 11, 3, 2, 13)
z <- runif(10, min=1000, max=10000)
par(mar = c(5, 4, 4, 4) + 0.3)
barplot(y, col=rainbow(length(x)))
par(new = TRUE)
plot(x, z, type = "l", axes = FALSE, bty = "n", xlab = "", ylab = "")
axis(side=4, at = pretty(range(z)))
mtext("z", side=4, line=3)
library(plotrix)
twoord.plot(x,y,x,z,
xlab="x",
ylab="y",
rylab="z",
main="Main",
type=c("bar","l"),lcol=rainbow(length(x)),rcol=4)