R語言繪圖包12--向日葵圖的繪制


R語言繪圖包系列:


1. hexin包

# Packages
library(hexbin)
library(RColorBrewer)

# Create data
x <- rnorm(mean=1.5, 5000)
y <- rnorm(mean=1.6, 5000)

# Make the plot
bin<-hexbin(x, y, xbins=40)
my_colors=colorRampPalette(rev(brewer.pal(11,'Spectral')))
plot(bin, main="" , colramp=my_colors , legend=F ) 

2. ggplot2 (geom_hex)

# install.packages("ggplot2")
library(ggplot2)

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex()
Few bins

You can control de number of bins in both vertical and horizontal directions. Default value is 30.

# install.packages("ggplot2")
library(ggplot2)

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex(bins = 15)
Too many bins

Note that if you set too many bins the hexbin chart will look like a classical scatter plot.

# install.packages("ggplot2")
library(ggplot2)

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex(bins = 60)
Border color

The border for all hexagons can be customized with the color argument of the geom_hex function.

# install.packages("ggplot2")
library(ggplot2)

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex(color = "white")
Fill and transparency

You can also set a fill color for all the hexagons with fill and control the transparency of the colors with alpha.

# install.packages("ggplot2")
library(ggplot2)

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex(color = 1, fill = 4, alpha = 0.4) 
Color palette

If you want to keep the gradient color palette representing values you can change the default colors with scale_fill_viridis_c, scale_fill_gradient or a similar function.

# install.packages("ggplot2")
library(ggplot2)

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex() +
  scale_fill_viridis_c()
Legend customization

Width and height

# install.packages("ggplot2")
library(ggplot2)

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex() +
  guides(fill = guide_colourbar(title = "Count"))

Remove the labels and the ticks

# install.packages("ggplot2")
library(ggplot2)

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex() +
  guides(fill = guide_colourbar(label = FALSE,
                                ticks = FALSE))

Remove the legend

# install.packages("ggplot2")
library(ggplot2)

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex() +
  theme(legend.position = "none")

參考:

  1. Hexbin chart with the hexbin package
  2. Hexin chart in ggplot2
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容