python数据可视化之美专业图表绘制指南_2019第23周：评《R语言数据可视化之美：专业图表绘制指南》...

--- 大师，大师，618买的什么书呀，好奇呢。

··· 一本R语言绘图的书，可花了呢。

"Beautiful Visualization with R" provides a comprehensive introduction to the many types of visualisation that you can create with R. As well as foundational visualisations like bar charts, scatterplots, and histogram, this book will teach you about more complex tools like 3d plots, ternary plots, polar coordinate plots and more! Read this book to learn how to create visually compelling plots that help you understand your data. ——Hadley Wickham RStudio 首席科学家，莱斯大学统计系助理教授，ggplot2等软件包开发者

#EasyCharts团队出品，

#如需使用与深入学习，请联系微信：EasyCharts

#---------------------------------------图5-1-2核密度估计峰峦图--------------------------------------

library(ggplot2)

library(ggridges)

library(RColorBrewer)

ggplot(lincoln_weather, aes(x = Mean Temperature [F], y = Month, fill = ..density..)) +

geom_density_ridges_gradient(scale = 3, rel_min_height = 0.00,size = 0.3) +

###------------------------------自定义编写代码实现：图5-1-2核密度估计峰峦图-------------------------------

library(reshape2)

colormap

dt

splitdata

xmax

xmin

N

labels_y

mydata

for (i in 1:N){

tempy

newdata

newdata$variable mydata } Step mydata$offest

mydata$V1_density_offest p for (i in 1:N){ p geom_line(data=mydata[mydata$variable==i,],aes(x=x, y=V1_density_offest),color="black",size=0.5)

}

scale_y_continuous(breaks=seq(-Step,-Step*N,-Step),labels=labels_y)+

xlab("Mean Temperature [F]")+

ylab("Month")+

theme_classic()+

theme(

panel.background=element_rect(fill="white",colour=NA),

panel.grid.major.x = element_line(colour = "grey80",size=.25),

panel.grid.major.y = element_line(colour = "grey60",size=.25),

axis.line = element_blank(),

text=element_text(size=15,colour = "black"),

plot.title=element_text(size=15,hjust=.5),

legend.position="right"

)

#EasyCharts团队出品，

#如有问题修正与深入学习，可联系微信：EasyCharts

library(ggplot2)

library(grid)

library(RColorBrewer)

library(dplyr)

install.packages("SuppDists")

library(SuppDists) #提供rJohnson()函数

# somewhat hackish solution to:

# based mostly on copy/pasting from ggplot2 geom_violin source:

"%||%"

if (!is.null(a)) a else b

}

color

geom_flat_violin

position = "dodge", trim = TRUE, scale = "area",

show.legend = NA, inherit.aes = TRUE, ...) {

layer(

data = data,

mapping = mapping,

stat = stat,

geom = GeomFlatViolin,

position = position,

show.legend = show.legend,

inherit.aes = inherit.aes,

params = list(

trim = trim,

scale = scale,

...

)

)

}

GeomFlatViolin

ggproto("GeomFlatViolin", Geom,

setup_data = function(data, params) {

data$width params$width %||% (resolution(data$x, FALSE) * 0.9) # ymin, ymax, xmin, and xmax define the bounding rectangle for each group data %>% group_by(group) %>% mutate(ymin = min(y), ymax = max(y), xmin = x, xmax = x + width / 2) }, draw_group = function(data, panel_scales, coord) { # Find the points for the line to go all the way around data xmaxv = x + violinwidth * (xmax - x)) #利用transform函数为数据框mydata增加数据 newdata newdata_Polygon newdata_Polygon$colour

newdata_Path

ggplot2:::ggname("geom_flat_violin", grobTree(

GeomPolygon$draw_panel(newdata_Polygon, panel_scales, coord), GeomPath$draw_panel(newdata_Path, panel_scales, coord))

)

},

draw_key = draw_key_polygon,

default_aes = aes(weight = 1, colour = "grey20", fill = "white", size = 0.5,

alpha = NA, linetype = "solid"),

required_aes = c("x", "y")

)

# "%||%"

# "%>%" %", "magrittr")

set.seed(141079)

# Generate sample data -------------------------------------------------------

findParams

value

as.double(skew), as.double(kurt - 3), gamma = double(1),

delta = double(1), xi = double(1), lambda = double(1),

type = integer(1), PACKAGE = "SuppDists")

list(gamma = value$gamma, delta = value$delta,

xi = value$xi, lambda = value$lambda,

type = c("SN", "SL", "SU", "SB")[value\$type])

}

# 均值为3，标准差为1的正态分布

n

# Johnson分布的偏斜度2.2和峰度13

s

# Johnson分布的偏斜度0和峰度20)

k

# 两个峰的均值μ1，μ2分别为1.89和3.79，σ1 = σ2 =0.31

mm

mydata

Class = factor(rep(c("n", "s", "k", "mm"), each = 100),

c("n", "s", "k", "mm")),

Value = c(n, s, k, mm)+3

)

#-------------------------------------------------------------

colnames(mydata)

d %

summarize(mean = mean(Value),

sd = sd(Value))

plot1

geom_flat_violin(position=position_nudge(x=.2)) +

geom_jitter(aes(color=Class), width=.1) +

geom_pointrange(aes(y=mean, ymin=mean-sd, ymax=mean+sd),

data=d, size=1, position=position_nudge(x=.2)) +

coord_flip() +

theme_bw() +

theme( axis.text = element_text(size=13),

axis.title = element_text(size=15),

legend.position="none")

plot2

geom_flat_violin(aes(fill=Class),position=position_nudge(x=.25),color="black") +

geom_jitter(aes(color=Class), width=0.1) +

geom_boxplot(width=.1,position=position_nudge(x=0.25),fill="white",size=0.5) +

coord_flip() +

theme_bw() +

theme( axis.text = element_text(size=13),

axis.title = element_text(size=15),

legend.position="none")

library(gridExtra)

grid.arrange(plot1,plot2,ncol = 2, nrow = 1)

《樊绍述墓志铭》：“必出于己，不蹈袭前人一言一句，又何其难也。”

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

07-17

04-23 172