繁体   English   中英

在 R ggplot 中,关于 'geom_rect' 极坐标的几个问题

[英]In R ggplot , a few questions about 'geom_rect' with polar coordinates

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

  1. 有什么办法可以简化当前的代码吗?
  2. 如何在标签中添加“数量”(目前仅包含“类别/子类别”)
  3. “填充颜色”很难看,尤其是“sub_category”(可能使用相同的颜色,只需更改“alpha”参数)
  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()

在此处输入图片说明

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

逐条回答您的问题。

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

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

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 包(v2.0.1) 于 2021 年 10 月 26 日创建

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

您可以将label = paste0(name, "\\n", amount)为美学(请参阅代码)。

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

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

  1. 边界不是很平滑。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM