美文网首页
R语言绘图包12--向日葵图的绘制

R语言绘图包12--向日葵图的绘制

作者: Hayley笔记 | 来源:发表于2023-07-22 13:28 被阅读0次

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

相关文章

网友评论

      本文标题:R语言绘图包12--向日葵图的绘制

      本文链接:https://www.haomeiwen.com/subject/gkyxpdtx.html