R 将形状和颜色图例合并为一个(ggplot)

R 将形状和颜色图例合并为一个(ggplot),r,ggplot2,legend,R,Ggplot2,Legend,我正在尝试将形状和颜色图例合并为一个,这样我的图形看起来更干净。例如,我希望BG数据(mg/dl)用红色填充,传感器1的CGM回顾值(mg/dl)用橙色填充,等等。。我试着使用scale\u color\u manual和scale\u shape\u manual但我似乎无法将它们结合起来 这是我的密码: #Load libabries needed to create graphs library(ggplot2) library(dplyr) library(scales) librar

我正在尝试将形状和颜色图例合并为一个,这样我的图形看起来更干净。例如,我希望
BG数据(mg/dl)
用红色填充,传感器1的CGM回顾值(mg/dl)用橙色填充,等等。。我试着使用
scale\u color\u manual
scale\u shape\u manual
但我似乎无法将它们结合起来

这是我的密码:

#Load libabries needed to create graphs
library(ggplot2)
library(dplyr)
library(scales)
library(gridExtra)
library(grid)

#create dataset for Subject i
data1<-subset(carb_data,Subject.ID.. == 1)

carb1<-subset(data1,Visit == 1)

#Makes sure that the "Amount" is stored as a number and that "time" is stored as a 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 for Sensor 1 (mg/dl)", ], size=4) +
   geom_point(data=carb1[carb1$Type == "CGM Retropective Value for Sensor 2 (mg/dl)", ], size=4) +
   geom_point(data=carb1[carb1$Type == "CGM Retropective Value for Sensor 3 (mg/dl)", ], size=4) + 
   geom_point(data=carb1[carb1$Type == "CGM Retropective Value for 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), by = 50),2)) + 
   xlab("Time (HH:MM)") + ylab("Glucose (mg/dL), Carbohydrates (grams)") + 
   ggtitle(toString(c("CGM Retrospectve and 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)

我不确定您想归档什么-类似于
q+guides(shape=guide\u legend(override.aes=list)(color=c(“红色”,rep(“黑色”,3~”))))
?很抱歉@lukeA回复太晚!我编辑了我的问题并更新了图片,使它更清晰一些。我想知道是否有一种方法可以去除冗余信息,并将两个图例合并为一个。例如,传感器1的CGM追溯值(mg/dl)在两个图例中都出现,因此我想将颜色(顶部图例)与形状(底部图例)结合起来,形成一个颜色/形状图例
    > 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 for Sensor 1 (mg/dl)", "CGM Retropective Value for Sensor 2 (mg/dl)", 
    "CGM Retropective Value for Sensor 3 (mg/dl)", "CGM Retropective Value for 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")