How to add stripchart on a boxplot without outliers? - r

I made a boxplot without outliers.
boxplot(data, names = names(files), las = 2, outline=FALSE)
Now, I am trying to add stripchart on it:
stripchart(data, method = "jitter", add = T, col = "red")
Plot looks something like this:
I understand the reason why plot is like this: because I have not plotted outliers with boxplot. How to add stripchart properly (without outliers)?

I have solved my problem.
b <- boxplot(data, names = names(files), las = 2, outline=FALSE)
stripchart(data[!data %in% b$out], method = "jitter", add = T, col = "red",
vertical = T)

Related

How to superimpose two ts objects with two different axes with ts.plot in R?

library(tseries)
library(readxl)
data = read_excel(.......)
#Create tseries
equity = ts(data$EQUITY, start = c(2015,01,01), end = c(2020,01,01), frequency = 12)
cci = ts(data$CCI, start = c(2015,01,01), end = c(2020,01,01), frequency = 12)
#Plot the two series together
ts.plot(equity, cci, lty = c(1:2))
This code returns the following chart: R Chart.
Keeping in mind that I'd like to use the ts.plot function since I'm plotting other tseries objects and I'm not a fan of how ggplot2 handles them, is there a way to add a second axis to the chart and make it so that the two series appear superimposed instead of being far apart like in the linked image? The desired result would be like in this chart plotted with Python and matplotlib: Python Chart, possibly with a legend, too.
Any help would be greatly appreciated, thanks!
Use plot.ts rather than ts.plot like this. (See ?legend for other legend position keywords.)
z <- ts(cbind(1:10, 110:101))
col <- c("black", "blue")
plot(z[, 1], xlab = "", ylab = "", yaxt = "n", col = col[1])
axis(2, col.axis = col[1])
par(new = TRUE)
plot(z[, 2], xlab = "", ylab = "", yaxt = "n", col = col[2])
axis(4, col.axis = col[2])
legend("top", legend = c("z1", "z2"), lty = 1,
col = col, text.col = col)

How to add a legend to a plot in R

I´m new to R. I cannot put a legend in a plot chart, there is someting wrong with my code, I´ve tried to put a legend on this plot. I did a hash to put some colors on my model, but now I can´t figure out how to put the correct legend on it. This is what I di:
myhash<-c(a="green",b="pink",c="blue",d="purple",e="orange",f="brown",g="yellow",
h="black",i="gray")
mycolor<-myhash[df$category]
plot(df$growth,df$tannin,col=mycolor,cex=1,pch=16,xlab="Crecimiento",ylab="Taninos", main = "Modelo lineal Taninos vs Crecimiento")
I tried to put the legend like this :
legend("topright",c(df$tannin),fill=c("green","pink","blue","purple","orange","brown","yellow",
"black","gray"))
Please let me know how can I fix it, Im very beginner in R. Also, I cannot use any library.
Thanks in advance.
The problem seems to be that there is no legend (the argument to function legend, not the function) to show as text beside the fill colors.
To use a named argument, legend = unique(names(mycolor)) solves the problem.
set.seed(2020)
df <- data.frame(category = rep(c("a","b", "c", "d"), 4),
growth = 1:16, tannin = cumsum(rnorm(16)))
myhash <- c(a = "green", b = "pink", c = "blue", d = "purple")
mycolor <- myhash[df$category]
plot(df$growth, df$tannin, col = mycolor, cex = 1, pch = 16,
xlab = "Crecimiento", ylab = "Taninos",
main = "Modelo lineal Taninos vs Crecimiento")
legend("topright", legend = unique(names(mycolor)), fill = mycolor)

Legend appears, but it does not show color

