I want to know how to control the key scale in scatterplot with 3 axis in the openair package.
When I make scatter plot, the key scale is randomly applied according to data. I want to fix the key scale to 0~100 % RH.
scatterPlot(data,
x="O3",y="SOC",z="RH",col="jet",linear="FALSE",cex=0.8,fontsize=35,
xlim=c(0,0.05),ylim=c(0,20),key.fooer = "RH(%)", xlab="O3 (ppm)",
ylab="SOC(ug/m3)",labelFontsize=13)
To force the graph, you can create pseudo data outside the limits and with HR = 0. For example:
data <- data.frame("O3"=c(0.01, 0.02, 0.03, 0.04, 0.03),
"SOC"=c(5, 3, 4, 2, 8),
"RH"=c(100, 52, 75, 83, 63))
newdata <- rbind(data, data.frame("O3"=-1, "SOC"=-1, "RH"=0))
scatterPlot(newdata, x="O3",y="SOC",z="RH",col="jet",linear="FALSE",cex=0.8,fontsize=35,
xlim=c(0,0.05),ylim=c(0,20),key.footer = "RH(%)", xlab="O3 (ppm)",
ylab="SOC(ug/m3)",labelFontsize=13)
Related
I am trying to plot a volcano plot in R using the plot function and calibrate package in R and am trying to use the textxy function to plot only certain points.
Here is some data:
Metabolites <- data.frame(Metabolite = c("Glucose", "Galactose", "Creatine", "Lactose", "N-Acetylputrescine", "Tyramine", "Adenine", "Glycine", "Erythritol", "Choline"), Neg_pvalue = c(10, 8, 2, 1, 0.5, 0.7, 5, 3, 5.8, 4), LogFC = c(4, -3, 2, -1, 0.5, 0.7, 1, -2, -4, -1), padjust = c(1.453557e-19, 5.312771e-08, 4.983176e-02, 9.585447e-01, 2.449707e-01, 3.058580e-01, 4.223173e-02, 1.002379e-03, 4.466316e-27, 1.003879e-01))
Here is my code:
with(Metabolites, plot(LogFC, Neg_pvalue, pch=20, main="CNL", xlim=c(-5,6)))
with(subset(Metabolites, padjust <.05 ), points(LogFC, Neg_pvalue, pch=20, col="blue"))`
with(subset(Metabolites, padjust <.05 & abs(LogFC) > 2), points(LogFC, Neg_pvalue, ph=20, col="red"))
Now here is the issue:
with(subset(Metabolites, padjust <.05 & abs(LogFC) > 2), textxy(LogFC, Neg_pvalue, labs=Metabolite[1:3], cex=.5, offset = 0.2))`
If I plot this code, I get only the top 3 data points, as is indicated with the labs=Metabolite[1:3] part of the code. Alternatively, if I plot labs=Metabolite, then I get all labels.
If I wanted to plot the labels of only: Glycine, Lactose, and Erythritol as given in the Metabolites$Metabolite, am I able to do this?
Also, say I wanted to keep my top 3 data points labeled (labs=Metabolite[1:3]), but also want to label other metabolites of interest, say Tyramine and N-Acetylputrescine too; how can I do this?
This seems to work by slecting items that are in that set and using those character values as lables:
library(calibrate)
with(subset(Metabolites, Metabolite %in% c( 'Glycine', 'Lactose', 'Erythritol' )),
textxy(LogFC, Neg_pvalue, labs=c( 'Glycine', 'Lactose', 'Erythritol' ), cex=.5, offset = 0.2))
My graph displays correctly without using scale. I want to have it looks better so I convert factor to numeric then using scale_x_continuous. However, the graph looks incorrect when I convert from factor to numeric (How to convert a factor to an integer\numeric without a loss of information?). I can't use scale without converting to numeric. Please run a sample code below with and without these lines ( main$U <- as.numeric(as.character(main$U)), and + scale_x_continuous(name="Temperature", limits=c(0, 160)) ). Thank you.
library("ggplot2")
library("plyr")
df<-data.frame(U = c(25, 25, 25, 25, 25, 85, 85, 85, 125, 125),
V =c(1.03, 1.06, 1.1,1.08,1.87,1.56,1.75,1.82, 1.85, 1.90),
type=c(2,2,2,2,2,2,2,2,2,2))
df1<-data.frame(U = c(25, 25,25,85, 85, 85, 85, 125, 125,125),
V =c(1.13, 1.24,1.3,1.17, 1.66,1.76,1.89, 1.90, 1.95,1.97),
type=c(5,5,5,5,5,5,5,5,5,5))
df2<-data.frame(U = c(25, 25, 25, 85, 85,85,125, 125,125),
V =c(1.03, 1.06, 1.56,1.75,1.68,1.71,1.82, 1.85,1.88),
type=c(7,7,7,7,7,7,7,7,7))
main <- rbind(df,df1,df2)
main$type <- as.factor(main$type)
main <- transform(main, type = revalue(type,c("2"="type2", "5"="type5", "7" = "type7")))
main$U <- as.factor(main$U)
main$U <- as.numeric(as.character(main$U))
ggplot(main, aes(U, V,color=type)) +
geom_boxplot(width=0.5/length(unique(main$type)), size=.3, position="identity") +
scale_x_continuous(name="Temperature", limits=c(0, 160))
You have to specify the group in your call to geom_boxplot, and to keep the legend you can use color=factor(U) (i.e, converting U back). To not lose information on the groups that have the same x-values, I think it is best to create a new grouping column first. You take all unique pairs of U and type and create a new variable based on which row falls into which of these pairs.
main$U <- as.character(main$U)
main$type <- as.character(main$type)
grp_keys <- unique(as.matrix(main[, c("U", "type")]))
grp_inds <- 1:nrow(grp_keys)
main$grps <- apply(main, 1, function(x) {
grp_inds[colSums(as.character(x[c("U", "type")]) == t(grp_keys)) == length(c("U", "type"))]
})
Then, plotting (width adjusted because it looks very small with higher range),
main$U <- as.numeric(as.character(main$U))
ggplot(main, aes(U, V,color=type)) +
geom_boxplot(aes(group = grps, color = type), width=20/length(unique(main$type)), size=.3, position="identity") +
scale_x_continuous(name="Temperature", limits=c(0, 160))
I have a dataset that includes one point that is much higher in magnitude than the others; I am trying to use gap.plot to show this point on the graph without introducing unnecessary white-space. However, my line plot skips over the point in the top panel of the graph, leaving it isolated.
Code is as follows:
year<-c(2001:2015)
y<-c(30, 13, 0, 0, 0, 1, 5, 309, 7, 1, 58, 1, 62, 69, 49)
par(bty="n") #Deletes box
gap.plot(
year,y
, type="b"
, ylim=c(0,325)
, gap=c(100,275)
, gap.axis="y"
, xaxt = "n"
, xtics =NA
, xticlab=NA
, xlim =c(2001,2015)
, xlab="Year"
, ylab="Positive Samples"
, pch=15
)
axis(1,at=c(2001:2015))
abline(h=seq(100,103), col="white",lwd=5) #Eliminates horizontal lines
axis.break(axis=2,breakpos=100,style="slash")
I end up with the following graph:
What am I doing wrong?
I have got dive depth data for seabirds over several trips and I would like to find the modes for each trip, plot the density functions and a line corresponding to the modes. So far, here's the code I have been using:
maxdepths<-read.csv("maximum_depths.csv", header=T)
maxdepths_ind21<-maxdepths[maxdepths$bird=="21",]
# create value labels
trip.f <- factor(maxdepths_ind21$trip, levels= c(21.1,21.2,21.3,21.4,21.5,21.6,21.7,21.8),
labels = c("Trip1", "Trip2", "Trip3", "Trip4", "Trip5", "Trip6", "Trip7", "Trip8"))
# plot densities
z<-sm.density.compare(maxdepths_ind21$maxdep, maxdepths_ind21$trip,model="equal")
sm.density.compare(maxdepths_ind21$maxdep, maxdepths_ind21$trip, xlab="Maximum depth (m)", xlim=c(0, 90), axes=F)
title(main="Maximum dive depth by trip, individu 21")
axis(side = 1, at = c(0,5,10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90))
axis(side = 2, at = c(0,0.01,0.02,0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 1))
# add legend via mouse click
colfill<-c(2:(2+length(levels(trip.f))))
legend(locator(1), levels(trip.f), fill=colfill)
The result looks good, ie I've got one curve per trip with different colours/line types per trip.
I now would like to draw lines for each trip when the density functions are maximized, as well as find those values. I am aware of this thread
R: getting data (instead of plot) back from sm.density.compare
and I have tried assigning the result of sm.density.compare to an object and then calling it, like so:
z<-sm.density.compare(maxdepths_ind21$maxdep, maxdepths_ind21$trip,model="equal")
z
I was looking for the values of the modes within this output but I got confused by all the values that are returned.
Any help would be much appreciated!
TIA
I used DoseFinds to building the two models and I want to
plot both model on the same graph to compare.
library(DoseFinding)
doses <- c(0, 10, 25, 50, 100, 150)
fmodels <- Mods(emax = 25,
doses=doses, placEff = 0.5, maxEff = -0.4,
addArgs=list(scal=200))
fmodels2 <- Mods(emax = 25,
doses=doses, placEff = -1.5, maxEff = -1.4,
addArgs=list(scal=200))
plot(fmodels)
plot(fmodels2)
Combine the two things into one object:
doses <- c(0, 10, 25, 50, 100, 150)
fmodels2 <- Mods(emax = c(25,25),
doses=doses, placEff = c(0.5,-1.5), maxEff = c(-0.4,-1.4),
addArgs=list(scal=200))
then plot with superpose=TRUE:
plot(fmodels2, superpose=TRUE)
The two lines don't overlap much so although it looks like two separate graphs, it isnt!
I guess you want to use superpose = TRUE when you call the plot-function (?plot.Mods). This will plot the models in the same graph if they are in the same Mods-object. See ?Mods for how to have more than one model in the same object.