Related
I was trying to follow this tutorial (https://popgen.nescent.org/2018-03-27_RDA_GEA.html) and plot the RDA, but I would like to remove the two dashed lines (x=0 and y=0). Does anyone know how to get rid of them?
This is the graph I'm talking about
According to this post you can alter a plot.rda() to remove the dotted lines if you build the plot up yourself from scratch, but it's a complicated/challenging task. The easiest/best solution in my opinion is to draw white lines over the dotted lines with abline(h = 0, v = 0, col = "white", lwd = 2) and redraw the plot borders with box() before you plot the points/lines. See the ## PLOTTING ## section below for an example:
## OBTAIN & LOAD THE DATA ##
#install.packages(c("psych","vegan"), dependencies=TRUE)
library(psych) # Used to investigate correlations among predictors
library(vegan) # Used to run RDA
temp <- tempfile()
download.file("https://github.com/NESCent/popgenInfo/blob/master/data/wolf_geno_samp_10000.zip?raw=true",
temp)
gen <- read.csv(unzip(temp, "wolf_geno_samp_10000.csv"), row.names=1)
dim(gen)
sum(is.na(gen))
gen.imp <- apply(gen,
2,
function(x) replace(x,
is.na(x),
as.numeric(names(which.max(table(x))))))
sum(is.na(gen.imp)) # No NAs
env <- read.csv(url("https://raw.githubusercontent.com/NESCent/popgenInfo/master/data/wolf_env.csv"))
str(env)
env$individual <- as.character(env$individual)
env$land_cover <- as.factor(env$land_cover)
identical(rownames(gen.imp), env[,1])
pairs.panels(env[,5:16], scale=T)
pred <- subset(env, select=-c(precip_coldest_quarter, max_temp_warmest_month, min_temp_coldest_month))
table(pred$land_cover)
pred <- subset(pred, select=-c(land_cover))
pred <- pred[,5:12]
colnames(pred) <- c("AMT","MDR","sdT","AP","cvP","NDVI","Elev","Tree")
pairs.panels(pred, scale=T)
wolf.rda <- rda(gen.imp ~ ., data=pred, scale=T)
wolf.rda
RsquareAdj(wolf.rda)
summary(eigenvals(wolf.rda, model = "constrained"))
screeplot(wolf.rda)
signif.full <- anova.cca(wolf.rda, parallel=getOption("mc.cores"))
signif.full
signif.axis <- anova.cca(wolf.rda, by="axis", parallel=getOption("mc.cores"))
signif.axis
vif.cca(wolf.rda)
plot(wolf.rda, scaling=3)
plot(wolf.rda, choices = c(1, 3), scaling=3)
levels(env$ecotype) <- c("Western Forest","Boreal Forest","Arctic","High Arctic","British Columbia","Atlantic Forest")
eco <- env$ecotype
bg <- c("#ff7f00","#1f78b4","#ffff33","#a6cee3","#33a02c","#e31a1c")
## PLOTTING ##
plot(wolf.rda, type="n", scaling=3)
abline(h = 0, v = 0, col = "white", lwd = 2)
box()
points(wolf.rda, display="species", pch=20, cex=0.7, col="gray32", scaling=3) # the SNPs
points(wolf.rda, display="sites", pch=21, cex=1.3, col="gray32", scaling=3, bg=bg[eco]) # the wolves
text(wolf.rda, scaling=3, display="bp", col="#0868ac", cex=1) # the predictors
legend("bottomright", legend=levels(eco), bty="n", col="gray32", pch=21, cex=1, pt.bg=bg)
plot(wolf.rda, type="n", scaling=3, choices=c(1,3))
abline(h = 0, v = 0, col = "white", lwd = 2)
box()
points(wolf.rda, display="species", pch=20, cex=0.7, col="gray32", scaling=3, choices=c(1,3))
points(wolf.rda, display="sites", pch=21, cex=1.3, col="gray32", scaling=3, bg=bg[eco], choices=c(1,3))
text(wolf.rda, scaling=3, display="bp", col="#0868ac", cex=1, choices=c(1,3))
legend("topleft", legend=levels(eco), bty="n", col="gray32", pch=21, cex=1, pt.bg=bg)
Also, in future, if you could please post the code required to obtain and load the data or a minimal, reproducible example, it would have made this question a lot easier to answer; see How to make a great R reproducible example
I am trying to pull charts (.png) from the data from SQL Server using ODBC in R. The code is running fine but label on y-axis is in decimals. eg. 10.0%, 20.0%, 30.0%, 40.0% and 50.0%.
I do not want this in decimal place on y-axis. Please help me to get rid of this issue.
Please see my below code and help me with this issue.
Output Chart
# produce plots for all the pids
for (i in 1:n_pids){
print(paste0('working on ', i, ' pid ', def_dates$pid_names[i]))
pidx <- def_dates[i,'pid']
sub <- res[pid == pidx]
png(file = paste0(pidx, '-misrated.png'), width = 750, height = 350, units = "px", pointsize = 12 )
par(oma = c(5, 1,1,1), mar=c(0,3,3,3))
with(sub, plot(date, edf1/100,
type="l", col="blue",
ylim=c(0,.5), bty='n',
xlab=NA, ylab=NA, cex=0.9,lwd=2,
axes=F,xaxs = "i",yaxs="i",
panel.first =
c(abline(h = seq(0,1,.1), lty = 2, col = 'grey')
,NULL)
))
abline(v=as.Date(def_dates[def_dates$pid == pidx,'def'],'%m-%d-%Y'),
col="red",lwd=2)
axis.Date(1, at = seq(min(sub$date), max(sub$date), length.out = 10),
format= "%b %Y") # bottom dates
var1= seq(0,0.5,.1)#pretty_breaks(n=5)(sub$edf1/100)
axis(2, lwd=0,
at=var1,
label=percent(var1),
las=1) # EDF values
par(new=T)
with(sub, plot(date, displaystring, type="l",
col="darkgreen", lwd=2,
axes=F, xlab=NA, ylab=NA))
axis(side = 4,
at=seq(1,NROW(unique(sub$displaystring))),
labels=unique(sub$displaystring),
lwd=0,cex=0.5,las=1,
col="darkgreen"
)
par(oma = c(0,0,0,0))
legend("bottom", legend=c("DENBURY RESOURCES INC [1-Yr EDF9]","MIS RATING"), xpd = TRUE,
horiz = TRUE,
inset = c(0,0), bty = "n",
col=c("blue", "darkgreen"), lty=1, cex = 1)
dev.off()
#ggsave(paste0(pidx, '-rated.png'), p, scale=3, height=3, width=6, unit="in")
}
I can't vouch for the rest of the code, but the only portion that has affect on the y-axis here is:
axis(2, lwd=0,
at=var1,
label=percent(var1),
las=1) # EDF values
When you find scales::percent, the help page does not appear to give an easy way to control the decimal places. I suggest we use good-ole sprintf for this.
scales::percent(var1)
# [1] "0.0%" "10.0%" "20.0%" "30.0%" "40.0%" "50.0%"
sprintf("%0.0f%%", 100*var1)
# [1] "0%" "10%" "20%" "30%" "40%" "50%"
So your resulting code will be:
axis(2, lwd=0,
at=var1,
label=sprintf("%0.0f%%", 100*var1),
las=1) # EDF values
I want to combine three plots that I made using the following function
#FUNCTION
dlogistic<-function(k=300,rd=0.599,NO1=2,t=20){
N<-c(NO1,numeric(t))
for(i in 1:t){
N[i + 1] <- N[i] + rd* N[i] * (1-N[i]/k)
}
return(N)
}
#Plot #1
Nts <- dlogistic()
Nts
#Plot #2
K450<-300*1.5
n450=dlogistic(k=K450)
n450
#Plot#3
k05<-300*0.5
n05=dlogistic(k=k05)
n05
The first plot is named Nts, the second, n0450, and the last, n05. Also, I want to use the matplot command somehow.
Here's one using base R. First plot one of them and then add others using lines. Make sure you set the ylim to encompass the whole range of data.
graphics.off()
windows(width = 6, height = 6)
plot(Nts, type = "l", ylim = c(min(Nts, n450, n05), max(Nts, n450, n05)))
lines(n450, type = "l", col = "red")
lines(n05, type = "l", col = "blue")
legend('topleft', legend = c("Nts", "n450", "n05"), lty = 1, col = c("black", "red", "blue"))
With matplot+matlines:
matplot(1:length(Nts), cbind(Nts, n450, n05), pch=19, xlab='x', ylab='y')
matlines(1:length(Nts), cbind(Nts, n450, n05), xlab='x', ylab='y')
legend('topleft', legend=c('Nts','n450','n05'), col=1:3, pch=19, lwd=1, lty=1:3)
With matplot only:
matplot(1:length(Nts), cbind(Nts, n450, n05), type='l', xlab='x', ylab='y')
legend('topleft', legend=c('Nts','n450','n05'), col=1:3, lwd=1, lty=1:3)
With ggplot:
library(ggplot2)
ggplot() +
geom_line(aes(x=1:length(Nts), y=Nts, col='Nts')) +
geom_line(aes(x=1:length(n450), y=n450, col='n450')) +
geom_line(aes(x=1:length(n05), y=n05, col='n05')) +
xlab('x')+
ylab('y')+
theme_bw()
ENV
R version 3.3.1
MAC OSX 10.9.4
I would like to plot a style like figure below, which is plotted by matlab.
There is full grid on the plot with customized axis range (e.g. $10^0~10^{-4}$) and axis label (e.g. 10^0 10^1 10^-2 10^-3 10^-4 10^-5). There are ten ticks between 10^0 and 10^1 and also other labels. Similar for y axis.
Expected:
I tried:
initial.dir<-getwd()
setwd("/Rworks/bin")
sink("r.o")
pk <- read.table("2017.file)
rownames(pk)<-c("k","pk")
d.f <- data.frame(t(pk))
png(file="m.png")
plot(
d.f$k,
d.f$pk,
type = "n",
log = "xy",
xlim = c( 10^0, 10^2),
ylim = c( 0.00001, 1),
)
lines( d.f$k, d.f$pk, col = "green4", lty = "dotted")
points( d.f$k, d.f$pk, bg = "limegreen", pch = 21 )
box()
dev.off
sink()
setwd(initial.dir)
I got:
The axis and axis label and the ticks and grid is not what I want. Can anyone can give an advices? Thanks.
Worst case scenario, you can just draw the axes and background lines yourself.
plot(
x=c(1,2), y=c(0.6,0.2),
pch=21, bg="red",
log = "xy",
xlim = c( 10^0, 10^2),
ylim = c( 0.00001, 1),
xaxt="n", yaxt="n",
xlab="", ylab="",
yaxs="i"
)
lines(x=c(1,2), y=c(0.6,0.2))
axis(1, at=10^(0:2),
labels=expression(10^0, 10^1, 10^2))
axis(2, at=10^(-5:0), las=1,
labels=expression(10^-5, 10^-4, 10^-3, 10^-2, 10^-1, 10^0))
abline(h=outer((1:10),(10^(-5:-1))), col="#00000033", lty=2)
abline(v=outer((1:10),(10^(0:1))), col="#00000033", lty=2)
Here's an example - it's not exactly what you want (e.g. you could play around with theme options such as panel.grid.minor to get dotted grid lines), but it's most of the way there.
Exponential-format axis tick labels, from here:
fancy_scientific <- function(l) {
# turn in to character string in scientific notation
l <- format(l, scientific = TRUE)
# quote the part before the exponent to keep all the digits
l <- gsub("^(.*)e", "'\\1'e", l)
# turn the 'e+' into plotmath format
l <- gsub("e", "%*%10^", l)
# return this as an expression
parse(text=l)
}
Manual ticks from #G5W's answer: might be possible to write a function to do this automatically, or one might exist somewhere.
yticks = outer((1:10),(10^(-5:-1)))
xticks = outer((1:10),(10^(0:1)))
Draw the plot (with #G5W's sample mini-data)
library(ggplot2)
ggplot(data.frame(x=1:2,y=c(0.6,0.2)),
aes(x,y))+
geom_point(colour="red")+
scale_x_log10(limits=c(1,100),labels=fancy_scientific,
minor_breaks=xticks)+
scale_y_log10(limits=c(1e-5,1),labels=fancy_scientific,
minor_breaks=yticks)+
theme_bw()
To plot the empirical cumulative density of three variables, x1, x2 and x3, I used the following in r:
plot.ecdf(x1, col="blue",
main="Distribution XYZ",
xlab="x_i", ylab="Prob(x_i<=y)")
lines(ecdf(x2), col="red") # adds a line
lines(ecdf(x3), col="green") # adds line
legend(600,0.6, # places a legend from (x,y)=(600,0.6) on
c("x1","x2","x3"), # puts text in the legend
lty=c(1,1,1), # gives the legend appropriate symbols (lines)
lwd=c(1,1,1),col=c("blue","red","green")) # gives the legend lines the correct color and width
The resulting plot however has two horizontal lines (broken lines) at 0 and 1 besides the box. And, the origin of the box has a space below zero on the vertical axis and a space to the left of zero on the horizontal axis. May you suggest how to remove these space and the additional lines. I wanted but could not post the plot.
EDITED:
A sample data can be generated as follows:
sample data
n <- 1000; u <- runif(n)
a <- -4.46; b <- 1.6; c <- -4.63
d <- ( a * u ) + (b * ( ( 1.5 * ( u ** 2 )) - 0.5 )) + (c * ( (2.5 * (u ** 3)) - (1.5 * u )))
x1 <- -126/d; x2 <- -131/d; x3 <- -187/d
It sounds like you are asking for the style provided by different settings for 'xaxs' and 'yaxs', and maybe 'xlim':
Try:
plot.ecdf(x1, col="blue",
main="Distribution XYZ",
xlab="x_i", ylab="Prob(x_i<=y)",
xaxs="i",yaxs="i", xlim=c(0,1000) )
lines(ecdf(x2), col="red")
lines(ecdf(x3), col="green")
legend(600,0.6,
c("x1","x2","x3"),
lty=c(1,1,1),
lwd=c(1,1,1),col=c("blue","red","green"))
use the yaxs = "i" argument:
plot.ecdf(x1, col="blue",
main="Distribution XYZ",
xlab="x_i", ylab="Prob(x_i<=y)", yaxs = "i")
This will align the axis at the ylim points (which supposedly are 0 and 1 for plot.ecdf).
If you also want to remove the dotted line at 0 and 1, just call box():
box()
You could use the arguments xlim, ylim and col.01line:
x1 = runif(100)
x2 = runif(100)
x3 = runif(100)
plot.ecdf(x1, col="blue", main="Distribution XYZ",xlab="x_i", ylab="Prob(x_i<=y)", ylim=c(0, 1), xlim=c(0,1), col.01line="white", verticals=FALSE)
lines(ecdf(x2), col="red", col.01line="white")
lines(ecdf(x3), col="green", col.01line="white")
legend(600,0.6,c("x1","x2","x3"), lty=c(1,1,1), lwd=c(1,1,1),col=c("blue","red","green"))
Alternatively, you could just use the generic plot:
f1 = ecdf(x1)
f2 = ecdf(x2)
f3 = ecdf(x3)
pts = seq(0, 1, 0.01)
plot(pts, f1(pts), col="blue", type="b", xlab="x_i", ylab="Prob(x_i<=y)", pch=16, main="Distribution XYZ")
lines(pts, f2(pts), col="red", type="b", pch=16)
lines(pts, f3(pts), col="green", type="b", pch=16)
legend("topleft", c("x1","x2","x3"), fill=c("blue","red","green"))