r - 如何更改ggplot2中geom_rects图例的顺序?
问题描述
我正在尝试使用包中的geom_rect()
几何图形绘制图ggplot2
。
这是我的 MWE:
library(tidyverse)
groups = tribble(
~size, ~income,
10, 400,
60, 500,
20, 600
)
heights = c("Height <= 60 Inches",
"Height 61-70 Inches",
"Height >= 71 Inches")
g = ggplot()
g = g + geom_rect(aes(xmin = 0,
ymin = 0,
xmax = groups$size[1],
ymax = groups$income[1],
fill = heights[1]))
g = g + geom_rect(aes(xmin = groups$size[1],
ymin = 0,
xmax = sum(groups$size[1:2]),
ymax = groups$income[2],
fill = heights[2]))
g = g + geom_rect(aes(xmin = sum(groups$size[1:2]),
ymin = 0,
xmax = sum(groups$size[1:3]),
ymax = groups$income[3],
fill = heights[3]))
g = g + labs(fill = 'People Groups')
我对在绘图上绘制矩形的方式感到满意,但是,我对填充顺序如何混乱感到不满。
如何将填充顺序更改为:“高度 <= 60 英寸”、“高度 61-70 英寸”、“高度 >= 71 英寸”?
解决方案
推荐阅读
- c - 格式 %c 需要 char* 类型的参数,但具有 int
- gitlab - 集成时抛出 GitLab Webhook 错误
- android - 将 BottomNavigation 与 State 和构建参数一起使用
- c++ - 如何使用依赖 glfw、GLEW 和 GL 制作 Dear ImGui 的 config.cmake 文件
- r - 如何将这个晦涩的字符向量格式化为 R 中的日期时间(或 POSIX)对象?
- reactjs - React:从单独的输入状态动态更新字段值
- javascript - 使用部分视图内的输入文本框的 OnChange 刷新部分视图(C# .Net Core)
- r - 如何从二项分布中计算两组向量之间的差异数?
- python-extensions - (python)打印一直说“无”
- java - Java如何使用超类构造函数设置子类实例变量