r - 在 R ggplot 中,关于带有极坐标的“geom_rect”的几个问题
问题描述
在 R ggplot 中,使用 'geom_rect' 可以生成下图。现在,我有几个问题:</p>
- 有什么方法可以简化当前代码?
- 如何在标签中添加“金额”(目前仅包含“类别/子类别”)
- “填充颜色”很难看,特别是对于“子类别”(可能使用相同的颜色,只需更改“阿尔法”参数)
- 边界不是很光滑(锯齿状边界)
任何人都可以帮忙吗?谢谢!
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 个标签,我们需要预先汇总数据,但不必费心预先计算 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)
- 如何在标签上添加“金额”?
您可以将其设置label = paste0(name, "\n", amount)
为美学(参见代码)。
- “填充颜色”很难看。
查看scale_fill_*()
离散调色板的函数系列。
- 边界不是很光滑。
这取决于图形设备的抗锯齿。在 RStudio 中,您可以设置“工具 > 全局选项 > 常规 > 图形 > 后端 > 选择 AGG”(或 Cairo)。例如,为了保存绘图,您可以使用ragg::agg_png()
.
推荐阅读
- c - 单链表输出不希望的零
- javascript - 如何在具有 document.write 的 DIV 元素中异步加载 js 内容?
- python - AttributeError:模块“tensorflow”没有属性“random_shuffle”
- c++ - 如何在 Windows Composer 中集成 Qt 无框窗口?(系统快捷方式不起作用)
- jsonschema - json 模式 04 中的 anyOf 具有对象和数组
- java - 如何使用hibernate创建具有现有对象的复合键对象
- android - 单击的按钮显示文本和图像
- firebase - 如何在flutter中更新firebase中的集合文档?
- algorithm - 断开节点与图的连接
- jquery - Microsoft Edge PDF 滚动和突出显示问题