#就是要你好看
library(ggsci)
library(ggplot2)
set.seed(2019)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_aaas()+
theme_classic()+
labs(title = 'AAAS',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_lancet()+
theme_classic()+
labs(title = 'LANCET',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_npg()+
theme_classic()+
labs(title = 'NPG',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_jama()+
theme_classic()+
labs(title = 'JAMA',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_jco()+
theme_classic()+
labs(title = 'JCO',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_nejm()+
theme_classic()+
labs(title = 'NEJM',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_d3()+
theme_classic()+
labs(title = 'D3',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_ucscgb()+
theme_classic()+
labs(title = 'UCSCGB',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
#RColorBrewer配色方案 已经整合进了 ggplot2 包内
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_brewer(palette = 'Set1')+ # 设置颜色的代码 在此
theme_classic()+
labs(title = 'RCB-Set1',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_brewer(palette = 'Set2')+
theme_classic()+
labs(title = 'RCB-Set2',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_brewer(palette = 'Set3')+
theme_classic()+
labs(title = 'RCB-Set3',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_brewer(palette = 'Accent')+
theme_classic()+
labs(title = 'RCB-Accent',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_brewer(palette = 'Pastel1')+
theme_classic()+
labs(title = 'RCB-Pastel1',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_brewer(palette = 'Pastel2')+
theme_classic()+
labs(title = 'RCB-Pastel2',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
#paletteer配色 最强大的配色方案 此包跑不出来
# devtools::install_github("EmilHvitfeldt/paletteer")
# rm(paletteer)
# library(paletteer)
# library(ggplot2)
#
# View(palettes_d_names)
#
# ggplot(dsamp,aes(carat, price, fill = cut))+
# geom_point(shape = 21, color = 'black', size = 3)+
# scale_colour_paletteer_d(palette = 'mpalette', direction = 1, dynamic = FALSE)+
# # scale_fill_paletteer_d(package = 'awtools',
# # palette = 'mpalette')+ 此处有改动
# theme_classic()+
# labs(title = 'awtools-mpalette',x='',y='')+
# theme(legend.position = c(.15,.8),
# legend.background = element_rect(fill = NA),
# plot.title = element_text(hjust = 0.5))
#
# ggplot(dsamp,aes(carat, price, fill = cut))+
# geom_point(shape = 21, color = 'black', size = 3)+
# scale_fill_paletteer_d(package = 'ggthemes',
# palette = 'Tableau20')+
# theme_classic()+
# labs(title = 'ggthemes-Tableau 20',x='',y='')+
# theme(legend.position = c(.15,.8),
# legend.background = element_rect(fill = NA),
# plot.title = element_text(hjust = 0.5))
#
# ggplot(dsamp,aes(carat, price, fill = cut))+
# geom_point(shape = 21, color = 'black', size = 3)+
# scale_fill_paletteer_d(package = 'jcolors',
# palette = 'pal2')+
# theme_classic()+
# labs(title = 'jcolors-pal2',x='',y='')+
# theme(legend.position = c(.15,.8),
# legend.background = element_rect(fill = NA),
# plot.title = element_text(hjust = 0.5))
#
# ggplot(dsamp,aes(carat, price, fill = cut))+
# geom_point(shape = 21, color = 'black', size = 3)+
# scale_fill_paletteer_d(package = 'LaCroixColoR',palette = 'PassionFruit')+
# theme_classic()+
# labs(title = 'LaCroxColoR-PassionFruit',x='',y='')+
# theme(legend.position = c(.15,.8),
# legend.background = element_rect(fill = NA),
# plot.title = element_text(hjust = 0.5))
#
# ggplot(dsamp,aes(carat, price, fill = cut))+
# geom_point(shape = 21, color = 'black', size = 3)+
# scale_fill_paletteer_d(package = 'NineteenEightyR',palette = 'miami2')+
# theme_classic()+
# labs(title = 'NineteenEightyR-miami2',x='',y='')+
# theme(legend.position = c(.15,.8),
# legend.background = element_rect(fill = NA),
# plot.title = element_text(hjust = 0.5))
#
# ggplot(dsamp,aes(carat, price, fill = cut))+
# geom_point(shape = 21, color = 'black', size = 3)+
# scale_fill_paletteer_d(package = 'rcartocolor',palette = 'Temps')+
# theme_classic()+
# labs(title = 'rcartocolor-Temps',x='',y='')+
# theme(legend.position = c(.15,.8),
# legend.background = element_rect(fill = NA),
# plot.title = element_text(hjust = 0.5))
#谁都不服,那就自己来
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_manual(values = c('#a6cee3','#1f78b4',
'#b2df8a','#33a02c','#fb9a99'))+
theme_classic()+
labs(title = 'Customized palette',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5))
#连续色
library(scales)
#单色
sequential <- c('#f7fcfd','#e5f5f9','#ccece6','#99d8c9','#66c2a4',
'#41ae76','#238b45','#006d2c','#00441b')
show_col(sequential)
#双色
diverging <- c('#d73027', '#f46d43','#fdae61','#fee08b','#ffffbf',
'#d9ef8b','#a6d96a','#66bd63','#1a9850')
show_col(diverging)
#选择合适的主题风格
set.seed(2019)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
# default theme
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_manual(name = '',
values = c('#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99'))+
labs(title = 'default theme',x='',y='')+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA,color = NA),
plot.title = element_text(hjust = 0.5))
# bw theme
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_manual(name = '',
values = c('#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99'))+
labs(title = 'bw theme',x='',y='')+
theme_bw()+ # 此行代码为主题
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA,color = NA),
plot.title = element_text(hjust = 0.5))
# light theme
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_manual(name = '',
values = c('#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99'))+
labs(title = 'light theme',x='',y='')+
theme_light()+ # 此行代码为主题
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA,color = NA),
plot.title = element_text(hjust = 0.5))
# classic theme
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_manual(name = '',
values = c('#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99'))+
labs(title = 'classic theme',x='',y='')+
theme_classic()+ # 此行代码为主题
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA,color = NA),
plot.title = element_text(hjust = 0.5))
# minimal theme'
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_manual(name = '',
values = c('#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99'))+
labs(title = 'minimal theme',x='',y='')+
theme_minimal()+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA,color = NA),
plot.title = element_text(hjust = 0.5))
# test theme
ggplot(dsamp,aes(carat, price, fill = cut))+
geom_point(shape = 21, color = 'black', size = 3)+
scale_fill_manual(name = '',
values = c('#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99'))+
labs(title = 'test theme',x='',y='')+
theme_test()+
theme(legend.position = c(.15,.8),
legend.background = element_rect(fill = NA,color = NA),
plot.title = element_text(hjust = 0.5))
#背景网格修饰
ggplot(dsamp,aes(cut, weight = price, fill = color))+
geom_bar(width = 0.8, position = 'dodge', color = 'gray40',size = .5)+
scale_fill_npg()+
scale_y_continuous(expand = c(0,0),limits = c(0,5e5))+
theme_bw()+
theme(legend.position = c(.12,.7),
legend.background = element_rect(fill = NA,color = NA))
ggplot(dsamp,aes(cut, weight = price, fill = color))+
geom_bar(width = 0.8, position = 'dodge', color = 'gray40',size = .5)+
scale_fill_npg()+
scale_y_continuous(expand = c(0,0),limits = c(0,5e5))+
theme_bw()+
theme(legend.position = c(.12,.7),
legend.background = element_rect(fill = NA,color = NA),
panel.grid.major.x = element_blank(), # panel背景.grid网格.major.x轴上主要的网格线
panel.grid.minor.y = element_blank()) # panel.grid.minor.y y轴上非正对刻度的网格线
ggplot(dsamp,aes(cut, weight = price, fill = color))+
geom_hline(yintercept = seq(1e5,4e5,1e5),color = 'gray')+
# 添加了正对刻度的横线,添加纵线用 geom_wline
geom_bar(width = 0.8, position = 'dodge', color = 'gray40',size = .5)+
scale_fill_npg()+
scale_y_continuous(expand = c(0,0),limits = c(0,5e5))+
theme_classic()+
theme(legend.position = c(.12,.7),
legend.background = element_rect(fill = NA,color = NA))
ggplot(dsamp,aes(cut, weight = price, fill = color))+
geom_bar(width = 0.8, position = 'dodge', color = 'gray40',size = .5)+
scale_fill_npg()+
scale_y_continuous(expand = c(0,0),limits = c(0,5e5))+
theme_classic()+
theme(legend.position = c(.12,.7),
axis.line = element_blank(), # 去掉横纵坐标轴
legend.background = element_rect(fill = NA,color = NA),
panel.grid.major.y = element_line(color = 'gray')) # 添加了正对刻度的横线
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.