Maybe you can help me. I need to plot a time series, let's call it, ts for that, I use the following code:
plot(ts,col="royalblue", ylab="time series plot", main = "Plot", grid.col=NA)
But I also need to plot in the same plot, a vertical line at the date 18-03-2020 (March 18, 2020). I tried with the following line of code but I don't get any vertical line:
geom_vline(xintercept = 03-18-2020, color="red", linetype="doted", size=2.5)
And also with this one:
abline(a=NULL,b=NULL,h=NULL,v="18-03-2020", col="red")
And this one:
date1 <- as.Date("2020-03-18") + 0:99
abline(v=as.Date(date1))
But with none of them I get the vertical line I need.
What am I doing wrong? could you help me?
You can do it with abline. Set v and position of x axis where you want the horizontal line. In this example v=0.4
plot(ts,col="royalblue", ylab="time series plot", main = "Plot", grid.col=NA)
abline(v=0.4, col="blue")
Related
I am using the following code if it helpful to plot some data using hist() in R.
hist(info$data, breaks=300, main="Some Data", xlab="data", xlim=c(0, 10000))
And this is the image I get the following image:
Is there a way to get rid of the line at y=0 that is underlining all of the bars? Or is there some better way to extend it to the y axis so it looks more like an axis rather than just some line beneath my bars?
I would use ggplot2
library(ggplot2)
ggplot(info, aes(x=data)) + geom_histogram(binwidth) +
ggtitle("Some Data") + xlab("data") + xlim(0,10000)
I'm sorry for asking this question, but I already googled and searched here but I found nothing useful (that means lots of different functions for drawing the plot, but no one with my problem).
I have a vector containing the data I have to plot (named "rmse"), and a vector containing the names of the columns on the x-axis (named "nomi"). I simply want to plot the data with the labels on the x-axis rotated of 90°, due to space problems.
I found this useful site: http://harding.edu/fmccown/r/
Looking at it, I found how to rotate the labels on the axis, but, even though I have 12 columns, I have 6 columns with an overwritten label and 6 columns without label.
Here's my code:
library(lattice)
library(gstat)
nomi<-c("Quota","No Quota","Mare","No Mare","Slope","No Slope","Terreno","No Terreno","Facet","No Facet","Po","No Po")
rmse<-c(1.79,1.97,1.82,1.84,1.82,1.82,1.80,1.83,1.82,1.84,1.82,1.81)
g_range <- range(0, rmse)
plot(rmse, type='h',axes=F, ann=F)
axis(1, at=1:12, lab=F)
text(axTicks(1),par("usr")[3], srt=90, adj=1, labels=nomi, xpd=T, cex=0.8)
axis(2, las=1)
box()
And here's the plot:
Do you know what am I doing wrong? I know it's a simple questions, but I'm quite a beginner and sometimes I need help :)
Thank you for the attention!
I solved! It was enough to add "las=2" as argument of axis, thanks to joran to have suggested me that I can avoid "text" ;)
nomi<-c("Quota","No Quota","Mare","No Mare","Slope","No Slope","Terreno","No Terreno","Facet","No Facet","Po","No Po")
rmse<-c(1.79,1.97,1.82,1.84,1.82,1.82,1.80,1.83,1.82,1.84,1.82,1.81)
g_range <- range(0, rmse)
plot(rmse, type='h',axes=F, ann=F)
axis(1, at=1:12, lab=nomi, las=2)
axis(2, las=1)
box()
Another approach is to use by ggplot2 command to produce the chart
dt <- data.frame(
rownum = 1:length(nomi),
nomi=c("Quota","No Quota","Mare","No Mare","Slope","No Slope","Terreno","No Terreno","Facet","No Facet","Po","No Po"),
rmse=c(1.79,1.97,1.82,1.84,1.82,1.82,1.80,1.83,1.82,1.84,1.82,1.81)
)
library(ggplot2)
ggplot(dt) + aes(x =reorder(nomi,rownum), y = rmse) + geom_bar(stat = "identity")+
theme(axis.text.x = element_text(angle=90, face="bold", colour="black"))+
scale_x_discrete(name="" )
I am using the latticeExtra library "ecdfplot" to plot my error. I want to add gridlines.
The following does not seem to work:
ecdfplot(err)
grid(ny=10)
It gives the following (gridless) result:
I really would love to give a "graphical summary" where the quantiles are indicated by lines, and their intersections with the data are shown on the x-axis.
Can you tell me how to add gridlines?
How about adding vertical lines at a particular x-location?
Try the argument axis = axis.grid
require(latticeExtra)
data(singer, package = "lattice")
ecdfplot(~height, data = singer, add=TRUE, axis = axis.grid, par.settings = theEconomist.theme())
How to change the axis length? for ex:
s <- data.table(school=rep(1:3,5), wave=c(rep(1,7), rep(2,8)), v1=rpois(15,10))
plot(s$wave,s$v2)
I get a scatter plot where the data is at the edges of the plot (a lot of white space in the graph). changing the xaxp values doesn't help (tried xaxp=c(-1, +2,4)) but nothing happened) and when I try to define it a factor I get a box plot. I know I can "squeeze" it when i save to .png but is there any other way?
I tried to upload pictures to convey the problem but I don't have enough reputation.
edit-thanks for whoever uploaded it (although the axis are reversed - wave is the x and V2 is the y). the thing is that there is a lot of "free space" between the 1st and the 2nd wave. the position is perfect when i define the wave a factor (it's centered and each factor is half the axis length) but it keeps giving me a box plot!
You can add a lot of values to your plot function, like colour, title, and also the limits of the axsis
Your code:
s <- data.frame(school=rep(1:3,5), wave=c(rep(1,7), rep(2,8)), v1=rpois(15,10))
plot(s$wave,s$v2)
And now just add some more:
plot(
x = s$wave,
y = s$v2,
col = "red",
main = "This is my title",
xlab = "the label of the x-axis",
ylab = "the label of the y-axis",
xlim = c(-5, 5), # the limits of the x-axis,
ylim = c(-4, 10) # the limits of the y-axis
)
You can add much more like size and type of the points ...
just as jlhoward mentioned
i found a function in the "lattice" package that does exactly what i want - a boxplot without the box.
the function is called stripplot.
http://www.math.ucla.edu/~anderson/rw1001/library/base/html/stripplot.html
thank you all for the help
I'm trying to generate a histogram in R with a logarithmic scale for y. Currently I do:
hist(mydata$V3, breaks=c(0,1,2,3,4,5,25))
This gives me a histogram, but the density between 0 to 1 is so great (about a million values difference) that you can barely make out any of the other bars.
Then I've tried doing:
mydata_hist <- hist(mydata$V3, breaks=c(0,1,2,3,4,5,25), plot=FALSE)
plot(rpd_hist$counts, log="xy", pch=20, col="blue")
It gives me sorta what I want, but the bottom shows me the values 1-6 rather than 0, 1, 2, 3, 4, 5, 25. It's also showing the data as points rather than bars. barplot works but then I don't get any bottom axis.
A histogram is a poor-man's density estimate. Note that in your call to hist() using default arguments, you get frequencies not probabilities -- add ,prob=TRUE to the call if you want probabilities.
As for the log axis problem, don't use 'x' if you do not want the x-axis transformed:
plot(mydata_hist$count, log="y", type='h', lwd=10, lend=2)
gets you bars on a log-y scale -- the look-and-feel is still a little different but can probably be tweaked.
Lastly, you can also do hist(log(x), ...) to get a histogram of the log of your data.
Another option would be to use the package ggplot2.
ggplot(mydata, aes(x = V3)) + geom_histogram() + scale_x_log10()
It's not entirely clear from your question whether you want a logged x-axis or a logged y-axis. A logged y-axis is not a good idea when using bars because they are anchored at zero, which becomes negative infinity when logged. You can work around this problem by using a frequency polygon or density plot.
Dirk's answer is a great one. If you want an appearance like what hist produces, you can also try this:
buckets <- c(0,1,2,3,4,5,25)
mydata_hist <- hist(mydata$V3, breaks=buckets, plot=FALSE)
bp <- barplot(mydata_hist$count, log="y", col="white", names.arg=buckets)
text(bp, mydata_hist$counts, labels=mydata_hist$counts, pos=1)
The last line is optional, it adds value labels just under the top of each bar. This can be useful for log scale graphs, but can also be omitted.
I also pass main, xlab, and ylab parameters to provide a plot title, x-axis label, and y-axis label.
Run the hist() function without making a graph, log-transform the counts, and then draw the figure.
hist.data = hist(my.data, plot=F)
hist.data$counts = log(hist.data$counts, 2)
plot(hist.data)
It should look just like the regular histogram, but the y-axis will be log2 Frequency.
I've put together a function that behaves identically to hist in the default case, but accepts the log argument. It uses several tricks from other posters, but adds a few of its own. hist(x) and myhist(x) look identical.
The original problem would be solved with:
myhist(mydata$V3, breaks=c(0,1,2,3,4,5,25), log="xy")
The function:
myhist <- function(x, ..., breaks="Sturges",
main = paste("Histogram of", xname),
xlab = xname,
ylab = "Frequency") {
xname = paste(deparse(substitute(x), 500), collapse="\n")
h = hist(x, breaks=breaks, plot=FALSE)
plot(h$breaks, c(NA,h$counts), type='S', main=main,
xlab=xlab, ylab=ylab, axes=FALSE, ...)
axis(1)
axis(2)
lines(h$breaks, c(h$counts,NA), type='s')
lines(h$breaks, c(NA,h$counts), type='h')
lines(h$breaks, c(h$counts,NA), type='h')
lines(h$breaks, rep(0,length(h$breaks)), type='S')
invisible(h)
}
Exercise for the reader: Unfortunately, not everything that works with hist works with myhist as it stands. That should be fixable with a bit more effort, though.
Here's a pretty ggplot2 solution:
library(ggplot2)
library(scales) # makes pretty labels on the x-axis
breaks=c(0,1,2,3,4,5,25)
ggplot(mydata,aes(x = V3)) +
geom_histogram(breaks = log10(breaks)) +
scale_x_log10(
breaks = breaks,
labels = scales::trans_format("log10", scales::math_format(10^.x))
)
Note that to set the breaks in geom_histogram, they had to be transformed to work with scale_x_log10