r - Combining shape and color legends into one (ggplot) -
i'm trying combine shape , color legends 1 graph looks cleaner. example, bg data (mg/dl)
filled red, cgm retropective value sensor 1 (mg/dl) filled withorange, etc.. tried working scale_color_manual
, scale_shape_manual
can't seem combine them.
here code:
#load libabries needed create graphs library(ggplot2) library(dplyr) library(scales) library(gridextra) library(grid) #create dataset subject data1<-subset(carb_data,subject.id.. == 1) carb1<-subset(data1,visit == 1) #makes sure "amount" stored number , "time" stored date 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=type, color= type, shape=type, label=amount)) + geom_point(data=carb1[carb1$type=="bg data (mg/dl)", ], size=2, colour="red") + geom_point(data=carb1[carb1$type == "cgm retropective value sensor 1 (mg/dl)", ], size=4) + geom_point(data=carb1[carb1$type == "cgm retropective value sensor 2 (mg/dl)", ], size=4) + geom_point(data=carb1[carb1$type == "cgm retropective value sensor 3 (mg/dl)", ], size=4) + geom_point(data=carb1[carb1$type == "cgm retropective value sensor 4 (mg/dl)", ], 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), = 50),2)) + xlab("time (hh:mm)") + ylab("glucose (mg/dl), carbohydrates (grams)") + ggtitle(tostring(c("cgm retrospectve , reference values"))) + 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,1,2,3,5,6)) print(q)
and data:
> dput(carb1) structure(list(edc.id. = structure(c(14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l, 14l), .label = c("subject1", "subject10", "subject11", "subject12", "subject13", "subject14", "subject15", "subject16", "subject17", "subject18", "subject19", "subject2", "subject20", "subject3", "subject4", "subject5", "subject6", "subject7", "subject8", "subject9"), 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, 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, 1l, 1l), date = structure(c(11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l, 11l), .label = c("10/1/14", "10/3/14", "10/7/14", "8/14/14", "8/18/14", "8/19/14", "8/20/14", "8/21/14", "8/25/14", "8/27/14", "8/28/14", "9/1/14", "9/10/14", "9/12/14", "9/15/14", "9/16/14", "9/18/14", "9/19/14", "9/2/14", "9/22/14", "9/23/14", "9/24/14", "9/25/14", "9/26/14", "9/29/14", "9/3/14", "9/30/14", "9/4/14", "9/5/14", "9/8/14", "9/9/14"), class = "factor"), start = structure(c(475l, 496l, 508l, 522l, 13l, 59l, 74l, 106l, 120l, 177l, 197l, 266l, 294l, 323l, 353l, 383l, 412l, 434l, 69l, 195l, 282l, 323l, 353l, 383l, 412l, 434l, 353l, 383l, 412l, 434l), .label = c("0:16", "1:25", "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: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:33", "10:34", "10:35", "10:36", "10:37", "10:38", "10:39", "10:40", "10:42", "10:43", "10:44", "10:45", "10:46", "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: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: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: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:26", "17:29", "17:30", "17:32", "17:34", "17:35", "17:36", "17:38", "17:39", "17:40", "17:50", "17:51", "17:54", "18:09", "2:40", "21:24", "21:27", "3:48", "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: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(484l, 505l, 516l, 530l, 12l, 58l, 72l, 104l, 118l, 176l, 196l, 266l, 294l, 323l, 353l, 383l, 412l, 436l, 184l, 253l, 448l, 323l, 353l, 383l, 412l, 436l, 353l, 383l, 412l, 436l), .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: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:42", "10:43", "10:44", "10:45", "10:46", "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: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: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: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:42", "13:43", "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: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: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", "21:24", "21: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: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: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(7l, 7l, 7l, 7l, 7l, 7l, 7l, 7l, 7l, 7l, 7l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 5l, 5l, 5l, 5l, 5l, 3l, 3l, 3l, 3l), .label = c("bgm", "disconnect or reconnect time", "sensor 1", "sensor 2", "sensor 3", "sensor 4", "ysi"), class = "factor"), amount = c(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, 224, 269, 283, 267, 243, 252, 269, 272, 281), type = structure(c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 6l, 6l, 6l, 4l, 4l, 4l, 4l, 4l, 2l, 2l, 2l, 2l), .label = c("bg data (mg/dl)", "cgm retropective value sensor 1 (mg/dl)", "cgm retropective value sensor 2 (mg/dl)", "cgm retropective value sensor 3 (mg/dl)", "cgm retropective value sensor 4 (mg/dl)", "disconnect or reconnect time"), class = "factor"), order = c(241l, 242l, 243l, 244l, 245l, 246l, 247l, 248l, 249l, 250l, 251l, 252l, 253l, 254l, 255l, 256l, 257l, 258l, 2401l, 2402l, 2403l, 2881l, 2882l, 2883l, 2884l, 2885l, 2900l, 2901l, 2902l, 2903l ), time = structure(c(1427101320, 1427102700, 1427103600, 1427104500, 1427105400, 1427108400, 1427109300, 1427111340, 1427112240, 1427115900, 1427117100, 1427121600, 1427123400, 1427125200, 1427127000, 1427128800, 1427130600, 1427132400, 1427109000, 1427116980, 1427122680, 1427125200, 1427127000, 1427128800, 1427130600, 1427132400, 1427127000, 1427128800, 1427130600, 1427132400), class = c("posixct", "posixt"), tzone = "gmt")), .names = c("edc.id.", "subject.id..", "visit", "date", "start", "end", "description", "amount", "type", "order", "time"), row.names = c(177l, 178l, 179l, 180l, 181l, 182l, 183l, 184l, 185l, 186l, 187l, 188l, 189l, 190l, 191l, 192l, 193l, 194l, 1597l, 1598l, 1599l, 1761l, 1762l, 1763l, 1764l, 1765l, 1766l, 1767l, 1768l, 1769l), class = "data.frame")
Comments
Post a Comment