简体   繁体   English

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

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

In R ggplot , using 'geom_rect' can generate below plot.在 R ggplot 中,使用 'geom_rect' 可以生成下面的图。 Now, I have a few questions :现在,我有几个问题:

  1. Is there any way to simplify current code ?有什么办法可以简化当前的代码吗?
  2. How to add 'amount' to the label (currently, it's only include 'category/sub_category')如何在标签中添加“数量”(目前仅包含“类别/子类别”)
  3. The 'fill color' is ugly , especially for the 'sub_category' (maybe using same colors ,just change the 'alpha' parameter) “填充颜色”很难看,尤其是“sub_category”(可能使用相同的颜色,只需更改“alpha”参数)
  4. the boarder isn't very smooth (jagged border)边界不是很平滑(锯齿状边框)

Anyone can help ?任何人都可以帮忙吗? Thanks!谢谢!

   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()

在此处输入图片说明

In the future, please ask 1 question per post.以后,请在每个帖子中提出 1 个问题。

To answer your questions point by point.逐条回答您的问题。

  1. Can this code be simplified?这段代码可以简化吗?

Yes, you essentially have a stacked bar chart, so if we want 1 label per area we need to pre-summarise the data, but needn't bother with precalculating cumsums and such.是的,您本质上有一个堆积条形图,所以如果我们想要每个区域 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

Subsequently, adding coord_polar() will make the barchart in the donut chart.随后,添加coord_polar()将在圆环图中制作条形图。

barchart + coord_polar(theta = "y")

Created on 2021-10-26 by the reprex package (v2.0.1)reprex 包(v2.0.1) 于 2021 年 10 月 26 日创建

  1. How to add 'amount' to the label?如何在标签上添加“金额”?

You can just set label = paste0(name, "\\n", amount) as an aesthetic (see code).您可以将label = paste0(name, "\\n", amount)为美学(请参阅代码)。

  1. The 'fill color' is ugly. “填充颜色”很难看。

Look into the scale_fill_*() family of functions for discrete palettes.查看用于离散调色板的scale_fill_*()系列函数。

  1. The border isn't very smooth.边界不是很平滑。

That depends on the anti-aliasing for your graphics device.这取决于您的图形设备的抗锯齿。 In RStudio you can set 'Tools > Global Options > General > Graphics > Backend > Select AGG' (or Cairo).在 RStudio 中,您可以设置“工具 > 全局选项 > 常规 > 图形 > 后端 > 选择 AGG”(或开罗)。 For saving plots you could for example use ragg::agg_png() .例如,为了保存绘图,您可以使用ragg::agg_png()

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

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