R语言绘图

R语言三大绘图R包之1---ggplot2

2021-04-08  本文已影响0人  Seurat_Satija
require(ggplot2)
data(diamonds)
set.seed(42)
small <- diamonds[sample(nrow(diamonds), 1000), ]
View(small)
head(small)
## # A tibble: 6 x 10
##   carat cut       color clarity depth table price     x     y     z
##   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.39 Ideal     I     VVS2     60.8    56   849  4.74  4.76  2.89
## 2  1.12 Very Good G     SI2      63.3    58  4478  6.7   6.63  4.22
## 3  0.51 Very Good G     VVS2     62.9    57  1750  5.06  5.12  3.2 
## 4  0.52 Very Good D     VS1      62.5    57  1829  5.11  5.16  3.21
## 5  0.28 Very Good E     VVS2     61.4    55   612  4.22  4.25  2.6 
## 6  1.01 Fair      F     SI1      67.2    60  4276  6.06  6     4.05
p <- ggplot(data = small, mapping = aes(x = carat, y = price))
p + geom_point()
image.png
p <- ggplot(data=small, mapping=aes(x=carat, y=price, shape=cut))
p+geom_point()
image.png
p <- ggplot(data=small, mapping=aes(x=carat, y=price, shape=cut, colour=color))
p+geom_point()
image.png
p <- ggplot(small)
p+geom_point(aes(x=carat, y=price, shape=cut, colour=color))
image.png
ggplot(small)+geom_histogram(aes(x=price))
image.png
ggplot(small)+geom_histogram(aes(x=price, fill=cut))
image.png
ggplot(small)+geom_histogram(aes(x=price, fill=cut), position="dodge")
image.png
ggplot(small)+geom_histogram(aes(x=price, fill=cut), position="fill")
image.png
ggplot(small)+geom_bar(aes(x=clarity))
image.png
ggplot()+geom_bar(aes(x=c(LETTERS[1:3]),y=1:3), stat="identity")
image.png
ggplot(small)+geom_density(aes(x=price, colour=cut))
image.png
ggplot(small)+geom_density(aes(x=price,fill=clarity))
image.png
ggplot(small)+geom_boxplot(aes(x=cut, y=price,fill=color))
image.png
ggplot(small)+geom_point(aes(x=carat, y=price, shape=cut, colour=color))+scale_y_log10()+scale_colour_manual(values=rainbow(7))
image.png
ggplot(small, aes(x=carat, y=price))+geom_point()+scale_y_log10()+stat_smooth()
image.png
ggplot(small)+geom_bar(aes(x=cut, fill=cut))+coord_flip()
image.png
ggplot(small)+geom_bar(aes(x=factor(1), fill=cut))+coord_polar(theta="y")
image.png
ggplot(small)+geom_bar(aes(x=factor(1), fill=cut))+coord_polar()
image.png
ggplot(small)+geom_bar(aes(x=clarity, fill=cut))+coord_polar()
image.png
ggplot(small, aes(x=carat, y=price))+geom_point(aes(colour=cut))+scale_y_log10() +facet_wrap(~cut)+stat_smooth()
image.png
p <- ggplot(small)+geom_boxplot(aes(x=cut, y=price,fill=color))
p + ggtitle("Price vs Cut")+xlab("Cut")+ylab("Price")
image.png
require(ggthemes)
p + theme_wsj()
image.png
f <- function(x) 1/(x^2-1)
x <- seq(-3,3, by=0.001)
y <- f(x)
d <- data.frame(x=x,y=y)

p <- ggplot()
p <- p+geom_rect(fill = "white",color="black",size=3,
                 aes(NULL, NULL,xmin=-3, xmax=3,
                     ymin=-3,ymax=3, alpha=0.1))

p <- p + geom_line(data=d, aes(x,y), size=3)+ylim(-3,3)
theme_null <- function() {
  theme_bw() %+replace%
    theme(axis.text.x=element_blank(),
          axis.text.y=element_blank(),
          legend.position="none",
          panel.grid.minor=element_blank(),
          panel.grid.major=element_blank(),
          panel.background=element_blank(),
          axis.ticks=element_blank(),
          panel.border=element_blank())
}

p+theme_null()+xlab("")+ylab("")
image.png
ggplot(diamonds, aes(carat, price))+ stat_density2d(aes(fill = ..level..), geom="polygon")+ scale_fill_continuous(high='darkred',low='darkgreen')
image.png
Normal <- c(0.83, 0.79, 0.99, 0.69)
Cancer <- c(0.56, 0.56, 0.64, 0.52)
m <- c(mean(Normal), mean(Cancer))
s <- c(sd(Normal), sd(Cancer))
d <- data.frame(V=c("Normal", "Cancer"), mean=m, sd=s)
d$V <- factor(d$V, levels=c("Normal", "Cancer"))

p <- ggplot(d, aes(V, mean, fill=V, width=.5))
p <- p+geom_errorbar(aes(ymin=mean, ymax=mean+sd, width=.2), 
                     position=position_dodge(width=.8))
p <- p + geom_bar(stat="identity", position=position_dodge(width=.8), colour="black")
p <- p + scale_fill_manual(values=c("grey80", "white"))
p <- p + theme_bw() +theme(legend.position="none") + xlab("") + ylab("")
p <- p + theme(axis.text.x = element_text(face="bold", size=12), 
               axis.text.y = element_text(face="bold", size=12))
p <- p+scale_y_continuous(expand=c(0,0), limits=c(0, 1.2), breaks=seq(0, 1.2, by=.2))
p <- p+geom_segment(aes(x=1, y=.98, xend=1, yend=1.1))
p <- p+geom_segment(aes(x=2, y=.65, xend=2, yend=1.1))
p <- p+geom_segment(aes(x=1, y=1.1, xend=2, yend=1.1))
p <- p + annotate("text", x=1.5, y=1.12, label="*")#添加显著性标志
print(p)
image.png
参考 https://mp.weixin.qq.com/s/oLgpTGdQgcka-OD757_3lA
上一篇下一篇

猜你喜欢

热点阅读