I need to plot two sets of data on one graph, and then use locator() to draw a vertical line at a given date. I have the code below and it works until after the plot function. The lines function is not adding the second dataset to my graph.
Can someone please help me understand what I'm doing incorrectly?
Thank you in advance
LMT <- read.csv("LMT.csv",header = T)
JNJ <- read.csv("JNJ.csv",header = T)
LMT$Date <- as.Date(LMT$Date)
JNJ$Date <- as.Date(JNJ$Date)
plot(y=LMT$Adj.Close, x=LMT$Date, ylim = c(0,500), type = "l")
lines(JNJ$Adj.Close,type = "l")
Related
I was currently using Quantmod to visualize technical analysis of equity data. When I came across the addMACD() functions for adding MACD graph and it works fine, except when I need to visualize the histogram only instead of the line graph.
addMACD(fast = display$macdFast, slow = display$macdSlow, signal = display$macdSignal, histogram = TRUE)
After reading through the documentation, I cannot figure out a way to remove the line plot of MACD graph. Is is possible to remove line plot while retaining histogram plot for MACD?
I always seem have problems with creating complicated newTA objects with quantmod, but here is an SO example. I find it easier to use rtsplot. That is a plotting package for xts objects but based on the base plots.
Since the macd histogram is nothing but the difference between the macd and the macd signal you can create your own histogram values:
library(quantmod)
goog <- getSymbols("GOOGL", from = "2019-01-01", auto.assign = F)
goog_macd <- MACD(goog$GOOGL.Close)
goog_macd_his <- goog_macd$macd - goog_macd$signal
library(rtsplot)
layout(c(1,1,1,2))
rtsplot(goog, type = "candle")
rtsplot(goog_macd_his , type = "h", lwd = 2)
I'm struggeling with the histogram function in my exploratory analysis. I would like to run a couple of variables in my dataset through a histogram function and for each add the title and a line at the arithmetic mean. This is how far I've got (but the main title is still missing):
histo.abline <-function(x){
hist(x)
abline(v = mean(x, na.rm = TRUE), col = "blue", lwd = 4)}
sapply(dataset[c(7:10)], histo.abline)
I tried to add a main argument in the histogram function but it just doesn't pick the right variable name of my dataset vector. When I put main=x there, it says returns NULL for each variable. Colnames, names and other functions didn't work either. Could you help me?
you can try to do it with ggplot:
library(ggplot)
histo.abline <-function(dataset,colnum){
p<-ggplot(dataset,aes(dataset[,colnum]))+geom_histogram(bins=5,fill=I("blue"),col=I("red"), alpha=I(.2))+
geom_vline(xintercept = mean(dataset[,colnum], na.rm = TRUE))+xlab(as.character(names(dataset)[colnum]))
return(p)
}
since you have not provided data lets work with mtcars and create a list of histograms
dataset=mtcars
listOfHistograms<-lapply(3:7,function(x) histo.abline(dataset,x))
your list has 5 histograms that you can plot for instance the first by:
print(listOfHistograms[[1]])
More histogram options for ggplot here: https://www.r-bloggers.com/how-to-make-a-histogram-with-ggplot2/
hope this helps
EDIT: Multiple Plot in one graph
One way to do it is through cowplot library:
library(cowplot)
plot_grid(plotlist=listOfHistograms[1:4])
I wanted to make a box plot. I have more than 1000 rows but when I am plotting them, it shows only a few entries.
Dataset:
https://www.dropbox.com/s/tgaqfgm2gkl7i3r/maintenance_data_updated.csv
#Start of Box plot Temperature
training_data <- read.csv("C:/Users/akhan/Documents/maintenance_data_updated_2.csv", stringsAsFactors = TRUE)
library(dplyr)
dt_temperature <- select(training_data, Runtime, Defect, Machine, Temperature, Plant)
dt_temperature$Machine_Plant = paste(dt_temperature$Machine,dt_temperature$Plant,sep = "_")
attach(dt_temperature)
class(Temperature)
class(Defect)
class(Runtime)
class(Machine)
?boxplot
boxplot(Temperature ~ Machine_Plant)
Current output: https://www.dropbox.com/s/7nv5n80en1vpkyt/Rplot01.png
Can anyone please give a hint what is the solution ?
What do you mean saying 'it shows only a few entries'? If your problem is about having only 4 boxplots annotated on X-axis, solution could be like this:
boxplot(Temperature ~ Machine_Plant, las=3)
Type
?par
for more information about las parameter.
Hopefully a straightforward question but I made a simple figure in R using filled.contour(). It looks fine, and what it should like given the data. However, I want to add a reference line along a contour for 0 (level = 0), and the plotted line doesn't match the colors on the filled.contour figure. The line is close, but not matching with the figure (and eventually crossing over another contour from the filled.contour plot). Any ideas why this is happening?
aa <- c(0.05843150, 0.11300040, 0.15280030, 0.183524400, 0.20772430, 0.228121000)
bb <- c(0.01561055, 0.06520635, 0.10196237, 0.130127650, 0.15314544, 0.172292410)
cc <- c(-0.02166599, 0.02306650, 0.05619421, 0.082193680, 0.10334837, 0.121156780)
dd <- c(-0.05356592, -0.01432910, 0.01546647, 0.039156660, 0.05858709, 0.074953650)
ee <- c(-0.08071987, -0.04654243, -0.02011676, 0.000977798, 0.01855881, 0.033651089)
ff <- c(-0.10343798, -0.07416114, -0.05111547, -0.032481132, -0.01683215, -0.003636035)
gg <- c(-0.12237798, -0.09753544, -0.07785126, -0.061607548, -0.04788856, -0.036169540)
hh <-rbind(aa,bb,cc,dd,ee,ff,gg)
z <- as.matrix(hh)
y <- seq(0.5,1.75,0.25)
x <- seq(1,2.5,0.25)
filled.contour(x,y,z,
key.title = title(main=expression("log"(lambda))),
color.palette = topo.colors) #This works
contour(x,y,z, level=0,add=T,lwd=3) #This line doesn't match plot
This is completely answered in the ?filled.contour help page. In the Notes section it states
The output produced by filled.contour is actually a combination of two plots; one is the filled contour and one is the legend. Two separate coordinate systems are set up for these two plots, but they are only used internally – once the function has returned these coordinate systems are lost. If you want to annotate the main contour plot, for example to add points, you can specify graphics commands in the plot.axes argument. See the examples.
And the examples given in that help page show how to annotate on top of the main plot. In this particular case, the correct way would be
filled.contour(x,y,z,
key.title = title(main=expression("log"(lambda))),
color.palette = topo.colors,
plot.axes = {
axis(1)
axis(2)
contour(x,y,z, level=0,add=T,lwd=3)
}
)
which produces
I would like to plot vectors from a capscale ordination using VEGAN.
I am familiar with the display ="bp" command, but this adds labels that are obscured by site points. Is there an easy means of removing these? I am happy to add them in later i.e. once exported and within word for publication.
My code thus far is as follows:
plot(mod, scaling = 3, type="n")
with(data, points(mod, display="sites", cex=Pointsize,
pch=ifelse(Cat=="Reference",21,19)) ,bg=Cat,)
with(data,text(mod,display="bp"))
Help will be appreciated
Use the points() method instead of the text() method:
points(mod, display = "bp")
(There also should be no need for the with(data) in that last line of code you show.)
Here is a reproducible example:
require(vegan)
data(varespec)
data(varechem)
ord <- cca(varespec ~ ., data = varechem)
plot(ord, type = "n", display = "sites")
points(ord, display = "sites")
points(ord, display = "bp")