This can be plotted very easily.
xvals <- c(5.5, 15.5, 25.5, 35.5, 45.5, 55.5, 65.5, 75.5, 85.5, 95.5)
yvals <- c(81, 63, 45, 27, 9, -9, -27, -45, -63, -81)
xn <- rep(1000, 10)
plot(xvals, yvals)
Both xvals and xn share the same yvals so I want to plot in one graph:
yaxis:
xaxis lower:xvals
xaxis upper:xn
I simply want to add xn to the same plot as an xaxis (the upper) axis(3).
Any idea on this!
Leave your code as it is up to plot(xvals, yvals). Then add the following:
#plot the first plot
plot(xvals, yvals)
#start new overlaid plot
par(new=TRUE)
#plot xn but remove the xaxis and the x label for now
plot(xn, yvals, xaxt='n', xlab='')
#add those at the top of the graph (3)
axis(3, xn)
#bonus. add this line to add a secondary xlabel on top
mtext(side = 3, line = 2, "xn")
Result:
Related
Would you advise on how I could make an even display of unevenly spaced number on a graph. For example, considering the code below :
BREAKS = c(0, 0.1, 1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500)
a <- seq(0,100,0.1)
b <- seq(0,1000,0.1)
plot(ecdf(a), col="red", xlim=c(0,100), main=NA, breaks=BREAKS)
plot(ecdf(b), col="green", xlim=c(0,100), add=T, breaks=BREAKS)
I would like to show on X-axis (0, 0.1, 1 and 10) spaced in an equal/even manner.
W = c(20000, 5000, 3000, 8, 2, 0.5)
BMR = c(19000, 12000, 960, 86, 30, 10)
BMRPlot <- plot(W, BMR, main='Graph 2', cex=1.25, pch=21, bg='blue', lwd=1)
The above is the data I am trying to plot, however as you can probably tell the final data points once plotted appear to be indistinguishable as they are so close together. What could I add to my line of code that would change the view of this so that all points could be visible?
In a situation like this, you more or less have to use a transformation to make all the points visible. Otherwise, the points would have to be incredibly small not to overlap, and then you wouldn't be able to see them.
A log transformation of x and y seems to work here.
logW = log(c(20000, 5000, 3000, 8, 2, 0.5))
logBMR = log(c(19000, 12000, 960, 86, 30, 10))
BMRPlot <- plot(logW, logBMR, main='Graph 2', cex=1.25, pch=21, bg='blue', lwd=1)
As noted by commenter below, you can do the log-transform within the plot statement if you want your tick values to be untransformed:
W = c(20000, 5000, 3000, 8, 2, 0.5)
BMR = c(19000, 12000, 960, 86, 30, 10)
BMRPlot <- plot(W, BMR, main='Graph 2', cex=1.25, pch=21, bg='blue', lwd=1, log="xy")
I want to have one figure with two plots, one of them is a ggplot2 object, and the second is a plot generated with gplots. For example combine the next two plots in a row:
library(ggplot2)
library(gplots) #For plotmeans
df = structure(list(age = c(14, 22, 35, 21, 88, 66, 14, 22, 35, 21),
values = c(22, 8, 1.9, 26.8, 32, 15.,1.9, 26.8, 32, 15.)),
.Names = c("age", "values"),
row.names = 1:10,
class = "data.frame")
ggplot(df, aes(values)) + geom_histogram()
plotmeans(df$values ~ df$age)
I tried grid, gridExtra, par and layout but w/o success.
Any idea how can I do so?
I found the next solution using gridBase:
(based on https://stackoverflow.com/a/14125565/890739)
library(gridBase) # To combine two plots
par(mfrow=c(1, 2))
plot.new()
vps <- baseViewports()
pushViewport(vps$figure)
vp1 <-plotViewport(c(1.8,1,0,1))
#Plot histogram
g1 <- ggplot(df, aes(values)) + geom_histogram()
print(g1,vp = vp1)
plotmeans(df$values ~ df$age)
Is there a simpler way?
I have the following bar chart produced using this code:
MD1<-read.csv("MD_qual_OTU_sorted.csv")
MD1<-data.frame(Samples=c("A","B","C","D","E","F","G","H","I","J","K","L","M", "N","O","P","Q", "R"), Number.of.OTUs=c(13,10,9,9,15,11,7,7,9,9,5,10,10,7,15,17,8,9))
par(las=1)
barplot(MD1[,2],names.arg=MD1[,1], ylab='OTU Count', yaxt='n', xlab='MD samples', main='Total OTU count/Sample',density=c(90,90, 90, 90, 90, 90, 10, 10, 10, 10, 10, 10, 40, 40, 40, 40, 40, 40), col=c("yellow","yellow","pink", "pink","green","green","red","red", "purple", "purple", "blue", "blue", "orange", "orange","cyan", "cyan","chartreuse4", "chartreuse4" ))
usr <- par("usr")
par(usr=c(usr[1:2], 0, 20))
axis(2, at=seq(0,20,5))
I want to split samples A-F into a separate group (Day 3), G-L (Day 5) and M-R (Day 15)
There are similar questions posted however I am not sure how to tidy up the manner in which I have inputted my data to be able to use these solutions.
You could consider using ggplot2, separate plots are very easy using facet_wrap and facet_grid.
library(ggplot2)
#create a grouping variable
MD1$Day <- rep(c("Day 03","Day 05","Day 15"),
each=6)
p1 <- ggplot(MD1, aes(x=Samples,y=Number.of.OTUs)) +
geom_bar(stat="identity") + facet_wrap(~Day,
scales="free_x")
p1
Or, if you want to use base-R and approach your original image:
#add colors/densities
MD1$col <- c("yellow","yellow","pink", "pink","green","green","red","red",
"purple", "purple", "blue", "blue", "orange", "orange","cyan", "cyan","chartreuse4", "chartreuse4" )
MD1$density <- c(90,90, 90, 90, 90, 90, 10, 10, 10, 10, 10, 10, 40, 40, 40, 40, 40, 40)
#set 1 row three cols for plotting
par(mfrow=c(1,3))
#split and plot
lapply(split(MD1, MD1$Day),function(x){
barplot(x[,2],
names.arg=x[,1],
ylab='OTU Count',
ylim=c(0,20),
main=unique(x$Day),
col=x$col,
density=x$density)
})
For the following sample code:
y <- c(23, 34, 11, 9.6, 26, 31, 38, 38, 30, 36, 31)
days <- seq(as.Date("2015-2-25"), by="day", length=11)
n <- length(y)
x <- 1:n
plot(x, y, type='n', xlab="Days", ylab="Y", xaxt='n')
axis(1, at=seq(1,11) ,labels=format(days, "%d"), las=1)
lines(y)
I have the following chart:
What I want is when the month changes, I want to be able to add the month name below the day on the x axis. So in this example, when it becomes 01, it should show 01 Mar (March on a separate line)
It can happen if you do something like this:
Your data plus the month vector:
y <- c(23, 34, 11, 9.6, 26, 31, 38, 38, 30, 36, 31)
days <- seq(as.Date("2015-2-25"), by="day", length=11)
#my addition
#contains the name of the month for where day == '01' else is blank
months <- ifelse(format(days, '%d')=='01', months(days) , '')
n <- length(y)
x <- 1:n
Solution:
plot(x, y, type='n', xlab="Days", ylab="Y", xaxt='n')
axis(1, at=seq(1,11) ,labels=format(days, "%d"), las=1)
lines(y)
Up to here is only your code. Now you need to add a new axis, set the axis colour to white and plot the month vector created above:
par(new=T) #new plot
par(mar=c(4,4,4,2)) #set the margins. Original are 5,4,4,2.
#I only changed the bottom margin i.e. the first number from 5 to 4
#plot the new axis as blank (colour = 'white')
axis(1, at=seq(1,11) ,labels=months, las=1, col='white')
The result looks like what you asked for: