MyVector<-rnorm(10) plot(MyVector,main="Scatterplot",pch=1)
You can customise the type of points you want using e.g. pch=22
You can customize also color
plot(MyVector,main="Scatterplot",pch=19,col=1)
or use name like:
plot(MyVector,main="Scatterplot",pch=19,col="red")
color names
You can use factors to define your color
head(MyDataFrame)
## Genes FoldChange pvalue ## 1 Gene_1 -0.6264538 8.960140e-01 ## 2 Gene_2 0.1836433 1.934082e-01 ## 3 Gene_3 -0.8356286 1.145058e-01 ## 4 Gene_4 1.5952808 6.642649e-06 ## 5 Gene_5 0.3295078 1.970055e-03 ## 6 Gene_6 -0.8204684 1.249134e-05
plot(MyDataFrame$FoldChange,-log10(MyDataFrame$pvalue),
pch=19,cex=.5,
col=c("black","red")[(MyDataFrame$pvalue < 0.05) +1])
abline(h=-log10(0.05))
legend("topright", legend = c("P>0.05","P<0.05"),
pch = 19, col = 1:2,)
before plot:
plot option:
after plot:
Example
data(iris)
par(mar=c(5,5,1,1))
par(mfrow=c(1,2))
plot(iris$Sepal.Length,iris$Sepal.Width, col=as.factor(iris$Species),
pch=as.numeric(as.factor(iris$Species))+16,xlab="Sepal Length",
ylab="Sepal Width",main="Sepal")
plot(iris$Petal.Length,iris$Petal.Width, col=as.factor(iris$Species),
pch=as.numeric(as.factor(iris$Species))+16, xlab="Petal Length",
ylab="Petal Width",main="Petal")
iris_mean_sepal_width<-aggregate( iris$Sepal.Width,list(iris$Species), mean) iris_mean_sepal_width
## Group.1 x ## 1 setosa 3.428 ## 2 versicolor 2.770 ## 3 virginica 2.974
barplot(iris_mean_sepal_width$x,
horiz=TRUE,
names.arg = iris_mean_sepal_width$Group.1)
you can use the library scales to add some transparency to color using alpha()
library(scales)
hist(iris$Sepal.Width[iris$Species == "setosa"],col=alpha("red",.5))
hist(iris$Sepal.Width[iris$Species == "versicolor"],add=T,col=alpha("blue",.5))
You can simply create boxplot with the boxplot() function. It will create a boxplot per column in your data frame
head(iris,n=3)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species ## 1 5.1 3.5 1.4 0.2 setosa ## 2 4.9 3.0 1.4 0.2 setosa ## 3 4.7 3.2 1.3 0.2 setosa
par(mar=c(3,4,1,1)) boxplot(iris)
Or you can use the linear model annotation
## Gene_Expression Treatment Batch ## 1 -0.3058154 Control B1 ## 2 0.8936737 Control B2 ## 3 -1.0472981 Control B3 ## 4 1.9713374 A_vehic B1
par(mar=c(3,4,1,1)) boxplot(Gene_Expression~Treatment,data=MyMetadata)
Using gplots package, we can plot heatmap and row color
iris_scaled<-apply(iris[,1:4],2,scale)
heatmap.2(t(iris_scaled),col=redgreen(75),trace="none",
ColSideColors = c("black","red","green")[factor(iris$Species)],
cexRow = .8,)
Another way to have nicer plot, is to use the ggplot2 package:
library(ggplot2)
ggplot(aes(FoldChange,-log10(pvalue),color=as.factor(pvalue<0.05)),data=MyDataFrame)+
geom_point()+
scale_color_manual("P<0.05",values=c("FALSE"="black","TRUE"="red"))+
theme_bw()
head(MyDataFrame)
## Genes FoldChange pvalue ## 1 Gene_1 -0.6264538 8.960140e-01 ## 2 Gene_2 0.1836433 1.934082e-01 ## 3 Gene_3 -0.8356286 1.145058e-01 ## 4 Gene_4 1.5952808 6.642649e-06 ## 5 Gene_5 0.3295078 1.970055e-03 ## 6 Gene_6 -0.8204684 1.249134e-05
MyPlot <- ggplot(aes(FoldChange,-log10(pvalue),color=as.factor(pvalue<0.05)),data=MyDataFrame)
MyPlot <- MyPlot + geom_point()
Here we rescaled color
MyPlot <- MyPlot + scale_color_manual(values=c("FALSE"="black","TRUE"="red"))
Finally we apply a theme:
MyPlot <- MyPlot + theme_bw()
(MyPlot + theme(legend.position = "none")) | # | tell patchwork to plot side by side plot
MyPlot + labs(title = "DE expression",subtitle = "Cortex",x="Fold-Change",y="-log10 Pvalue", color="Significant") +
geom_hline(yintercept = -log10(0.05))+geom_text_repel(aes(label=Genes),size=2) +
theme( plot.title=element_text(size=10,face="bold"),
axis.text=element_text(size=15),
axis.title=element_text(size=10,face="bold"))
## Warning: ggrepel: 78 unlabeled data points (too many overlaps). Consider ## increasing max.overlaps