首页 > 解决方案 > 使用 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 输出

我不知道如何更改图层的顺序。也许我错过了关于ggplot2and data.frames 的一些非常重要的东西。

标签: rggplot2

解决方案


默认情况下,名称按字母顺序排列。您可以通过制作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 日创建


推荐阅读