在R中绘制多个数据集

时间:2015-05-05 00:18:41

标签: r plot ggplot2

我正在使用包ega和函数plotClarkeGrid来生成以下图表。这个包非常有用,因为它绘制了象限和颜色坐标点,节省了我很多时间。这正是我想要的,但现在我已经识别出异常值,并希望通过在数据点周围添加一个圆圈来突出显示它们。我的原始数据中有数百个异常值,这只是其中的一小部分。

enter image description here

到目前为止,我已经创建了两个单独的文件,一个包含所有数据,另一个只包含异常值。我一直试图做的只是简单地绘制所有点的图形,然后将异常值绘制为大的红色圆圈,以便它们可见。总之,有没有办法为这个图形添加点,以便它看起来像这样? enter image description here

以下是异常值:

data1 <- structure(list(BG = c(193L, 195L, 204L, 213L, 230L, 231L, 288L, 
    268L, 214L, 176L, 169L, 288L, 268L, 214L, 176L, 169L, 252L, 210L, 
    217L, 122L, 175L, 181L, 223L, 255L, 264L, 228L, 193L, 259L, 256L, 
    243L, 236L, 234L, 230L, 236L, 234L, 232L, 230L, 230L, 225L, 248L, 
    289L, 292L, 262L, 329L), CGM.Progressive = c(165L, 144L, 148L, 
    160L, 172L, 179L, 277L, 247L, 248L, 214L, 300L, 281L, 249L, 249L, 
    212L, 300L, 243L, 272L, 300L, 300L, 190L, 180L, 178L, 110L, 93L, 
    144L, 171L, 206L, 206L, 196L, 299L, 300L, 300L, 209L, 205L, 229L, 
    239L, 247L, 250L, 195L, 200L, 221L, 244L, 105L), BB.include = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("BG", 
    "CGM.Progressive", "BB.include"), row.names = c(NA, -44L), class = "data.frame")

以下是所有要点:

data2 <- structure(list(BG = c(58L, 57L, 67L, 121L, 157L, 191L, 193L, 
195L, 204L, 213L, 230L, 231L, 239L, 252L, 285L, 240L, 209L, 165L, 
140L, 242L, 280L, 288L, 268L, 214L, 176L, 169L, 242L, 280L, 288L, 
268L, 214L, 176L, 169L, 190L, 221L, 232L, 232L, 233L, 209L, 274L, 
252L, 217L, 122L, 218L, 236L, 248L, 263L, 268L, 252L, 210L, 217L, 
122L, 274L, 252L, 217L, 248L, 263L, 120L, 124L, 146L, 169L, 175L, 
181L, 223L, 255L, 264L, 228L, 193L, 150L, 147L, 141L, 144L, 138L, 
150L, 147L, 141L, 230L, 216L, 207L, 231L, 259L, 256L, 243L, 236L, 
234L, 230L, 157L, 207L, 231L, 259L, 256L, 243L, 236L, 234L, 230L, 
259L, 256L, 237L, 236L, 234L, 232L, 230L, 230L, 225L, 237L, 259L, 
243L, 237L, 236L, 357L, 347L, 318L, 301L, 303L, 158L, 138L, 94L, 
88L, 137L, 188L, 201L, 225L, 248L, 289L, 292L, 262L, 329L, 109L, 
94L, 137L, 168L, 188L, 225L, 248L, 289L), CGM.Progressive = c(57L, 
50L, 46L, 105L, 170L, 169L, 165L, 144L, 148L, 160L, 172L, 179L, 
251L, 269L, 293L, 276L, 226L, 148L, 110L, 227L, 255L, 277L, 247L, 
248L, 214L, 300L, 231L, 259L, 281L, 249L, 249L, 212L, 300L, 199L, 
197L, 194L, 205L, 220L, 252L, 273L, 270L, 110L, 112L, 204L, 204L, 
255L, 260L, 252L, 243L, 272L, 300L, 300L, 300L, 300L, 239L, 283L, 
293L, 160L, 198L, 243L, 193L, 190L, 180L, 178L, 110L, 93L, 144L, 
171L, 147L, 128L, 141L, 129L, 121L, 116L, 146L, 126L, 216L, 203L, 
202L, 227L, 267L, 267L, 268L, 266L, 270L, 270L, 161L, 197L, 243L, 
206L, 206L, 196L, 299L, 300L, 300L, 232L, 200L, 231L, 209L, 205L, 
229L, 239L, 247L, 250L, 240L, 236L, 250L, 222L, 193L, 300L, 300L, 
275L, 278L, 300L, 171L, 163L, 106L, 74L, 130L, 187L, 184L, 176L, 
195L, 200L, 221L, 244L, 105L, 106L, 86L, 138L, 152L, 196L, 200L, 
192L, 186L), BB.include = c(1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 
0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("BG", 
"CGM.Progressive", "BB.include"), row.names = c(NA, -135L), class = "data.frame")

我的代码:

library(ega)
data1$BG = as.integer(as.character(data1$BG))
data1$CGM.Progressive = as.integer(as.character(data1$CGM.Progressive))

data2$BG = as.integer(as.character(data2$BG))
data2$CGM.Progressive = as.integer(as.character(data2$CGM.Progressive))

x1<-data1[1:45,2]
y1<-data1[1:45,1]

x2<-data1[1:136,2]
y2<-data1[1:136,1]

plotClarkeGrid(x1, y1, 
 title = "Reference vs. CGM, CEG \n for Retrospective Calibration", 
  xlab = "Reference (mg/dl)", 
  ylab = "Experimental (mg/dl)",
  pointsize = 1, pointalpha = 1) 

1 个答案:

答案 0 :(得分:2)

根据?plotClarkeGrid,该函数返回一个ggplot2对象。因此,您应该能够像这样在原始图中添加点:

# Your plot
p =  plotClarkeGrid(x1, y1, 
  title = "Reference vs. CGM, CEG \n for Retrospective Calibration", 
  xlab = "Reference (mg/dl)", 
  ylab = "Experimental (mg/dl)",
  pointsize = 1, pointalpha = 1)

# Add points to the plot
library(ggplot2)
p + 
  geom_point(data = data1, aes(CGM.Progressive, BG), 
     shape = 1, size = 10, fill = NA, color = "red")