首页 > 解决方案 > 在 R ggplot 中,关于带有极坐标的“geom_rect”的几个问题

问题描述

在 R ggplot 中,使用 'geom_rect' 可以生成下图。现在,我有几个问题:</p>

  1. 有什么方法可以简化当前代码?
  2. 如何在标签中添加“金额”(目前仅包含“类别/子类别”)
  3. “填充颜色”很难看,特别是对于“子类别”(可能使用相同的颜色,只需更改“阿尔法”参数)
  4. 边界不是很光滑(锯齿状边界)

任何人都可以帮忙吗?谢谢!

   library(tidyverse)
    
    test_data <- data.frame(category = c("retail","retail","retail","retail","retail","hotel"),
                            sub_category=c("retail_sub1","retail_sub2","retail_sub3","retail_sub4","retail_sub5","hotel"),
                          amount=c(51083.26,27454.13,22495.89,21195.05,16863.69,60210.6))
    
    test_data1 <- test_data %>% mutate(ymax=cumsum(amount),
                         ymin=lag(cumsum(amount),default=0))
    
    
    test_data1$y_category_position[test_data1$category=='retail'] <- 
      max(test_data1$ymax[test_data1$category=='retail'])/2
    
    
    test_data1$y_category_position[test_data1$category=='hotel'] <- 
      max(test_data1$ymax[test_data1$category=='retail'])+
      max(test_data1$ymax[test_data1$category=='hotel'])/6 # tried may times, seems 6 for divisor is better 
    
      test_data1 %>% ggplot()+
      # for category bar and label
      geom_rect(aes(xmin=3,xmax=6,ymin=ymin,ymax=ymax,fill=category))+
      geom_text(aes(x=4.5,y=y_category_position,label=category))+
      # for sub_category bar and label (exclude category 'hotel')
      geom_rect(data= test_data1 %>% filter(sub_category !='hotel'),
                aes(xmin=6,xmax=8,
                    ymin=ymin,ymax=ymax,fill=sub_category))+
     geom_text(data= test_data1 %>% filter(sub_category !='hotel'),
               aes(x=7,y=(ymax+ymin)/2,label=sub_category))+
      coord_polar(theta = 'y')+
      theme_minimal()

在此处输入图像描述

标签: rggplot2

解决方案


以后,请在每个帖子中提出 1 个问题。

逐点回答你的问题。

  1. 这段代码可以简化吗?

是的,你基本上有一个堆积条形图,所以如果我们想要每个区域有 1 个标签,我们需要预先汇总数据,但不必费心预先计算 cumsum 等。

library(tidyverse)

test_data <- data.frame(category = c("retail","retail","retail","retail","retail","hotel"),
                        sub_category=c("retail_sub1","retail_sub2","retail_sub3","retail_sub4","retail_sub5","hotel"),
                        amount=c(51083.26,27454.13,22495.89,21195.05,16863.69,60210.6))

barchart <- test_data %>%
  # Reshape data
  pivot_longer(-amount, names_to = "level", values_to = "name") %>%
  # Filter out the hotel subcategory, leaving in the (super)category
  filter(!(level == "sub_category" & name == "hotel")) %>%
  # Sum over category level and names
  group_by(level, name) %>%
  summarise(amount = sum(amount), .groups = "keep") %>%
  # Regular stacked bar chart code
  ggplot(aes(x = level, y = amount)) +
  geom_col(aes(fill = name), width = 1) +
  geom_text(
    aes(label = paste0(name, "\n", amount), # <--- point 2
        group = interaction(name, level)),
    position = position_stack(vjust = 0.5),
  )
barchart

随后,添加coord_polar()将在圆环图中制作条形图。

barchart + coord_polar(theta = "y")

reprex 包于 2021-10-26 创建(v2.0.1)

  1. 如何在标签上添加“金额”?

您可以将其设置label = paste0(name, "\n", amount)为美学(参见代码)。

  1. “填充颜色”很难看。

查看scale_fill_*()离散调色板的函数系列。

  1. 边界不是很光滑。

这取决于图形设备的抗锯齿。在 RStudio 中,您可以设置“工具 > 全局选项 > 常规 > 图形 > 后端 > 选择 AGG”(或 Cairo)。例如,为了保存绘图,您可以使用ragg::agg_png().


推荐阅读