r - 为什么我的 ggplot2 图例在这个 R 程序中重叠?
问题描述
我的数据是:
x y z a b c d e
1 1 0 0 7 0 7 0 0
2 2 0 0 7 7 14 7 0
3 3 0 0 7 14 21 14 0
4 4 0 0 7 21 28 21 0
5 5 0 0 7 28 35 28 0
6 6 0 0 7 35 42 35 0
7 7 0 0 7 42 49 42 0
8 8 0 0 7 49 56 49 0
9 9 0 0 7 56 63 56 0
10 10 0 0 7 63 70 63 0
11 11 0 0 7 70 77 70 0
12 12 0 0 7 77 84 77 0
13 13 0 0 7 84 91 84 0
14 14 0 0 7 91 98 91 0
15 15 0 0 7 98 105 98 0
我的代码是:
ggplot(data=df, aes(x))+
geom_step(aes(y=d))+
geom_point(aes(y=e, color="Blue"),shape=1, size=4)+
geom_step(aes(y=e))+
geom_point(aes(y=d,color="Red"),size=2)+
theme(axis.text.x = element_text( vjust = 0.5),
legend.position = "bottom",
axis.line = element_line(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank())+
scale_x_continuous("Patient number", labels = x, breaks = x)+
ylab("time")+
scale_color_identity(name="Time", breaks = c("Blue", "Red"),
labels = c("Wait time", "Idle time"),
guide = "legend")
我正在使用上面的代码,我的图例是重叠的。我该如何纠正?
解决方案
尝试这种方法,使用重塑数据比逐个添加几何更好,因为修改元素可能很麻烦。这里的代码:
library(ggplot2)
library(dplyr)
library(tidyr)
#Code
df %>% select(c(x,d,e)) %>% pivot_longer(-x) %>%
ggplot(aes(x=x,y=value, color=name,shape=name,group=name,fill=name))+
geom_step(color='black',show.legend = F)+
geom_point()+
theme(axis.text.x = element_text( vjust = 0.5),
legend.position = "bottom",
axis.line = element_line(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank())+
ylab("time")+
scale_color_manual('Time',values=c('red','blue'),
labels = c("Wait time", "Idle time"))+
scale_shape_manual('Time',values=c(21,21),
labels = c("Wait time", "Idle time"))+
scale_fill_manual('Time',values=c('transparent','blue'),
labels = c("Wait time", "Idle time"))
输出:
使用的一些数据:
#Data
df <- structure(list(x = 1:15, y = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), z = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), a = c(7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), b = c(0L, 7L, 14L, 21L,
28L, 35L, 42L, 49L, 56L, 63L, 70L, 77L, 84L, 91L, 98L), c = c(7L,
14L, 21L, 28L, 35L, 42L, 49L, 56L, 63L, 70L, 77L, 84L, 91L, 98L,
105L), d = c(0L, 7L, 14L, 21L, 28L, 35L, 42L, 49L, 56L, 63L,
70L, 77L, 84L, 91L, 98L), e = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15"))
此外,帖子中不存在某些元素breaks
,因此我省略了,但您可以在绘制原始情节时添加。
推荐阅读
- angular - 当 HttpStatus 为 202 时使用拦截器重定向和取消请求
- ms-access - 访问:从查询的基础表中显示表单字段
- typescript - TypeORM:使用 QueryBuilder 选择具有嵌套关系的 RANDOM()
- android - Kotlin:如何对数组 N x 2 或 2 连接数组进行排序?
- ios - 使用正则表达式解决方法的 Firebase Cloud Firestore 查询
- asp.net-core - 从 HttpTrigger Azure 函数获取 ActionContext
- javascript - 正常运行时,chrome.runtime.sendMessage 在第一次单击时不起作用。它在调试时工作
- java - 如何修复我的代码以在数组中查找某个字符并更改该数组
- python - 如何将特定列表序列与 dictreader 行匹配?
- javascript - 如何使用 PayPal v2 和 JavaScript 检测付款?