I am trying to change the y-axis to being horizontal so that it can be read easier for my talk. I am new to R and don't have a lot of experience with it.
This is my code:
first.value = pt(500, 1.127344, 1.561328)
space = (1 - first.value)/648
edges = seq(first.value + space, 1 - space, space)
value.edges = 1.561328*qt(edges, 1.127344)
data = read.table(file.choose())
observed = log(data$V1)
expected = log(value.edges)
plot(observed,expected, xlim =c(4,15), ylim = c(0,14), las =1)
abline(0,1)
I would like the word expected the same orientation as observed. Thank you!
Try this,
par(mar = c(6,6,6,3))
plot(1, las=1, ylab="")
mtext(text="Test", side=2, line=4, las=1)
You can change the values in par to your liking, put your data instead of the 1 in plot, add your x labels etc... Then use mtext to write the yaxis label
Related
I would like to create a plot of this data, with x-axis increments of 500000 and with sampleIDs on the y-axis. The following code works to create the plot, but the y-axis labels don't work, and I am unsure how to code the x-axis ticks. Also, I had to add headings manually to the data file (and then obviously add header = TRUE when I assigned d) to get the code to work. I shouldn't have had to put the column titles in though should I since I use setNames?
d = read.delim("n_reads_per_sample.tsv", header = TRUE, sep = "\t")
xticks <- ( ? increments of 500000 to xmax ? )
dotchart(
sort(setNames(d$n_reads, d$X.sample)),
xlim = c(0, at = xticks, 1 max(d$n_reads)),
labels = dimnames(d[[1]])
,
main = "reads per sample",
xlab = "number of reads",
ylab = "sample"
)
In case the link doesn't work, this is what the file looks like.
x.sample n_reads
LT-145 3193621
LT-323 786578
LT-458 485543
LT-500 3689123
LT-95 3308764
LT-367 765972
LT-205 2090226
LT-245 10238727
I can't get at your full data right now, so I am just using your sample in the question.
Not sure what you mean that the y-axis labels don't work. They seem OK to me. You can get the x-axis labels that you want by suppressing the x-axis produced by dotchart and then making your own axis using the axis function. That requires a little fancy footwork with par. Also, unless you stretch out your graphics window, there will not be enough room to print all of the axis labels. I reduced the font size and stretched the window to get the graph below.
UpperLimit <- ceiling(max(d$n_reads)/500000)*500000
xticks <- seq(0,UpperLimit, 500000)
par(xaxt = "n")
dotchart(
sort(setNames(d$n_reads, d$X.sample)),
xlim=c(0, UpperLimit),
labels = dimnames(d[[1]]),
main = "reads per sample",
xlab = "number of reads",
ylab = "sample"
)
par(xaxt = "s")
axis(1, at=xticks, cex.axis=0.7)
My problem is with the legend. The legend is cut off, so it is missing two values. How do I go about moving legends to the title position, while still maintaining correct formating so that everything fits and is aligned to the plot?
My legends are of varying lengths so it would be great to have a way to always have them perfectly line up above the plot.
x<-c(1, 30,60)
y<-c(.001,.023,.03)
data<-cbind(x,y)
N<-100
plot_pdf_1<-function(data, ymax, big){
par(cex=big)
plot(data, type = "l", lwd=2,xaxt="n", yaxt="n", xaxs="i", yaxs="i", ylim=c(0,ymax))
my_at<- c(0,10,20,30,40,50,60,70,80,90) # to specify the tick marks
#initialize all points to zero first
#number of data points
N_o1_male<-0
N_o1_female<-0
N_o139_male<-0
N_o139_female<-0
N_non_male<-0
N_non_female<-0
#subscript format to be used in my legend
my.expressions <-c(as.expression(bquote('N'['1M']*' = '*.( N_o1_male))), as.expression(bquote('N'['1F']*' = '*.( N_o1_female))), as.expression(bquote('N'['2M']*' = '*.( N_o139_male))),as.expression(bquote('N'['2F']*' = '*.( N_o139_female))), as.expression(bquote('N'['3M']*' = '*.( N_non_male))),
as.expression(bquote('N'['3F']*' = '*.( N_non_female))))
par(xpd=TRUE)#to allow legend in outer margins
legend("topleft",legend=my.expressions,inset=c(0,-.11),
text.col="black",box.col=0, bty="n", cex = .75, lty= c( 1,2,1,2,1,2), col = c("purple","purple","blue","blue","black","black"),horiz = TRUE,seg.len = 1)
}
#formatting to plot in a two by two layout
op <- par(mfrow = c(2,2),
oma = c(5,4,0,0) + 0.1,
mar = c(0,0,1,.5) + 0.1)
#calls each plotting function and layout in a two by two
twobytwo<-function(data,ymax,big){
op
plot_pdf_1(data,ymax,big)
plot_pdf_1(data,ymax,big)
plot_pdf_1(data,ymax,big)
plot_pdf_1(data,ymax,big)
title(xlab = "Age (years)",
ylab = "Probability Density",
outer = TRUE, line = 3)}
twobytwo(data, ymax=.04, big=1) #calls the two by two function which lays out four plots in a two by two format. The plots share the same axis.
Three solutions:
Reduce font with cex
Remove the spaces on both sides of the ' = '
Widen your chart. You can set the chart size with win.graph() in
Windows or X11() or quartz() in other OS.
Using win.graph(width=11, h=7) before the op<-... call
I have a plot that I have added a second line to using lines() function but the added line does not fit within the default axis span so I want to increase the range of both axis so the whole of the new line fits. The code for the graph is the following:
plot(spa8, ci.type="line", col="black", lwd=2, ci.lty=2, ci.col="black",
ylab = "Species Richness", yaxp = c(0,60,6), xaxp = c(0,200,10),
cex.axis = 0.75, frame.plot=FALSE, xvar = "individuals") +
lines(spa7, ci.type="line", col="grey", lwd=2, ci.lty=2, ci.col="grey",
xvar = "individuals")+
box(bty="L")
I have tried using usr() and xaxp()but with no luck, I have also tried to set my own axis using the axis() function but that hasn't worked either. I'm sure I must be doing something wrong here as it seems like something that should be simple to do!
Can someone help me out?
Thanks
Perhaps you could play around with the xlim and ylim arguments within the plot function. For example, try running the code below (line by line) and look at the difference.
## Default.
plot(1:10)
## Set x-axis to be between -10 and 10.
plot(1:10, xlim = c(-10, 10))
## Set x-axis to be between -10 and 10, and y-axis to be between -5 and 5.
plot(1:10, xlim = c(-10, 10), ylim = c(-5, 5))
Hope that helps :)
I have this code:
# Plotting everything
plot( p1, col= "lightgreen", xlim=c(-2.5,4.5), ylim=c(0, 700), main="Daily Total Precipitation for AR and Oct-May", xlab="ln(x)" , ylab="Frequency", xaxt = "n") # first histogram
plot( p2, col="red", xlim=c(-2.5,4.5), ylim=c(0, 700), xaxt = "n" , add=T)
# Adding in text labels on top of the bars
text(x, y, paste(round(percents,2),"%"), cex=0.50, pos=3, offset=0.3, col="black")
axis(side=1, at=breaks) # new x-axis
# parameter that needs to be set to add a new graph on top of the other ones
par(new=T)
plot(x, percents, xlim=c(-2.5,4.5), type="l", col="yellow", lwd=3.0, axes=F, ylab=NA, xlab=NA)
axis(side=4, at=seq(0,100,by=10), col="yellow", col.axis="yellow") # additional y-axis
mtext("Percent", side=4, col="yellow")
# legend settings
legend("topleft", c("AR", "Oct-May"), lwd=10, col=c("red", "lightgreen"))
Which produces this graph:
And I can't seem to figure out how to get the secondary y-axis label to show up in the correct position. Any help or suggestions is greatly appreciated.
Edit: Using RStudio.
One option is to specify the line argument to mtext(). In the example below I add a couple more lines to the right (side = 4) margin of the plot using par(), and then I draw three labels using mtext() at the default (line = 0), line 3 (line = 3), and line -3 (line = -3):
op <- par(mar = c(5,4,4,4) + 0.1)
plot(1:10)
mtext("line0", side = 4)
mtext("line3", side = 4, line = 3)
mtext("line-3", side = 4, line = -3)
par(op)
Note that line numbers increase away from the plot region and that negative line values move into the plot region, or to the left of the right boundary of the plot region.
It takes a little playing with the number of margin lines (as set in par(mar = x)) and which line you want to draw on using mtext(), but a little trial and error should get you what you want.
Note also that you don't need to specify integer values for the line argument. You can specify fractions of lines too: line = 2.5.
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)