重新排列图例ggplot

时间:2015-01-23 01:31:35

标签: r ggplot2 legend

首先,谢谢你的任何意见。我对R比较新,所以任何提示/建议都会非常感激。所以我有以下图表: enter image description here

我想重新安排传奇,所以它看起来像这样,所以它更容易理解: enter image description here

我尝试使用scale_fill_discrete无法使用。这是我的代码:

carb_data<- read.csv("Carb_Insulin_BG.csv", header=TRUE)



library(ggplot2)
library(dplyr)

#get data for patient 1
data1<-subset(carb_data,Subject.ID.. == 1)

#get all the data from Visit 1
carb1<-subset(data1,Visit == 1)

visit1 <- toString(carb1[1,3])

carb1<- mutate(carb1, Start=sprintf("%05s", as.character(Start)), Amount= as.numeric(as.character(Amount)))

q <- ggplot(data=carb1, aes(x=Start, 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(size=2) + 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("Value") + ggtitle(toString(c("Carbohydrate, Insulin ,and Blood Glucose Data \n Site ID 1 \nVisit", visit1)))

q

我的数据:

> dput(carb1)
structure(list(EDC.ID. = structure(c(34L, 34L, 34L, 34L, 34L, 
34L, 34L, 34L, 14L, 34L, 34L, 34L, 14L, 34L, 34L, 34L, 14L, 34L, 
34L, 14L, 34L, 34L), .Label = c("Subject 1", "Subject 10", "Subject 11", 
"Subject 12", "Subject 13", "Subject 14", "Subject 15", "Subject 16", 
"Subject 17", "Subject 18", "Subject 19", "Subject 2", "Subject 20", 
"Subject 3", "Subject 4", "Subject 5", "Subject 6", "Subject 7", 
"Subject 8", "Subject 9", "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), Visit = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Data = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 12L, 1L, 1L, 1L, 12L, 1L, 1L, 1L, 
12L, 1L, 1L, 12L, 1L, 1L), .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 = c("00:00", "09:02", "09:25", "09:55", "10:10", "11:00", 
    "11:15", "11:49", "11:54", "12:04", "13:05", "13:25", "13:40", 
    "14:40", "15:10", "15:40", "16:10", "16:10", "16:40", "17:10", 
    "17:10", "17:40"), End = structure(c(1L, 483L, 504L, 530L, 
    14L, 62L, 75L, 108L, 113L, 122L, 181L, 201L, 238L, 270L, 
    298L, 327L, 357L, 357L, 387L, 416L, 416L, 439L), .Label = c("0:00", 
    "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: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", "2:40", "21:24", "21:27", 
    "21:39", "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: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(16L, 16L, 16L, 16L, 16L, 16L, 16L, 
    16L, 5L, 16L, 16L, 16L, 4L, 1L, 1L, 1L, 5L, 1L, 1L, 5L, 1L, 
    1L), .Label = c("BGM", "Dextrose 50% (mL)", "dextrose Tabs", 
    "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(150, 161.5, 150.5, 144.5, 138, 122, 119, 114.5, 
    8, 110, 57.95, 53.95, 127, 102, 156, 217, 8, 250, 273, 4, 
    275, 277), Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 3L, 1L, 1L, 3L, 1L, 1L
    ), .Label = c("BG Data (mg/dL)", "Carb (grams)", "Insulin (units)"
    ), class = "factor"), Order = c(463L, 461L, 462L, 464L, 465L, 
    466L, 467L, 468L, 125L, 469L, 470L, 471L, 1L, 472L, 473L, 
    474L, 126L, 475L, 476L, 127L, 477L, 478L)), class = "data.frame", .Names = c("EDC.ID.", 
"Subject.ID..", "Visit", "Data", "Start", "End", "Description", 
"Amount", "Type", "Order"), row.names = c(NA, -22L))

0 个答案:

没有答案