I'm trying to make a plot in R. My x-axis is a week number converted to factor and my y-axis is an amount.
When I run plot() instead of dots I get horizontal lines.
Why does this happen?
Here is a sample dataset:
df <- data.frame(fin_week=as.factor(seq(1,20, by =1)), amount=(rnorm(20)^2)*100)
plot(df)
Looking at the documentation, it's because the first column is a factor. When R tries to find the right plot() to run, it looks into plot.dataframe, where it plots on the type of 1st column i.e a factor. Hence it plots using plot.factor(), which gives a line by default, which is used for box plots.
try using plot.default(df) to plot and you should get it the scatter plot
Related
I want to add multiple vertical lines to a time series plot.
Normally you would specify abline(v=x-intercept) but my x-axis is in the form 01/08/2000 – 31/07/2001. How would I adapt the abline code to add vertical lines for example 13/10/2000 and 30/05/2001?
Firstly, I have tried the following zoo plot but the x-axis is not giving me the preferred date format;
dat<-read.table("Aug2000-July2001.txt", sep="\t", header=T)
z <- with(dat, zoo(cbind(NEE,SWC)))
options("na.actions"=na.omit)
plot.zoo(z, ylab=c("NEE(umolm-2s-1)", "SWC(%)"),
col=c("black","blue"), xlab=c("Date"), lwd=2)
Secondly, I have also tried e.g.
abline(v=as.Date("13/10/2000"))
but it is not working. How do I add multiple vertical lines and how do I add my date format on the x-axis on a zoo plot?
I have a table with some of the columns as follows:
I have two problems that I am having trouble to solve for. Firstly when I do a multiple column matrix plot using lattice xyplot, I find that all the points are connected. How can I get separate disconnected lines?
x<-cbind(rnorm(10),rnorm(10))
xyplot(x~1:nrow(x),type="l")
Secondly, I am having trouble figuring out how to make one line thicker than the other. For example, given that I want column 1, then column 1's line will be thicker than that of column 2.
The lattice plotting paradigm,like that of ggplot2 that followed it, expects data to be in long format in dataframes:
dfrm <- data.frame( y=c(rnorm(10),rnorm(10)),
x=1:10,
grp=rep(c("a","b"),each=10))
xyplot(y~x, group=grp, type="l", data=dfrm, col=c("red","blue"))
This might not be the most elegant solution but it gets the job done:
x<-cbind(rnorm(10),rnorm(10))
plot1<-xyplot(x[,1]~1:nrow(x),type="l",col="red",lwd=3)
plot2<-xyplot(x[,2]~1:nrow(x),type="l")
library(latticeExtra)
plot1+plot2
I assumed that you wanted V1 and V2 plotted against the number of observations.
Otherwise you indeed only have one line.
You can adjust the axis and labels according to taste.
Let's say I have the following dataset
bodysize=rnorm(20,30,2)
bodysize=sort(bodysize)
survive=c(0,0,0,0,0,1,0,1,0,0,1,1,0,1,1,1,0,1,1,1)
dat=as.data.frame(cbind(bodysize,survive))
I'm aware that the glm plot function has several nice plots to show you the fit,
but I'd nevertheless like to create an initial plot with:
1)raw data points
2)the loigistic curve and both
3)Predicted points
4)and aggregate points for a number of predictor levels
library(Hmisc)
plot(bodysize,survive,xlab="Body size",ylab="Probability of survival")
g=glm(survive~bodysize,family=binomial,dat)
curve(predict(g,data.frame(bodysize=x),type="resp"),add=TRUE)
points(bodysize,fitted(g),pch=20)
All fine up to here.
Now I want to plot the real data survival rates for a given levels of x1
dat$bd<-cut2(dat$bodysize,g=5,levels.mean=T)
AggBd<-aggregate(dat$survive,by=list(dat$bd),data=dat,FUN=mean)
plot(AggBd,add=TRUE)
#Doesn't work
I've tried to match AggBd to the dataset used for the model and all sort of other things but I simply can't plot the two together. Is there a way around this?
I basically want to overimpose the last plot along the same axes.
Besides this specific task I often wonder how to overimpose different plots that plot different variables but have similar scale/range on two-dimensional plots. I would really appreciate your help.
The first column of AggBd is a factor, you need to convert the levels to numeric before you can add the points to the plot.
AggBd$size <- as.numeric (levels (AggBd$Group.1))[AggBd$Group.1]
to add the points to the exisiting plot, use points
points (AggBd$size, AggBd$x, pch = 3)
You are best specifying your y-axis. Also maybe using par(new=TRUE)
plot(bodysize,survive,xlab="Body size",ylab="Probability of survival")
g=glm(survive~bodysize,family=binomial,dat)
curve(predict(g,data.frame(bodysize=x),type="resp"),add=TRUE)
points(bodysize,fitted(g),pch=20)
#then
par(new=TRUE)
#
plot(AggBd$Group.1,AggBd$x,pch=30)
obviously remove or change the axis ticks to prevent overlap e.g.
plot(AggBd$Group.1,AggBd$x,pch=30,xaxt="n",yaxt="n",xlab="",ylab="")
giving:
My problem is very simple.
I have to plot a data series in R, using bars. Data are contained in a vector vet.
I've used barplot, that plots my data from the first to the last:
barplot(vet), and everything was fine.
Now, on the contrary, I would like to plot not all my data, but just a part of them: from 10% to the end.
How could I do this with barplot()?
How could I do this with plot()?
Thanx
You need to subset your data before plotting:
##Work out the 10% quantile and subset
v = vet[vet > quantile(vet, 0.1)]
It is not clear exactly what you want to do.
If you want to plot only a subset of the bars (but the whole bars) then you could just subset the data before passing it to barplot.
If you want to plot all the bars, but only that part beyond 10% (not include 0) then you can do this by setting the ylim argument. But it is very discouraged to do a barplot that does not include 0. You may be better off using a dotplot instead of a barplot if 0 is not meaningful.
If you want the regular plot, but want to exclude plotting outside of a given window within the plot then the clip function may be what you want.
The gap.barplot function from the plotrix package may also be what you want.
I have a barplot() with the following code:
mp<-barplot(data.30.s$mean.rev,names.arg=data.30.s$dma.name,
main="Mean", ylab="Mean",las=2, cex.names=.5)
abline(h=mean(data.30.s$mean.rev))
lines(data.30.c$mean.rev,col=34)
data.30.s and data30.c have the same x-values and only differ in Y-values. I want to plot the line over the barplot to give a comparison. However, the x categories aren't matching up.
In this situation, I used identical datasets to see if the lines would work, but as you can see the plotted line x values don't match up. Any idea how to fix this or do this ggplot would be greatly appreciated. My ggplot code was:
ggplot(data.30.s,aes(dma.name,mean.rev))+geom_bar(stat="identity")
but the X axis names didn't work and were a garbled mess.
Do this instead:
lines(mp, data.30.c$mean.rev,col=34)
The reason this should work is that barplot returns the x-values at which the midpoints of bars have been drawn. Sio instead of using the index of the lines argument as you had been trying (implicitly) you will now give the right x-values to line up with the bars.