我有以下图表,我正在尝试编辑图例,以便Description
下的条目反转颜色。例如,BGM
条目将黑色背景切换为橙色,橙色短划线切换为黑色短划线。我想更改图例,以便图表颜色更容易区分,不一定必须是我提到的方式。
我的代码:
visit1 <- toString(carb1[1,3])
id1 <- toString(carb1[1,1])
date1<- toString(carb1[1,4])
carb1$Amount = as.numeric(as.character(carb1$Amount))
carb1$time = as.POSIXct(carb1$Start, format = "%H:%M", tz = "GMT")
q <- ggplot(data=carb1, aes(x=time, y=Amount, group=Description, color= Description, shape=Type, label=Amount )) + geom_text(data=carb1[carb1$Type=="Insulin (units)", ], size=3, hjust=0, vjust=2, color="black") +
geom_bar(data=carb1[carb1$Type=="Carb (grams)", ], stat="identity", width=.1) +
geom_point(data=carb1[carb1$Description != "Disconnect or Reconnect Time", ], size=4) +
theme(text = element_text(size=10), axis.text.x = element_text(angle=90, vjust=1)) +
scale_y_continuous(breaks = round(seq(0, max(carb1$Amount), by = 50),2)) +
xlab("Time (HH:MM)") + ylab("Glucose (mg/dL), Carbohydrates (grams)") +
ggtitle(toString(c("Blood Glucose Measurements \n with Carbohydrate and Insulin Administration"))) + geom_bar(data=carb1[carb1$Type=="Disconnect or Reconnect Time", ], width=.1, stat="identity") +
scale_x_datetime(breaks=date_breaks("1 hour"), labels= date_format("%H:%M")) +
theme_bw() +
scale_shape_manual(values=c(16,17,15,18)) +
theme(plot.margin = unit(c(0,0,0,0), "cm"))
q
grid.text((paste("Patient ID:", id1, sep = " ")),
x = unit(.929, "npc"), y = unit(.89, "npc"), just = c("right", "top"),
gp = gpar(fontface = "bold", fontsize = 12, col = "black"))
grid.text((paste("Visit:", visit1, sep = " ")),
x = unit(.813, "npc"), y = unit(.85, "npc"), just = c("right", "top"),
gp = gpar(fontface = "bold", fontsize = 12, col = "black"))
grid.text((paste("Date:", date1, sep = " ")),
x = unit(.87 , "npc"), y = unit(.81, "npc"), just = c("right", "top"),
gp = gpar(fontface = "bold", fontsize = 12, col = "black"))
dev.off()
我的数据:
> dput(carb1)
structure(list(EDC.ID. = structure(c(3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Subject 01", "Subject 02",
"Subject 03", "Subject 04", "Subject 05", "Subject 06", "Subject 07",
"Subject 08", "Subject 09", "Subject 10", "Subject 11", "Subject 12",
"Subject 13", "Subject 14", "Subject 15", "Subject 16", "Subject 17",
"Subject 18", "Subject 19", "Subject 20"), class = "factor"),
Subject.ID.. = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), Visit = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), Date = structure(c(9L, 9L, 9L, 9L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("-", "08/14/14",
"08/18/14", "08/19/14", "08/20/14", "08/21/14", "08/25/14",
"08/27/14", "08/28/14", "09/01/14", "09/02/14", "09/03/14",
"09/04/14", "09/05/14", "09/08/14", "09/09/14", "09/10/14",
"09/12/14", "09/15/14", "09/16/14", "09/18/14", "09/19/14",
"09/22/14", "09/23/14", "09/24/14", "09/25/14", "09/26/14",
"09/29/14", "09/30/14", "10/01/14", "10/03/14", "10/07/14"
), class = "factor"), Start = structure(c(216L, 114L, 361L,
420L, 492L, 513L, 526L, 540L, 11L, 61L, 76L, 109L, 123L,
182L, 202L, 274L, 302L, 331L, 361L, 391L, 420L, 445L, 71L,
200L, 290L, 190L, 261L, 457L), .Label = c("10:00", "10:01",
"10:02", "10:03", "10:04", "10:05", "10:06", "10:07", "10:08",
"10:09", "10:10", "10:11", "10:12", "10:13", "10:14", "10:15",
"10:16", "10:17", "10:18", "10:19", "10:20", "10:21", "10:22",
"10:23", "10:24", "10:25", "10:26", "10:27", "10:28", "10:29",
"10:30", "10:31", "10:32", "10:33", "10:34", "10:35", "10:36",
"10:37", "10:38", "10:39", "10:40", "10:41", "10:42", "10:43",
"10:44", "10:45", "10:46", "10:47", "10:48", "10:49", "10:50",
"10:51", "10:52", "10:53", "10:54", "10:55", "10:56", "10:57",
"10:58", "10:59", "11:00", "11:01", "11:02", "11:03", "11:04",
"11:05", "11:06", "11:07", "11:08", "11:09", "11:10", "11:11",
"11:12", "11:13", "11:14", "11:15", "11:16", "11:17", "11:18",
"11:19", "11:20", "11:21", "11:22", "11:23", "11:24", "11:25",
"11:26", "11:27", "11:28", "11:29", "11:30", "11:31", "11:33",
"11:34", "11:35", "11:36", "11:37", "11:38", "11:39", "11:40",
"11:41", "11:42", "11:43", "11:44", "11:45", "11:46", "11:47",
"11:48", "11:49", "11:50", "11:51", "11:52", "11:53", "11:54",
"11:55", "11:56", "11:57", "11:58", "11:59", "12:00", "12:01",
"12:03", "12:04", "12:05", "12:06", "12:07", "12:08", "12:09",
"12:10", "12:11", "12:12", "12:13", "12:14", "12:15", "12:16",
"12:18", "12:19", "12:20", "12:21", "12:22", "12:23", "12:24",
"12:25", "12:26", "12:27", "12:28", "12:29", "12:30", "12:31",
"12:32", "12:33", "12:34", "12:35", "12:36", "12:37", "12:38",
"12:39", "12:40", "12:41", "12:42", "12:43", "12:44", "12:45",
"12:46", "12:47", "12:48", "12:49", "12:50", "12:51", "12:52",
"12:53", "12:54", "12:55", "12:56", "12:57", "12:58", "12:59",
"13:00", "13:01", "13:03", "13:04", "13:05", "13:06", "13:07",
"13:08", "13:09", "13:10", "13:11", "13:12", "13:13", "13:14",
"13:15", "13:16", "13:17", "13:18", "13:19", "13:20", "13:21",
"13:22", "13:23", "13:24", "13:25", "13:27", "13:28", "13:29",
"13:30", "13:31", "13:32", "13:33", "13:34", "13:35", "13:36",
"13:37", "13:38", "13:39", "13:40", "13:41", "13:42", "13:43",
"13:44", "13:45", "13:46", "13:47", "13:48", "13:49", "13:50",
"13:51", "13:52", "13:53", "13:54", "13:55", "13:56", "13:57",
"13:58", "13:59", "14:00", "14:01", "14:02", "14:03", "14:04",
"14:05", "14:06", "14:08", "14:09", "14:10", "14:11", "14:12",
"14:13", "14:14", "14:15", "14:16", "14:17", "14:18", "14:19",
"14:20", "14:21", "14:22", "14:23", "14:24", "14:25", "14:26",
"14:27", "14:28", "14:29", "14:30", "14:31", "14:33", "14:34",
"14:35", "14:36", "14:37", "14:38", "14:39", "14:40", "14:41",
"14:42", "14:43", "14:44", "14:45", "14:46", "14:48", "14:49",
"14:50", "14:51", "14:52", "14:53", "14:54", "14:55", "14:57",
"14:58", "14:59", "15:00", "15:01", "15:02", "15:03", "15:04",
"15:05", "15:06", "15:07", "15:08", "15:09", "15:10", "15:12",
"15:13", "15:14", "15:15", "15:16", "15:17", "15:18", "15:19",
"15:20", "15:21", "15:22", "15:23", "15:24", "15:25", "15:26",
"15:27", "15:28", "15:29", "15:30", "15:31", "15:32", "15:33",
"15:34", "15:35", "15:36", "15:37", "15:38", "15:39", "15:40",
"15:41", "15:42", "15:43", "15:44", "15:45", "15:46", "15:47",
"15:48", "15:49", "15:50", "15:51", "15:52", "15:53", "15:54",
"15:55", "15:56", "15:57", "15:58", "15:59", "16:00", "16:01",
"16:02", "16:03", "16:04", "16:05", "16:06", "16:07", "16:08",
"16:09", "16:10", "16:11", "16:12", "16:13", "16:14", "16:15",
"16:16", "16:17", "16:18", "16:19", "16:20", "16:21", "16:22",
"16:23", "16:24", "16:25", "16:26", "16:27", "16:28", "16:29",
"16:30", "16:31", "16:32", "16:33", "16:34", "16:35", "16:36",
"16:37", "16:38", "16:39", "16:40", "16:42", "16:43", "16:44",
"16:45", "16:46", "16:47", "16:48", "16:49", "16:50", "16:51",
"16:52", "16:53", "16:54", "16:55", "16:56", "16:57", "16:58",
"16:59", "17:00", "17:01", "17:02", "17:03", "17:04", "17:05",
"17:06", "17:07", "17:08", "17:09", "17:10", "17:11", "17:12",
"17:14", "17:15", "17:17", "17:18", "17:19", "17:20", "17:21",
"17:22", "17:23", "17:24", "17:25", "17:26", "17:28", "17:29",
"17:30", "17:31", "17:32", "17:34", "17:35", "17:36", "17:38",
"17:39", "17:40", "17:41", "17:43", "17:44", "17:45", "17:50",
"17:51", "17:52", "17:54", "17:58", "18:03", "18:09", "18:11",
"18:15", "18:27", "7:51", "8:00", "8:04", "8:05", "8:10",
"8:14", "8:15", "8:18", "8:26", "8:29", "8:31", "8:33", "8:37",
"8:39", "8:40", "8:41", "8:43", "8:44", "8:45", "8:47", "8:48",
"8:50", "8:51", "8:52", "8:54", "8:55", "8:56", "8:57", "8:58",
"8:59", "9:00", "9:01", "9:02", "9:03", "9:04", "9:05", "9:06",
"9:07", "9:08", "9:09", "9:11", "9:12", "9:13", "9:14", "9:15",
"9:17", "9:18", "9:19", "9:20", "9:21", "9:22", "9:23", "9:24",
"9:25", "9:27", "9:28", "9:29", "9:30", "9:32", "9:33", "9:34",
"9:35", "9:36", "9:37", "9:38", "9:39", "9:40", "9:42", "9:43",
"9:44", "9:45", "9:46", "9:47", "9:48", "9:49", "9:50", "9:51",
"9:52", "9:53", "9:54", "9:55", "9:56", "9:57", "9:58", "9:59"
), class = "factor"), End = structure(c(237L, 112L, 356L,
415L, 477L, 498L, 510L, 524L, 13L, 61L, 74L, 107L, 121L,
180L, 200L, 269L, 297L, 326L, 356L, 386L, 415L, 438L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("---", "0:16", "10:00", "10:01",
"10:02", "10:03", "10:04", "10:05", "10:06", "10:07", "10:08",
"10:09", "10:10", "10:11", "10:12", "10:13", "10:14", "10:15",
"10:16", "10:17", "10:18", "10:19", "10:20", "10:21", "10:22",
"10:23", "10:24", "10:25", "10:27", "10:28", "10:29", "10:30",
"10:31", "10:32", "10:33", "10:34", "10:35", "10:36", "10:37",
"10:38", "10:39", "10:40", "10:41", "10:42", "10:43", "10:44",
"10:45", "10:46", "10:47", "10:48", "10:49", "10:50", "10:51",
"10:52", "10:53", "10:54", "10:55", "10:57", "10:58", "10:59",
"11:00", "11:01", "11:03", "11:04", "11:05", "11:06", "11:07",
"11:08", "11:09", "11:10", "11:12", "11:13", "11:14", "11:15",
"11:16", "11:17", "11:18", "11:19", "11:20", "11:21", "11:22",
"11:23", "11:24", "11:25", "11:26", "11:27", "11:28", "11:29",
"11:30", "11:31", "11:33", "11:34", "11:35", "11:36", "11:37",
"11:38", "11:39", "11:40", "11:41", "11:42", "11:43", "11:44",
"11:45", "11:46", "11:47", "11:48", "11:49", "11:50", "11:51",
"11:52", "11:53", "11:54", "11:55", "11:56", "11:57", "11:58",
"11:59", "12:00", "12:01", "12:03", "12:04", "12:05", "12:06",
"12:07", "12:08", "12:09", "12:10", "12:11", "12:12", "12:13",
"12:14", "12:15", "12:16", "12:18", "12:19", "12:20", "12:21",
"12:22", "12:23", "12:24", "12:25", "12:26", "12:27", "12:28",
"12:29", "12:30", "12:31", "12:32", "12:33", "12:34", "12:35",
"12:36", "12:37", "12:38", "12:39", "12:40", "12:41", "12:42",
"12:43", "12:44", "12:45", "12:46", "12:47", "12:48", "12:49",
"12:50", "12:51", "12:52", "12:53", "12:54", "12:55", "12:56",
"12:57", "12:58", "12:59", "13:00", "13:01", "13:03", "13:04",
"13:05", "13:06", "13:07", "13:08", "13:09", "13:10", "13:11",
"13:12", "13:13", "13:14", "13:15", "13:16", "13:17", "13:18",
"13:19", "13:20", "13:21", "13:22", "13:23", "13:24", "13:25",
"13:27", "13:28", "13:29", "13:30", "13:31", "13:32", "13:33",
"13:34", "13:35", "13:36", "13:37", "13:38", "13:39", "13:40",
"13:41", "13:42", "13:44", "13:45", "13:46", "13:48", "13:49",
"13:50", "13:51", "13:52", "13:53", "13:54", "13:55", "13:56",
"13:57", "13:58", "13:59", "14:00", "14:01", "14:02", "14:03",
"14:04", "14:05", "14:06", "14:08", "14:09", "14:10", "14:11",
"14:12", "14:14", "14:15", "14:16", "14:17", "14:18", "14:19",
"14:20", "14:21", "14:22", "14:23", "14:24", "14:25", "14:26",
"14:27", "14:28", "14:29", "14:30", "14:31", "14:33", "14:34",
"14:35", "14:36", "14:37", "14:38", "14:39", "14:40", "14:41",
"14:42", "14:43", "14:44", "14:45", "14:46", "14:48", "14:49",
"14:50", "14:51", "14:52", "14:53", "14:54", "14:55", "14:57",
"14:58", "14:59", "15:00", "15:01", "15:02", "15:03", "15:04",
"15:05", "15:06", "15:07", "15:08", "15:09", "15:10", "15:12",
"15:13", "15:14", "15:15", "15:16", "15:17", "15:18", "15:19",
"15:20", "15:21", "15:22", "15:23", "15:24", "15:25", "15:26",
"15:27", "15:28", "15:29", "15:30", "15:31", "15:32", "15:33",
"15:34", "15:35", "15:36", "15:37", "15:38", "15:39", "15:40",
"15:41", "15:42", "15:43", "15:44", "15:45", "15:46", "15:47",
"15:48", "15:49", "15:50", "15:51", "15:52", "15:53", "15:54",
"15:55", "15:56", "15:57", "15:58", "15:59", "16:00", "16:01",
"16:02", "16:03", "16:04", "16:05", "16:06", "16:07", "16:08",
"16:09", "16:10", "16:11", "16:12", "16:13", "16:14", "16:15",
"16:16", "16:17", "16:18", "16:19", "16:20", "16:21", "16:22",
"16:23", "16:24", "16:25", "16:26", "16:27", "16:28", "16:29",
"16:30", "16:31", "16:32", "16:33", "16:34", "16:35", "16:36",
"16:37", "16:38", "16:39", "16:40", "16:42", "16:43", "16:44",
"16:45", "16:46", "16:47", "16:48", "16:49", "16:50", "16:51",
"16:52", "16:53", "16:54", "16:55", "16:56", "16:57", "16:58",
"16:59", "17:00", "17:01", "17:02", "17:03", "17:04", "17:05",
"17:06", "17:07", "17:08", "17:09", "17:10", "17:11", "17:12",
"17:14", "17:15", "17:17", "17:18", "17:19", "17:20", "17:21",
"17:22", "17:23", "17:24", "17:25", "17:26", "17:29", "17:30",
"17:32", "17:34", "17:35", "17:36", "17:38", "17:39", "17:40",
"17:45", "17:50", "17:51", "17:54", "18:00", "18:09", "18:15",
"7:51", "8:00", "8:04", "8:05", "8:10", "8:14", "8:15", "8:18",
"8:26", "8:29", "8:31", "8:33", "8:37", "8:39", "8:40", "8:41",
"8:43", "8:44", "8:45", "8:47", "8:48", "8:50", "8:51", "8:52",
"8:54", "8:55", "8:57", "8:58", "8:59", "9:00", "9:01", "9:02",
"9:03", "9:04", "9:05", "9:06", "9:07", "9:08", "9:09", "9:11",
"9:12", "9:13", "9:14", "9:15", "9:17", "9:18", "9:19", "9:20",
"9:21", "9:22", "9:23", "9:24", "9:25", "9:27", "9:28", "9:30",
"9:32", "9:33", "9:34", "9:35", "9:36", "9:37", "9:38", "9:39",
"9:40", "9:42", "9:43", "9:44", "9:45", "9:46", "9:47", "9:48",
"9:49", "9:50", "9:51", "9:52", "9:53", "9:54", "9:55", "9:56",
"9:57", "9:58", "9:59"), class = "factor"), Description = structure(c(5L,
6L, 6L, 6L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L,
4L), .Label = c("BGM", "Dextrose 50% (mL)", "dextrose Tabs",
"Disconnect or Reconnect Time", "Food or Drink", "Humalog (units)",
"Humalog sc (units)", "Humalog SC (units)", "Injection (ccs)",
"IV Dextrose(mL)", "Lantus (units)", "Levemir (units)", "Novolin R (units)",
"Novolin R IV (units)", "Oral", "Other", "YSI"), class = "factor"),
Amount = c(127, 8, 8, 4, 161.5, 150.5, 150, 144.5, 138, 122,
119, 114.5, 110, 57.95, 53.95, 102, 156, 217, 250, 273, 275,
277, 350, 350, 350, 350, 350, 350), Type = structure(c(2L,
4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("BG Data (mg/dL)",
"Carb (grams)", "Disconnect or Reconnect Time", "Insulin (units)"
), class = "factor"), Order = c(1L, 125L, 126L, 127L, 461L,
462L, 463L, 464L, 465L, 466L, 467L, 468L, 469L, 470L, 471L,
472L, 473L, 474L, 475L, 476L, 477L, 478L, 1881L, 1882L, 1883L,
1881L, 1882L, 1883L), time = structure(c(1430487600, 1430481240,
1430496600, 1430500200, 1430470920, 1430472300, 1430473200,
1430474100, 1430475000, 1430478000, 1430478900, 1430480940,
1430481840, 1430485500, 1430486700, 1430491200, 1430493000,
1430494800, 1430496600, 1430498400, 1430500200, 1430502000,
1430478600, 1430486580, 1430492280, 1430485980, 1430490360,
1430503860), class = c("POSIXct", "POSIXt"), tzone = "GMT")), .Names = c("EDC.ID.",
"Subject.ID..", "Visit", "Date", "Start", "End", "Description",
"Amount", "Type", "Order", "time"), row.names = c(1L, 125L, 126L,
127L, 461L, 462L, 463L, 464L, 465L, 466L, 467L, 468L, 469L, 470L,
471L, 472L, 473L, 474L, 475L, 476L, 477L, 478L, 1881L, 1882L,
1883L, 2045L, 2046L, 2047L), class = "data.frame")
答案 0 :(得分:0)
刚想出来,但也可以增加&#34;破折号的大小。因此,以下内容更加明显:guides(color = guide_legend(override.aes = list(size = 1.5)))
代码:
q <- ggplot(data=carb1, aes(x=time, y=Amount, group=Description, color= Description, shape=Type, label=Amount )) + geom_text(data=carb1[carb1$Type=="Insulin (units)", ], size=3, hjust=0, vjust=2, color="black") +
geom_bar(data=carb1[carb1$Type=="Carb (grams)", ], stat="identity", width=.1) +
geom_point(data=carb1[carb1$Description != "Disconnect or Reconnect Time", ], size=4) +
theme(text = element_text(size=10), axis.text.x = element_text(angle=90, vjust=1)) +
scale_y_continuous(breaks = round(seq(0, max(carb1$Amount), by = 50),2)) +
xlab("Time (HH:MM)") + ylab("Glucose (mg/dL), Carbohydrates (grams)") +
ggtitle(toString(c("Blood Glucose Measurements \n with Carbohydrate and Insulin Administration"))) + geom_bar(data=carb1[carb1$Type=="Disconnect or Reconnect Time", ], width=.1, stat="identity") +
scale_x_datetime(breaks=date_breaks("1 hour"), labels= date_format("%H:%M")) +
theme_bw() +
scale_shape_manual(values=c(16,17,15,18)) +
theme(plot.margin = unit(c(0,0,0,0), "cm")) +
guides(color = guide_legend(override.aes = list(size = 1.5)))
导致图形如下所示: