1 Python-joypy 制作峰峦图
到网址:https://github.com/sbebo/joypy 可下载zip
JoyPy是一个基于 matplotlib+pandas 的单功能Python软件包,其目的仅在于:绘制Joyplots(又称脊线图)。
下载JoyPy包
pip install joypy #或者是从github下载 git clone git@github.com:sbebo/joypy.git cd joypy pip install .
原始数据形式:
import joypy import pandas as pd import numpy as np from matplotlib import pyplot as plt from matplotlib import cm iris = pd.read_csv("data/iris.csv") %matplotlib inline fig, axes = joypy.joyplot(iris)#连续值的列为一个"脊"
%matplotlib inline fig, axes = joypy.joyplot(iris, by="Name")#根据"Name"分组,每个Name是一行"脊",其中有多个,默认y轴一致 %matplotlib inline fig, axes = joypy.joyplot(iris, by="Name", ylim='own')#使用各自y值 但是这就不可比 建议使用: fig, axes = joypy.joyplot(iris, by="Name", overlap=3)
%matplotlib inline fig, axes = joypy.joyplot(iris, by="Name", column="SepalWidth", hist=True, bins=20, overlap=0, grid=True, legend=False)
温度
%matplotlib inline temp = pd.read_csv("data/daily_temp.csv",comment="%") temp.head()
%matplotlib inline labels=[y if y%10==0 else None for y in list(temp.Year.unique())]#只留下10的倍数的年份 避免太挤了 fig, axes = joypy.joyplot(temp, by="Year", column="Anomaly", labels=labels, range_style='own', #range_style='own'限制x显示范围不是所有的x轴 grid="y", linewidth=1, legend=True, figsize=(6,5),#grid="y"只显示y轴 fade=True加上就是显示原始值而不是估算的kde核密度值 title="Global daily temperature 1880-2014 (°C above 1950-80 average)", colormap=cm.autumn_r)
2 Python-Matplotlib 制作峰峦图
https://matplotlib.org/matplotblog/posts/create-ridgeplots-in-matplotlib/
3 R-ggridges 制作峰峦图
https://wilkelab.org/ggridges/
install.packages("ggridges") ggplot(diamonds, aes(x = price, y = cut)) + geom_density_ridges(scale = 4) + scale_y_discrete(expand = c(0, 0)) + # will generally have to set the `expand` option scale_x_continuous(expand = c(0, 0)) + # for both axes to remove unneeded padding coord_cartesian(clip = "off") + # to avoid clipping of the very top of the top ridgeline theme_ridges() #> Picking joint bandwidth of 458
ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = Month, fill = stat(x))) + geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) + scale_fill_viridis_c(name = "Temp. [F]", option = "C") + labs(title = 'Temperatures in Lincoln NE in 2016')
个人认为此图还能通过渐变颜色映射反映分布值的大小
可以做出很多调整:https://wilkelab.org/ggridges/articles/introduction.html
ggplot(iris, aes(x = Sepal.Length, y = Species, fill = Species)) + geom_density_ridges( aes(point_color = Species, point_fill = Species, point_shape = Species), alpha = .2, point_alpha = 1, jittered_points = TRUE ) + scale_point_color_hue(l = 40) + scale_discrete_manual(aesthetics = "point_shape", values = c(21, 22, 23))
另外一个风格:
https://www.ershicimi.com/p/fbc80ca437cbbdd6fed53ebda13719cd
github数据和代码下载:https://github.com/zonination/perceptions
原始数据:
#Import files, load plot and data packages, fire up the number machine. # setwd("~/Dropbox/R/Perceptions of Probability") probly <- read.csv("probly.csv", stringsAsFactors=FALSE) numberly <- read.csv("numberly.csv", stringsAsFactors=FALSE) library(tidyverse) library(ggjoy) library(scales) setwd("G:/RWORK/perceptions-master")#先改变工作空间目录 #Melt data into column format.将数据融合至两列,一列是变了名称 一列是值 numberly <- gather(numberly, "variable", "value", 1:10) numberly$variable <- gsub("[.]"," ",numberly$variable)#把.用空格置换 probly <- gather(probly, "variable", "value", 1:17) probly$variable <- gsub("[.]"," ",probly$variable) probly$value<-probly$value/100 # convert to % #Order in the court!按照想要的顺序排序 probly$variable <- factor(probly$variable, c("Chances Are Slight", "Highly Unlikely", "Almost No Chance", "Little Chance", "Probably Not", "Unlikely", "Improbable", "We Doubt", "About Even", "Better Than Even", "Probably", "We Believe", "Likely", "Probable", "Very Good Chance", "Highly Likely", "Almost Certainly")) numberly$variable <- factor(numberly$variable, c("Hundreds of", "Scores of", "Dozens", "Many", "A lot", "Several", "Some", "A few", "A couple", "Fractions of")) #Modify Theme: source("ztheme.R") #Plot probability data ggplot(probly,aes(variable,value))+ geom_boxplot(aes(fill=variable),alpha=.5)+ geom_jitter(aes(color=variable),size=3,alpha=.2)+ scale_y_continuous(breaks=seq(0,1,.1), labels=scales::percent)+ guides(fill=FALSE,color=FALSE)+ labs(title="Perceptions of Probability", x="Phrase", y="Assigned Probability", caption="created by /u/zonination")+ coord_flip()+ z_theme() ggsave("plot1.png", height=8, width=10, dpi=120, type="cairo-png") #Plot numberly data ggplot(numberly,aes(variable,value))+ geom_boxplot(aes(fill=variable),alpha=0.5)+ geom_jitter(aes(color=variable),size=3,alpha=.2)+ scale_y_log10(labels=trans_format("log10",math_format(10^.x)), breaks=10^(-2:6))+ guides(fill=FALSE,color=FALSE)+ labs(title="Perceptions of Probability", x="Phrase", y="Assigned Number", caption="created by /u/zonination")+ coord_flip()+ z_theme() ggsave("plot2.png", height=5, width=8, dpi=120, type="cairo-png") # Joyplot for probly ggplot(probly,aes(y=variable,x=value))+ geom_joy(scale=4, aes(fill=variable), alpha=3/4)+ scale_x_continuous(breaks=seq(0,1,.1), labels=scales::percent)+ guides(fill=FALSE,color=FALSE)+ labs(title="Perceptions of Probability", y="", x="Assigned Probability", caption="created by /u/zonination")+ z_theme() ggsave("joy1.png", height=8, width=10, dpi=120, type="cairo-png") #Joyplot for numberly ggplot(numberly,aes(y=variable,x=value))+ geom_joy(aes(fill=variable, alpha=3/4))+ scale_x_log10(labels=trans_format("log10",math_format(10^.x)), breaks=10^(-2:6))+ guides(fill=FALSE,color=FALSE)+ labs(title="Perceptions of Probability", x="Assigned Number", y="", caption="created by /u/zonination")+ z_theme() ggsave("joy2.png", height=5, width=8, dpi=120, type="cairo-png")
参考: