📜  R中的圆形条形图和自定义

📅  最后修改于: 2022-05-13 01:54:54.817000             🧑  作者: Mango

R中的圆形条形图和自定义

在本文中,我们将了解如何在 R 编程语言中创建圆形条形图和自定义。

圆形条形图类似于条形图,但它使用极坐标而不是笛卡尔坐标。圆形条形图是条形图以圆形而不是线的形式呈现。本文将向您展示如何使用 R 和 ggplot2 创建此类图表。它包含可重现的代码并解释了如何使用 coord_polar() 方法。

定义数据

要在 barplot 中使用数据集,我们需要创建数据集,所以在这里我们将创建它。

R
# Libraries
library(tidyverse) # help you to prepare the data
library(ggplot2) # help you to prepare the plots
 
# prepare dataset
data = data.frame(
  # add a parameter with a range list 1-100
  index = seq(1,100),
  # create labelled parameter
  label = paste( data ="Data ",
                    seq(1,100),
                    sep="= "),
  # random values in the range 1 - 100
  values = sample( seq(10,100), 100, replace = T)
)
 
# top five values of the dataframe
head(data)


R
# Make the plot
p <- ggplot(data, aes(x = as.factor(index), # x-axis factor label
                       
                      # y-axis numerical parameter
                      y = values)) +     
   
  # the bar height will represent
  # the actual value of the data
  geom_bar(stat = "identity",
           fill=alpha("green", 0.5)) + # define bar color
   
  # define size of inner circle
  # and the size of the bar
  ylim(-100,120) +
   
  # define the polar coordinate
  coord_polar(start = 0)
 
# plot
p


R
# Adding labels to the plot
 
data_with_labels = data
 
# number of labels required
number_of_label <- nrow(data_with_labels)
# find the angle of rotation of the label
angle <-  90 - 360 * (data_with_labels$index - 0.5) /number_of_label    
 
# check the label alignment - right or left
data_with_labels$hjust<-ifelse( angle < -90, 1, 0)
# check the label angle
data_with_labels$angle<-ifelse(angle < -90,
                               angle + 180, angle)
 
 
 
# Make the plot
# x-axis factor label
p <- ggplot(data, aes(x = as.factor(index),
                       
                      # y-axis numerical parameter
                      y = values)) +     
   
  # the bar height will represent
  # the actual value of the data
  geom_bar(stat = "identity",
            
           # define bar color
           fill=alpha("green", 0.5)) +
   
  # define size of inner circle
  # and the size of the bar
  ylim(-100,120) +
   
  # define the polar coordinate
  coord_polar(start = 0) +
 
  # add labels
  geom_text(data = data_with_labels,
            aes(x = index, y = values+10,
                 
                # label alignment
                label = label, hjust=hjust),
            color = "black", fontface="bold",
            alpha = 0.6, size = 2.5,
            angle = data_with_labels$angle,
            inherit.aes = FALSE )
 
p



输出:

index   label  values 
1     1 Data -1     28 
2     2 Data -2     46 
3     3 Data -3     54 
4     4 Data -4     25 
5     5 Data -5     43 
6     6 Data -6     26

示例 1:基本圆形条形图

coord_polar() 方法用于在特定坐标中创建绘图。

R

# Make the plot
p <- ggplot(data, aes(x = as.factor(index), # x-axis factor label
                       
                      # y-axis numerical parameter
                      y = values)) +     
   
  # the bar height will represent
  # the actual value of the data
  geom_bar(stat = "identity",
           fill=alpha("green", 0.5)) + # define bar color
   
  # define size of inner circle
  # and the size of the bar
  ylim(-100,120) +
   
  # define the polar coordinate
  coord_polar(start = 0)
 
# plot
p


输出:

示例 2:为数据添加标签

要将标签和数据添加到其中,将使用 geom_text() 方法。

R

# Adding labels to the plot
 
data_with_labels = data
 
# number of labels required
number_of_label <- nrow(data_with_labels)
# find the angle of rotation of the label
angle <-  90 - 360 * (data_with_labels$index - 0.5) /number_of_label    
 
# check the label alignment - right or left
data_with_labels$hjust<-ifelse( angle < -90, 1, 0)
# check the label angle
data_with_labels$angle<-ifelse(angle < -90,
                               angle + 180, angle)
 
 
 
# Make the plot
# x-axis factor label
p <- ggplot(data, aes(x = as.factor(index),
                       
                      # y-axis numerical parameter
                      y = values)) +     
   
  # the bar height will represent
  # the actual value of the data
  geom_bar(stat = "identity",
            
           # define bar color
           fill=alpha("green", 0.5)) +
   
  # define size of inner circle
  # and the size of the bar
  ylim(-100,120) +
   
  # define the polar coordinate
  coord_polar(start = 0) +
 
  # add labels
  geom_text(data = data_with_labels,
            aes(x = index, y = values+10,
                 
                # label alignment
                label = label, hjust=hjust),
            color = "black", fontface="bold",
            alpha = 0.6, size = 2.5,
            angle = data_with_labels$angle,
            inherit.aes = FALSE )
 
p


输出: