绘制基本散点图
rm(list = ls())
#加载R包
library(ggplot2)
#数据——以R自带示例数据iris为例
df<-iris
#绘制基本散点图
p<-ggplot(df) +
geom_point(aes(x = Sepal.Length, y = Sepal.Width,
alpha= Petal.Length ,
color = Species,
size = Petal.Width))
p
ggplot2默认主题
1、theme_gray()
p1<-p+theme_gray()+ggtitle("theme_gray()")+
theme(legend.position = 'none')
p1
2、theme_bw()
p2<-p+theme_bw()+ggtitle("theme_bw()")+
theme(legend.position = 'none')
p2
3、theme_classic()
p3<-p+theme_classic()+ggtitle("theme_classic()")+
theme(legend.position = 'none')
p3
4、theme_light()
p4<-p+theme_light()+ggtitle("theme_light()")+
theme(legend.position = 'none')
p4
5、theme_void()
p5<-p+theme_void()+ggtitle("theme_void()")+
theme(legend.position = 'none')
p5
6、theme_linedraw()
p6<-p+theme_linedraw()+ggtitle("theme_linedraw()")+
theme(legend.position = 'none')
p6
7、theme_minimal()
p7<-p+theme_minimal()+ggtitle("theme_minimal()")+
theme(legend.position = 'none')
p7
8、theme_dark()
p8<-p+theme_dark()+ggtitle("theme_dark()")+
theme(legend.position = 'none')
p8
cowplot::plot_grid(p1,p2,p3,p4,p5,p6,p7,p8,ncol = 4)
ggthemes拓展主题
#加载包
library(ggthemes)
1、theme_clean()
p+theme_clean()
2、theme_calc()
p+theme_calc()
其他主题如下,具体展示图片见公众号推文
p+theme_economist()
p+theme_igray()
p+theme_fivethirtyeight()
p+theme_pander()
p+theme_foundation()
p+theme_base()
p+theme_par()
p+theme_gdocs()
p+theme_map()
p+theme_few()
p+theme_tufte()
p+theme_stata()
p+theme_excel()
p+theme_wsj()
p+theme_hc()
p+theme_solid()
p+theme_solarized()
ggprism包拓展主题
#加载包
library(ggprism)
names(ggprism_data$themes)
#44种主题样式名称,通过替换名称可实现更换主题风格目的
示例,具体细节可参考此前推文:
p+theme_prism(palette = "autumn_leaves")
p+theme_prism(palette = "pearl")
p+theme_prism(palette = "summer")
p+theme_prism(palette = "spring")
p+theme_prism(palette = "ocean")
BBC风格---bbplot
#安装包
devtools::install_github('bbc/bbplot')
#加载包
library(bbplot)
#使用方式
p+bbc_style()
具体细节可参见文章:https://www.jianshu.com/p/c1436793ad67