I want to make a histogram for multiple variables.
I used the following code :
set.seed(2)
dataOne <- runif(10)
dataTwo <- runif(10)
dataThree <- runif(10)
one <- hist(dataOne, plot=FALSE)
two <- hist(dataTwo, plot=FALSE)
three <- hist(dataThree, plot=FALSE)
plot(one, xlab="Beta Values", ylab="Frequency",
labels=TRUE, col="blue", xlim=c(0,1))
plot(two, col='green', add=TRUE)
plot(three, col='red', add=TRUE)
But the problem is that they cover each other, as shown below.
I just want them to be added to each other (showing the bars over each other) i.e. not overlapping/ not covering each other.
How can I do this ?
Try replacing your last three lines by:
plot(One, xlab = "Beta Values", ylab = "Frequency", col = "blue")
points(Two, col = 'green')
points(Three, col = 'red')
The first time you need to call plot. But the next time you call plot it will start a new plot which means you lose the first data. Instead you want to add more data to it either with scatter chart using points, or with a line chart using lines.
It's not quite clear what you are looking for here.
One approach is to place the plots in separate plotting spaces:
par("mfcol"=c(3, 1))
hist(dataOne, col="blue")
hist(dataTwo, col="green")
hist(dataThree, col="red")
par("mfcol"=c(1, 1))
Is this what you're after?
Related
I am plotting a SpatialPointsDataFrame object of archaeological finds over a shapefile of the trench they were found in. So far I have managed to load and plot both sets of data together with:
coordinates(finds) <- ~x+y
trencharea <- readOGR(dsn="excpoly", layer="excpoly")
trencharea <- as.owin(trencharea)
plot(trencharea, main= "Trench & Finds")
plot(finds, add=TRUE, col = "blue", pch = 4)
However, I also want the x and y coordinates associated with the finds to be displayed on the x and y axes of my plot. How do I do this? I'm not looking for a solution that involves ggplot
I don't know if I understood correctly, are you looking for the following
plot(trencharea, main= "Trench & Finds")
plot(finds, add=TRUE, col = "blue", pch = 4)
axis(1)
axis(2)
box(col = 'black')
Dummy plot
I am a beginner with R. I managed to plot my data into overlapping histograms. However, I would like to place all the histograms on one page. I am struggling as I am not able to tell R, which sets to pick (only manage to plot one of the plots).
This is the code:
df<-read.csv("Salt dshalo sizes.csv",header=T)
#View(df)
library(ggplot2)
DSA<-df[,1]
DS1<-df[,2]
DSB<-df[,5]
DS2<-df[,6]
DSC<-df[,9]
DS3<-df[,10]
#remove the NA column by columns separately or it will chop the data
DSA=na.omit(DSA)
DS1=na.omit(DS1)
DSB=na.omit(DSB)
DS2=na.omit(DS2)
DSC=na.omit(DSC)
DS3=na.omit(DS3)
#plot histograms for DSA, DSB and DSC on one same graph
hist(DSA, prob=TRUE, main="Controls", xlab="Sizes (um)", ylab="Frequency", col="yellowgreen",xlim= c(5,25), ylim=c(0,0.5), breaks=10)
hist(DSB, prob=TRUE, col=rgb(0,0,1,0.5),add=T)
hist(DSC, prob=TRUE, col=rgb(0.8,0,1,0.5),add=T)
#add a legend to the histogram
legend("topright", c("Control 1", "Control2", "Control3"), text.width=c(1,1,1),lwd=c(2,2,2),
col=c(col="yellowgreen", col="blue", col="pink",cex= 1))
box()
#plot histograms for DS1, DS2 and DS3 on one same graph
hist(DS1, prob=TRUE, main="Monoculture Stressed", xlab="Sizes (um)", ylab="Frequency", col="yellowgreen",xlim= c(5,25), ylim=c(0,0.5), breaks=10)
hist(DS2, prob=TRUE, col=rgb(0,0,1,0.5),add=T)
hist(DS3, prob=TRUE, col=rgb(0.8,0,1,0.5),add=T)
#add a legend to the histogram
legend("topright", c("DS1", "DS2", "DS3"), text.width=c(1,1,1),lwd=c(2,2,2),
col=c(col="yellowgreen", col="blue", col="pink",cex= 1))
box()
# put both overlapping histograms onto one page
combined <- par(mfrow=c(1, 2))
plot(hist(DSA),main="Controls")
plot(hist(DS1),main="Monoculture stressed")
par(combined)
Basically, I get two separate overlapping histograms, but cannot put them on the same page.
EDIT: I evidently didn't read your question thoroughly. I see you figured out the add =T.
I assume what you are looking for then is the comment I made first:
par(mfrow = c(a,b)) where a and b are the number of rows and columns you want the graphics objects to be printed. I used c(2,2) for this pic.
I made a comment, but sounds like you may be talking about the add=T option.
a=rnorm(100, 2, 1)
b=rnorm(100, 4, 1)
hist(a, xlim=c(0,10), col="yellow")
hist(b, add=T, col="purple" )
you can play around with transparency options on colors to see both overlap. Such as rgb(1,0,0,1/4) as the color.
With transparency colors:
a=rnorm(100, 2, 1)
b=rnorm(100, 4, 1)
hist(a, xlim=c(0,10), col=rgb(1,1,0,1/4))
hist(b, add=T, col=rgb(1,0,0,1/4) )
I use following code to make a plot with the first set of my values (here it is the number of crimes).
This is what I do have now:
Here is the code I use to plot it:
library(lattice)
library(latticeExtra)
library(grid)
library(reshape)
library(RCurl)
dd <- read.csv(text = getURL("https://gist.githubusercontent.com/GeekOnAcid/da022affd36310c96cd4/raw/9c2ac2b033979fcf14a8d9b2e3e390a4bcc6f0e3/us_nr_of_crimes_1960_2014.csv"))
d <- melt(dd, id="Year")
names(d)[1] <- "time"
pdf("sparklines_lattice_multiple_ten.pdf", height=10, width=8)
xyplot(value~time | variable, d, xlab="", ylab="", strip=F, lwd=0.7, col=1, type="l",
layout=c(1,length(unique(d$variable))), between = list(y = 1),
scales=list(y=list(at=NULL, relation="free"), x=list(fontfamily="serif")),
par.settings = list(axis.line = list(col = "transparent"),
layout.widths=list(right.padding=20, left.padding=-5)),
panel = function(x, y, ...) {
panel.xyplot(x, y, ...)
pushViewport(viewport(xscale=current.viewport()$xscale-5,
yscale=current.viewport()$yscale, clip="off"))
panel.text(x=tail(x,n=1), y=tail(y,n=1), labels=levels(d$variable)[panel.number()],
fontfamily="serif", pos=4)
popViewport()
panel.text(x=x[which.max(y)], y=max(y), labels=round(max(y),0), cex=0.8,
fontfamily="serif",adj=c(0.5,2.5))
panel.text(x=x[which.min(y)], y=min(y), labels=round(min(y),0), cex=0.8,
fontfamily="serif",adj=c(0.5,-1.5))
panel.text(x=tail(x,n=1), y=tail(y,n=1), labels=round(tail(y,n=1),0), cex=0.8,
fontfamily="serif", pos=4)
panel.points(x[which.max(y)], max(y), pch=16, cex=1)
panel.points(x[which.min(y)], min(y), pch=16, cex=1, col="red")})
dev.off()
What I need: to add an extra dimension (i.e., extra line) to each sparkline with the number of suspects per each type of crime (e.g., Vehicle.Theft, Larceny.Theft, etc.). In other words, I also want to add extra line to each sparkline to compare the dynamics of (1) the number of crimes and (2) the number of suspect for each type of crime.
To be clear: I want to set up two lines for each sparkline.
Just before you answer my question please be aware of:
par(new=TRUE) doesn't work: Warning message: In par(new = TRUE) : calling par(new=TRUE) with no plot. It just creates two separated pages in a PDF file with two different sparklines. I need the opposite result: two sparklines on the same PDF page.
Thank you very much for your efforts to help me (if you do so).
I'm plotting multiple data series.
colos=c('red','green','purple','pink','brown')
par(new=F)
for (i in 1:5)
{
plot(dat[[i+1]],col=colos[i],cex=marksize,xlab='Reading #',ylab = 'Current')
par(new=T)
}
My plot looks like this:
Is there a way I can overwrite the plot axis with each iteration, but not overwrite the plotted points?
You may want to use the lines or points function(s) instead. Here's an example of how I usually go about this problem. This way you only overlay points on top of the existing plot, instead of plotting one plot on top of another.
Plot the first one with your original plot call, then use lapply to overlay the other columns' points on top of that.
set.seed(1)
dat <- data.frame(replicate(5, sample(10)))
colos <- c('red','green','purple','pink','brown')
plot(dat[[1]], col = colos[1], xlab = 'Reading #',
ylab = 'Current', ylim = range(as.matrix(dat)))
invisible(lapply(2:ncol(dat), function(i) points(dat[[i]], col = colos[i])))
Turn off the axes using xaxt and yaxt
E.g.:
plot(1:10)
par(new=TRUE)
plot(1:10, rnorm(10), xaxt="n", yaxt="n", xlab="", ylab="", type="l")
axis(side=4)
I would like to see the number of instances for each bin show up on the graph as well
set.seed(1)
x<-rnorm(1:100)
hist(x)
Try this
set.seed(1)
x<-rnorm(1:100)
y <- hist(x, plot=FALSE)
plot(y, ylim=c(0, max(y$counts)+5))
text(y$mids, y$counts+3, y$counts, cex=0.75)
which gives:
Another much simpler solution is just to use labels=TRUE in the hist(...) method itself. It will include number of occurrences/counts on the top of each bins in the histogram plot.
However, I would recommend to always include xlim and ylim for the histogram plots.
Code:
set.seed(1)
x <- rnorm(1:100)
hist(x, xlim = c(-3,3), ylim = c(1,30), labels = TRUE)
that automatically happen. it's called "frequency" on the left