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:
Related
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)
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
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)
I have three sets of data that I want to plot using Circlize. Set's "a" and "b" contain positive and negative values, set "c" contains only positive values. I want to plot using a consistent y axis for "a" and "b" and different one for "c". Unfortunately, I can't find a way of doing this.
The code i'm using is below, I seem to only be able to have unique y axis for each or a fixed y axis for all (if you uncomment "ylim..." in the code).
I've tried a few things, trying to loop through using the circos.lines() function instead but this didn't solve it.
circos.info() shows separate y axis limits so i think it should be possible.
Thanks in advance.
library("circlize")
library("reshape")
#--- Data ---#
a <- sort(rnorm(100,0,10))
b <- sort(rnorm(100,0,5))
c <- abs(rnorm(100,0,200))
data <- cbind("a" = a, "b" = b, "c" = c)
data_melt <- melt(data)
#--- Plotting ---#
circos.initialize( factors = data_melt$X2,
x = data_melt$X1,
sector.width = 1
)
circos.trackPlotRegion( factors = data_melt$X2,
y = data_melt$value,
# ylim = range(data_melt$value),
force.ylim = FALSE,
panel.fun = function(x, y) { circos.axis( ) }
)
circos.trackLines( data_melt$X2,
data_melt$X1,
data_melt$value,
type = "h",
col = "grey",
lwd = 3,
baseline = 0)`
circos.info(sector.index = NULL, track.index = 1)
ylim can be matrix in which each row corresponds to the y-range in each sector.
set.seed(123)
a <- sort(rnorm(100,0,10))
b <- sort(rnorm(100,0,1))
c <- abs(rnorm(100,0,200))
data <- cbind("a" = a, "b" = b, "c" = c)
data_melt <- melt(data)
#--- Plotting ---#
r_ab = range(data_melt[data_melt$X2 != "c", "value"])
r_c = range(data_melt[data_melt$X2 == "c", "value"])
circos.par(gap.degree = 5)
circos.initialize( factors = data_melt$X2,
x = data_melt$X1,
sector.width = 1
)
ylim = rbind(r_ab, r_ab, r_c)
circos.trackPlotRegion( factors = data_melt$X2,
x = data_melt$X1,
y = data_melt$value,
ylim = ylim,
force.ylim = F,
panel.fun = function(x, y) {
circos.lines(x, y, type = "h", col = "grey", lwd =3, baseline = 0)
circos.axis(labels.cex = 0.6)
circos.yaxis(labels.cex = 0.6)
}
)
circos.clear()
I also moved the code from circos.trackLines() to panel.fun() (because I think panel.fun() is more flexible to add multiple layers of graphics).
Also I added circos.yaxis() because y-axis has different ranges in sectors, it is important to explicitly show the y-ranges.
I wanna make a scatter plot with connecting lines for different groups and different individuals. I make panels conditioned by my group variable and groups conditioned by my individual variables. Now, I would like to add legend inside each panels(see the code below). In the plots, I would like to have legends of individuals for GRP==1 in the first panel, GRP==2 in the second panel, so on so forth. All the legends are located in the upper left corner of the panel they belong to. How shall I code?
library(lattice)
mydata <- data.frame(ID = rep(1: 20, each = 10),
GRP = rep(1: 4, each = 50),
x = rep(0: 9, 20))
mydata$y <- 1.2 * mydata$GRP * mydata$x +
rnorm(nrow(mydata), sd = mydata$GRP)
xyplot(y~ x | factor(GRP), data = mydata,
groups = ID,
type = "b",
as.table = T,
layout = c(2, 2),
panel = panel.superpose,
panel.groups = function (x, y, ...) {
panel.xyplot(x, y, ...)
}
)
Try something like this. Note that the subset command comes in the data statement in xyplot. This is on purpose. If you call subset as an xyplot argument, then the plots would have shown all 20 labels in each plot.
library(lattice)
mydata <- data.frame(ID = rep(1:20, each = 10), GRP = rep(1:4, each = 50), x = rep(0:9, 20))
mydata$y <- 1.2 * mydata$GRP * mydata$x + rnorm(nrow(mydata), sd = mydata$GRP)
i=1; j=1
for(grp in 1:4) {
a <- xyplot(y~x|factor(GRP), data=subset(mydata, GRP==grp),
groups = factor(ID),
type = "b",
auto.key=list(columns=4,space="inside")
)
print(a, split=c(i,j,2,2), more=T)
i=i+1; if(i>2){i=1;j=j+1} # basically, tell the plots which quadrant to go in
}