I am using R for plotting. When my graph plots the legend appears where I want it to be but the colors are missing. mtcars 2 is a modified version of mtcars (one of the pre-loaded data sets) that adds a model and country of origin to the data set. mtcars.pca is what I named my redundance analysis (rda function under vegan), and mtcars.clust is titled for hierarchical clustering of the continuous factors of mtcars (hclust function of vegan) Below is the code I am using with mtcars2.
pca.fig = ordiplot(mtcars.pca, type = "none", las=1, xlim=c(-15,15), ylim = c(-20,10))
points(pca.fig, "sites", pch = 19, col = "green", select = mtcars2$origin =="domestic")
points(pca.fig, "sites", pch = 19, col = "blue", select = mtcars2$origin =="foreign")
ordiellipse(mtcars.pca, mtcars2$origin, conf = 0.95, label = FALSE)
ordicluster(mtcars.pca, mtcars.clust, col = "gray")
legend("bottomright", title="Car Origin", c("domestic", "foreign"), col = "origin")
You need to specify a vector of colours in legend and also a pch:
library("vegan")
data(dune, dune.env)
ord <- rda(dune)
plot(ord, type = "n")
cols <- c("red","blue","green")
points(ord, col = cols[dune.env$Use], pch = 19)
legend("bottomright", legend = levels(dune.env$Use), bty = "n",
col = cols, pch = 19)
If you don't add pch but just use col = cols legend() doesn't display any points. Because you used pch = 19 in your points() calls, use the same in the legend() call.
Also, note how to plot points of different colours in a single pass. I have some examples and explanation that go through the indexing trick I used in my code above to achieve this in a blog post of mine from a few years ago: http://www.fromthebottomoftheheap.net/2012/04/11/customising-vegans-ordination-plots/
I came to this question having the next problem in xts object:
I wanted to plot all time-series in xts object with legend. Moreover, there were around 20.
I used (wrong):
plot(returns_xts)
addLegend(...)
Correct version:
plot(returns_xts, legend.loc = "bottomright", col=1:20, lty = 1)
There is legend.loc parameter
col = 1:20 generates colors for you
Result:

Delete Scale for Height in dendrogram visualisation

I can create a dendrogram using
x<-1:100
dim(x)<-c(10,10)
set.seed(1)
groups<-c("red","red", "red", "red", "blue", "blue", "blue","blue", "red", "blue")
x.clust<-as.dendrogram(hclust(dist(x)))
x.clust.dend <- x.clust
labels_colors(x.clust.dend) <- groups
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = groups, edgePar = "col") # add the colors.
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = 3, edgePar = "lwd") # make the lines thick
plot(x.clust.dend)
However I want to delete the scale of height information in the left as shown in Figure below.
My guess is that it should be extremely trivial but I am not able to find a way to do this. One solution which I don't want is using the ggplot2 as below:
ggplot(as.ggdend(dend2))
This is because I will loose some of the formatting like color_bars()
The graphical parameter 'axes = FALSE" can be used to remove the distance measure for the plot.dendogram command:
plot(x.clust.dend, axes=F)
This will produce the following dendogram without distance axis:
You can just set yaxt = "n"
plot(x.clust.dend, yaxt = "n")
You can add another axis with
axis(side = 2, labels = FALSE)

Overlay plots in R

I want to plot 3 barplots together in one graph based on values from different columns of a data frame.
It should look something like this.
The y-values of plot 1 are the sum of the y-values of plot 2 and 3. The color of plot 1 and 2 can be fully filled (e.g. blue & red), but the color of plot 3 has to be translucent.
I was able to make a plot for each column separately using the barplot() function, but I was not able to combine them in one graph.
barplot(covpatient[[1]]$cov, names.arg = covpatient[[1]]$exon, xlab = covpatient[[1]]$gene[1] , ylab = "read depth" , border = "blue", col = "blue")
barplot(covpatient[[1]]$plus, names.arg = covpatient[[1]]$exon, xlab = covpatient[[1]]$gene[1] , ylab = "read depth" , border = "red", col = "red")
barplot(covpatient[[1]]$min, names.arg = covpatient[[1]]$exon, xlab = covpatient[[1]]$gene[1] , ylab = "read depth" , border = "gray", col = "gray")
Could someone give me a hand?
I'm not exactly sure if this is what you want... but based on the graphic that you sent I think this will help:
require(ggplot2)
require(reshape2)
covpatient <-list()
covpatient$cov <-rnorm(100,2)
covpatient$plus <-rnorm(100,4)
covpatient$min <-rnorm(100,1)
plot_covpatient <- do.call(rbind,covpatient)
melted_plot_covpatient<-melt(plot_covpatient,value.name = 'Value')
ggplot(melted_plot_covpatient,aes(group=Var1))+
geom_density(aes(Value,colour=Var1,fill=Var1),alpha=.5)

Resources