rm(list = ls())
gc()
library(ggplot2)
data(mtcars)
?mtcars
#cyl:气缸数,mpg:英里/加仑,gear:档数
ggplot(mtcars, aes(x=factor(cyl), y=mpg, fill=factor(gear), colour=factor(gear))) +
geom_point(position = position_jitterdodge()) +
geom_boxplot(alpha=0.5)
#install.packages("beeswarm")
library(beeswarm)
data(breast)
head(breast)
# ER ESR1 ERBB2 time_survival event_survival
# 100.CEL.gz neg 8.372133 13.085894 39 1
# 103.CEL.gz pos 10.559356 9.491683 97 0
# 104.CEL.gz pos 12.299905 9.599574 11 1
# 105.CEL.gz pos 10.776632 9.681747 99 0
# 106.CEL.gz pos 10.505124 9.436763 40 1
# 107.CEL.gz neg 10.377741 8.695576 94 0
beeswarm(time_survival ~ event_survival, data = breast,
method = 'center',
pch = 16, pwcol = as.numeric(ER),
xlab = '', ylab = 'Follow-up time (months)',
labels = c('Censored', 'Metastasis'))#Censored:截尾;metastasis:转移
legend('topright', legend = levels(breast$ER),
title = 'ER', pch = 16, col = 1:2)
boxplot(time_survival ~ event_survival,
data = breast, add = T, names = c("",""), col="#0000ff22")
dev.off()
#png('diff_color.png',width=400*2,height=400*2,res=72*2,type='cairo-png')
beeswarm(time_survival ~ ER, data=breast,pch=16, pwcol=1+as.numeric(event_survival),xlab="",ylab="Follow-up time (months)",labels = c("ER neg", "ER pos"))
legend("topright",legend=c("yes","no"),title="Censored",pch=16,col=1:2)
dev.off()
#2.Compare the four methods for arranging points#点的组织方式可以多种,如("swarm", "center", "hex", "square")
## Generate some random data
set.seed(123)
distro <- list(runif = runif(100, min = -3, max = 3), rnorm = rnorm(100))
#png("arrange_points.png",width=600*2,height=600*2,res=72*2,type='cairo-png')
par(mfrow=c(2,2))#两行两列画布
for (m in c("swarm", "center", "hex", "square")) {
beeswarm(distro, col = 2:3, pch = 16,method = m,
main = paste('method = "', m, '"', sep = ''))
}
dev.off()