Related
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 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)
})
I am new to R, and I am trying to create a conditional probability plot, with pre-test probability on the x axis and post-test probability on the y axis. Similar to the one in the link conditional probability plot. I need to plot points for a positive test and join them together with a line, and plot points for a negative test and join the points together with a line, on the same graph.
I have the data:
Pre-test prob for negative test <- c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
Post-test prob for negative test <- c(0, 3, 7, 11, 17, 22, 30, 40, 53, 72, 100)
Pre-test prob for positive test < - c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
Post-test prob for positive test <- c(0, 38, 57, 69, 77, 83, 88, 94, 95, 98, 100)
However I am unsure how best to organise the data or of the code to produce the graph that I need! I have searched for "conditional probability plots" but haven't found anything helpful.
Any guidance would be much appreciated.
Thanks, Laura
The best way to organise the data is inside a data.frame:
test = data.frame(Pos.pre = a, Pos.post = b, Neg.pre = c, Neg.post = d)
(Assuming your individual data was called a, b, c, d.)
Now you can plot, e.g. positive post vs pre:
plot(Pos.post ~ Pos.pre, data = test, type = 'l')
(type = 'l' makes this a line plot.)
And you can add the negative results using the lines function, which adds data to an existing plot:
lines(Neg.post ~ Neg.pre, test, col = 'red')
Here, I’ve taken the liberty of making the second line red. Take a look at the documentation of plot, lines and par for many more options.
Once you have the time, I strongly urge you to learn using the ggplot2 library, which makes these kinds of plots more flexible. Case in point, with ggplot2 we could create the above plot in a single, extensible command:
ggplot(test) +
geom_line(aes(x = Pos.pre, y = Pos.post)) +
geom_line(aes(x = Neg.pre, y = Neg.post), color = 'red')
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:
I'm trying to make a grouped barplot in r, but there are some things I cannot figure out. This is what I have so far:
I would like:
to create a matrix from the data.frame (.csv file, see below)
the ablines to appear, but not in front of the bars
labels for the grouped bars (November, December, January, ... ->see data below)
for the plot layout to be as shown below. (I basically want the plot border)
I used the following code:
x<-matrix(nrow=3,ncol=7, data=c(200,227,196,210,279,319,220,126,111,230,196,123,240,106,94,250,154,233,260,226,218))
tiff("p_month_all.tiff", width=600, height=300)
par(mar=c(5,4,0.5,0.5))
a=c("November","December","January","February","March","April","May")
barplot(x, beside=TRUE, ylim=c(0,350),xlab="Month", axes=TRUE,axis.lty=1, ylab="Monthly Precipitation [mm]", col=c("darkblue","dodgerblue3","deepskyblue1"),panel.first= abline(h = c(50,100,150,200,250,300), col = "grey", lty = 2), xaxt="n", yaxt="n")
par(ps=12, cex =1, cex.main=2)
axis(2, c(0,350, c(50, 100, 150, 200, 250, 300)), las=1)
dev.off()
The data set (.csv file) looks like this:
Month Hornberg Strick Huetten
November 120 278 234
December 279 156 145
January 328 300 299
February 267 259 234
March 190 201 187
April 150 199 177
May 147 156 160
I've rewritten your code for clarity so you can see more easily what the problem is.
You were suppressing the axes with xaxt = "n" and yaxt = "n". I removed those lines.
Adding a call to box draws the box around the plot.
Adding a call to grid draws gridlines in the plot.
I've added row and column names to your data matrix so the plot know what to use in the axes.
I've updated the plot margins.
I also tidied a few bits like replacing month names with month.name and using seq.int rather than a hard-coded sequence.
x <- matrix(
c(
200, 227, 196,
210, 279, 319,
220, 126, 111,
230, 196, 123,
240, 106, 94,
250, 154, 233,
260, 226, 218
),
nrow = 3,
ncol = 7
)
colnames(x) <- month.name[c(11:12, 1:5)]
rownames(x) <- c("Hornberg", "Strick", "Huetten")
par(mar = c(5, 4, 1.5, 0.5), ps = 12, cex = 1, cex.main = 2, las = 1)
barplot(
x,
beside = TRUE,
ylim = c(0,350),
xlab = "Month",
axes = TRUE,
axis.lty = 1,
ylab = "Monthly Precipitation [mm]",
col = c("darkblue", "dodgerblue3", "deepskyblue1"),
panel.first = abline(
h = seq.int(50, 300, 50),
col = "grey",
lty = 2
)
)
box()
grid()
So, first of all, look through ggplot2 documentation, it's pretty good http://docs.ggplot2.org/0.9.3.1/index.html
If you haven't found an answer for your question, never give up googling :)
Ok, about your question:
Create data
help(read.csv) -> import your data to data.frame named x
Prepare data for the plot:
Melt your data to use it for the plot
x<-melt(x)
Use Month variable as a factor and order by month:
x$Month=factor(x$Month,level=month.name)
x<-x[order(x$Month),]
Plot the graph using ggplot2 (as you tagged it here and it's straitforward in use)
ggplot(x,aes(x=Month,y=value,fill=variable))+geom_bar(stat="bin",position="dodge")+theme_bw()+ylab("Monthly Precipitation [mm]")+xlab("Month")
For the colours, can use scale_fill_brewer() (great tutorials here:http://www.cookbook-r.com/Graphs/Colors_%28ggplot2%29/)
ggplot(x,aes(x=Month,y=value,fill=variable))+geom_bar(stat="bin",position="dodge")+theme_bw()+ylab("Monthly Precipitation [mm]")+xlab("Month")+scale_fill_brewer(palette="Blues")