Python和R語言峰巒圖制作


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 \n(°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")

   

 

參考:

https://mp.weixin.qq.com/s/UBzPDTc3lwCbeI-q1Bfj-w


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM