r - 使用 ggplot2 重新创建圆形图
问题描述
我正在尝试使用 R 和 重新创建以下插图ggplot2
:
我创建了以下代码:
library(ggplot2)
df <- data.frame(names = c(
"Enterprise Business Rules",
"ApplicationBusiness Rules",
"Interface Adapters",
"Frameworks & Drivers"))
ggplot(df, aes(x = factor(1), fill = names)) +
geom_bar(width = 1) +
coord_polar() +
xlab("") + ylab("") +
theme_void() +
theme(legend.title = element_blank())
但输出不正确:
我不知道如何更改图层的顺序。也许我错过了关于ggplot2
and data.frame
s 的一些非常重要的东西。
解决方案
默认情况下,名称按字母顺序排列。您可以通过制作names
一个因素并使用levels
参数指定顺序来解决这个问题。reverse = TRUE
另外,您可以使用函数的参数反转图例中的名称顺序guide_legend
。
library(ggplot2)
names <- rev(c(
"Enterprise Business Rules",
"ApplicationBusiness Rules",
"Interface Adapters",
"Frameworks & Drivers"))
df <- data.frame(names = factor(names, levels = names))
ggplot(df, aes(x = factor(1), fill = names)) +
geom_bar(width = 1) +
coord_polar() +
xlab("") + ylab("") +
theme_void() +
theme(legend.title = element_blank()) +
guides(fill = guide_legend(reverse = TRUE))
由reprex 包(v0.2.1)于 2019 年 4 月 4 日创建
推荐阅读
- asp.net-core - 是否有任何支持几何查询的带有 EF Core 的 ASP.NET Core OData API 示例?
- jenkins - 使用凭证将声纳添加到 Maven Jenkins 作业失败
- excel - 如何标记具有重复名称且条件值小于 20 的项目?
- odoo-12 - 从“Leaves Second Approve”编辑通知内容
- javascript - Wordpress 似乎不接受 css 音乐播放器的 @keyframes
- reactjs - Semantic UI React 下拉菜单未设置值 onChange: React+ Typescript
- jquery - 如何在 Jquery 中根据 td 值对 tr 进行排序
- mysql - 如何使用 Navicat 或 MySQL Workbench 将 MySQL 与 Docker 连接?
- r - R - cowplot:使用 plot_grid 排列 png 图像和 ggplot
- python - 在调用不同文件的函数中,有多少单词以某个字符结尾?