Grouped density plot with vertical line [duplicate] - r

This question already has answers here:
How to overlay density plots in R?
(8 answers)
Closed 4 years ago.
I have the following data set
set.seed(1)
startdate <- as.Date('2000-01-01')
enddate <- as.Date('2000-01-10')
Data <- data.frame(id = rep((1:1000),10),
group = rep(c("0","1"), 25),
IV = sample(1:100),
DV = sample(c("1", "0"), 10, replace = TRUE),
date = as.Date(
sample(as.numeric(start_date):
as.numeric(end_date), 1000,
replace = T), origin = '1970-01-01'))
I want to get two density plots for multiple groups (here, group = 1 and group = 0) and a vertical line on a defined point.
How do I do this?

To get the density plots and the line, do (description see in code comments)
Data$date_f <- as.factor(Data$date) # date as factor
Data$date_i <- as.integer(Data$date_f) # date as int
Data$date <- Data$date_i[!is.na(Data$date_i)] # excl missing
# date by group
date_1 <- Data$date_i[Data$group == "1"] # date group 1
date_2 <- Data$date_i[Data$group == "0"] # date group 2
# exclude missing
date_1 <- date_1[!is.na(date_1)]
date_2 <- date_2[!is.na(date_2)]
#View(date_i)
#plot
plot(density(date_1), xaxt='n', xlab = 'Date', lwd = 2.5, ylab = 'Density', main = 'Density and Line', las=1, col = "black", lty = 1) # line and labels
lines(density(date_2), col = 'blue', lwd = 2.5, lty = 1) # other line, repeat for each group
abline(v= 8, col='black', lwd = 1.5, lty = 1) # vertical line
tx=seq(min(date_1), max(date_1), by = 2) #labels
lb=levels(Data$date_f)[tx] #insert labels
axis(side = 1,at=tx,labels=lb, las=0.2) #insert axis

Related

Label the outlier in plot()

I want to plot data which has 1 outlier. I want such a plot where observation number of outlier is labeled. For this I have data:
res
x x x x x
-0.39123009 -0.02907481 0.01003539 0.65495527 -93.81415653
I am trying:
plot(res, type = "o")
cv <- -50
abline(h = -50, lty = 2)
text(res, labels = ifelse(res > cv, names(res), ""), cex = 1, col = 4) # add labels
Scatterplot appears but there no outlier labelling. How can I add labels according to observation number?
Is it because of repetition of "x"?
It looks like an issue due to the data structure. I took your data and transformed to multiple rows instead of one row. Here your data transformed and the code for plot:
#Data
df <- structure(list(V1 = c(-0.39123009, -0.02907481, 0.01003539, 0.65495527,
-93.81415653)), class = "data.frame", row.names = c("x", "x.1",
"x.2", "x.3", "x.4"))
Code:
#Plot
plot(df$V1,type="o")
cv <- -50
abline(h = -50, lty = 2)
text(df$V1,labels=ifelse(df$V1>cv,rownames(df),""),cex=1,col=4)
Output:

combining 2 dotplots with different y axis leads to overlap on x axis

I am trying to combine 2 dotplots using lattice and latticeExtra packages but am finding that the data groups on the x axis overlap in the combined plot. Here is a reproducible example:
First I create 2 reproducible data sets and melt them so that they are long instead of wide:
require(lattice)
df1 <- data.frame(Treatment = rep(c("B", "C"), each = 6),
LocB = sample(1:100, 12),
LocC = sample(1:100, 12))
dftwo <- data.frame(Treatment = rep(c("A"), each = 6),
LocA = sample(1:100, 6))
dat.reprod1 <- melt(df1, id.vars = 'Treatment')
dat.reprod2 <- melt(dftwo, id.vars = 'Treatment')
And then I create a dotplot for each dataset:
dotreprod1 <- dotplot(value ~ Treatment, data = dat.reprod1,
par.strip.text = list(cex = 3),
cex = 2)
dotreprod2 <- dotplot(value ~ Treatment, data = dat.reprod2,
par.strip.text = list(cex = 3), col = "orange",
cex = 2)
And then I combine them, adding a new Y axis for dotreprod2:
require(latticeExtra)
doubleYScale(dotreprod1, dotreprod2, add.ylab2 = TRUE, use.style = F)
Unfortunately there is no room on the x axis of the combined plot for "A" and so the orange points overlap with the blue ones. Is it possible to create space on the X axis so that "A","B", and "C" are next to one another and the points do not overlap?
In both individual plots, specify the x variable as a factor with levels of the combined data, and set drop.unused.levels = FALSE
dotreprod1 <- dotplot(value ~ factor(Treatment, levels = LETTERS[1:3]),
data = dat.reprod1,
drop.unused.levels = FALSE)
dotreprod2 <- dotplot(value ~ factor(Treatment, levels = LETTERS[1:3]),
data = dat.reprod2,
col = "orange",
drop.unused.levels = FALSE)
doubleYScale(dotreprod1, dotreprod2, add.ylab2 = TRUE, use.style = FALSE)

quantmod display weekly indicator on daily chart

I am trying to display the daily and weekly RSI on the daily charts but have no success
getSymbols('JNUG')
chartSeries(JNUG, subset = 'last 4 months', dn = 'red', TA = 'addRSI(n=14); addLines(h = c(30, 70), on = 2, col = "red"); addTA(RSI(Cl(to.weekly(JNUG)), n =2))')
I also tried the following:
chartSeries(JNUG, subset = 'last 4 months', dn = 'red', TA = 'addRSI(n=14); addLines(h = c(30, 70), on = 2, col = "red")')
addTA(RSI(Cl(to.weekly(JNUG)), n =14))
The weekly RSI does not appear on the plot. Can someone help.
When you plot the weekly points, NA values are filled in between on the merge to the original daily data. The weekly RSI plot wants to join together points and NAs as a line plot, but no line is drawn where there are NAs, no line gets drawn in the end.
Try this:
chartSeries(JNUG, subset = 'last 4 months', dn = 'red', TA = 'addRSI(n=14); addLines(h = c(30, 70), on = 2, col = "red"); addTA(RSI(Cl(to.weekly(JNUG)), n =2), type = "p")')
Or, if you want lines instead, try this (see lowest add_TA call):
library(quantmod)
chart_Series(JNUG, subset = '2016-11/')
add_RSI(n= 14)
v <- to.weekly(JNUG)
add_TA(merge(xts(order.by = index(JNUG)), RSI(Cl(v))), type = 'p')
#na.spline interpolates between points smoothly. Could also use fill = na.locf (produces a step function), etc ...
add_TA(merge(xts(order.by = index(JNUG)), RSI(Cl(v)), fill = na.spline), type = 'l')
which produces this:
EDIT: One way to add horizontal lines on add_TA subplots:
chart_Series(JNUG, subset = '2016-01/')
add_RSI(n= 14)
v <- to.weekly(JNUG)
# Note: na.locf does not introduce 'lookforward bias' in visualised technical indicator plots, while na.spline does.
add_TA(merge(xts(order.by = index(JNUG)), RSI(Cl(v)), fill = na.locf), type = 'l')
low_rsi <- 30
hi_rsi <- 70
xrsi_low <- xts(order.by = index(JNUG), x = rep(low_rsi, NROW(JNUG)))
xrsi_hi <- xts(order.by = index(JNUG), x = rep(hi_rsi, NROW(JNUG)))
add_TA(xrsi_low, col = "purple", type = 'l', on = 3, lty = 2)
add_TA(xrsi_hi, col = "purple", type = 'l', on = 3, lty = 4)

Draw a plot for a matrix in r

I have a matrix with three variables Row = Time, column = Date and the third variable Money which its value is an intersection of rows and columns. e.g. For Time = 5 and Date = 10, Money is 12 and for Time = 6 and Date = 15, Money is 15. I would like to draw the value of Money for the intersection of x_axis = Time and Y_axis = Date.
How to place Money in below?
plot.new()
matplot(Time,Date, type = "p", lty = 1:5, lwd = 1, lend = par("lend"),col = 1,
pch = 17 , xlab = "Time", ylab = "Date", xlim = range(0,40), ylim = range (0,120))
I think you could use geom_raster if you convert your data to a data.frame first:
ggplot(data, aes(Time, Date)) +
geom_raster(aes(fill = Money))
See more on this here: http://docs.ggplot2.org/current/geom_tile.html
edit:
see with random data here:
time <- c(1:100)
date <- c(1:100)
data <- expand.grid(TIME = time, DATE = date)
data$MONEY <- runif(1:10000, 0, 10)
ggplot(data, aes(TIME, DATE)) +
geom_raster(aes(fill = MONEY), interpolate = F)

Not sure how to plot this, a scatter plot with categories

I have the following dataset
SIZE ALG1 ALG2 ALG3
A 2 3 5
A 3 2 1
A 1 2 2
B 10 10 11
B 12 12 12
I'd like a plot having as a horizontal axis the SIZE column and as Y values the series ALG1 ALG2 ALG3.
E.g.
How can I get it wit Excel (actually OOCALC) or R?
In general I get the SIZE values repeated on the x-axis, which appears then much longer.
TIA
Suppose your data frame is called df1.
You have to convert it in a long format using reshape2::melt
melt(df1) -> dfmelt
library(ggplot2)
ggplot(data=dfmelt, aes(x=SIZE, y=value)) + geom_jitter(aes(color=variable))
or, more simply and quickly:
qplot(data=dfmelt, x=SIZE, y=value, geom="jitter", color=variable)
If you just want the points without "jittering" them:
qplot(data=dfmelt, x=SIZE, y=value, geom="point", color=variable)
In R you could do it as follows:
df <- data.frame(
size = c(rep('A', 3), rep('B', 2)),
alg1 = c(2,3,1,10,12),
alg2 = c(3,2,2,10,12),
alg3 = c(5,1,2,11,12)
)
plot(alg1 ~ jitter(as.numeric(size)), data = df,
xlim = c(0.5, 2.5), ylim = c(0, 14), col = 'red',
axes = FALSE, xlab = 'Size', ylab = 'Alg')
points(alg2 ~ jitter(as.numeric(size)), data = df, col = 'blue')
points(alg3 ~ jitter(as.numeric(size)), data = df, col = 'black')
axis(side = 2)
axis(side = 1, at = c(1, 2), labels = c('A', 'B'))
legend(x = 2, y = 4, legend = c('alg1', 'alg2', 'alg3'),
col = c('red', 'blue', 'black'), lty = 1)

Resources