如何在不打印美学错误的情况下将标签子集化为ggplot图?

时间:2018-07-06 22:31:06

标签: r ggplot2 error-handling geom-text aesthetics

这行代码不起作用。问题是当我添加此geom_text行时,该行旨在过滤掉数据的某些点并标记它们。

这是代码:

ggplot(data=bg_total_df, aes(x = bg_total_num, y = bg_total$bloodGlucose)) +
  geom_text(data = subset(bg_total, bloodGlucose < 78), label = bg_total$bloodGlucose)+
geom_point(color = ifelse(bg_total$bloodGlucose < 78, "red", "blue"))
  

错误:美学必须为长度1或与数据相同   (16):x,y

ggplot + geom_point代码可以一起正常工作。是在我收到此错误的地方添加了geom_text代码的地方。

我一直在尝试修复此geom_text代码集,以使图形的异常值(bloodGlucose小于78且大于271)将变为红色并标记其数量。我编写的代码是我第一次尝试从小于78的值开始。

这是设置df的代码。

bg_total <- filter(csutrauma.data, bloodGlucose >= 0)
bg_total_num <- 1:328

bg_total_df <- data.frame(bg_total_num, bg_total$bloodGlucose)

bg_total$bloodGlucose (these are the filtered out blood glucose data) <-  c(118.0,  72.0, 207.0, 107.0,  99.0, 106.0,  98.0,  89.0, 114.0,  98.0, 109.0, 114.0,  85.0, 138.0, 100.0, 118.0,  99.0, 104.0,  80.0, 105.0, 113.0,  89.0, 116.0, 121.0, 120.0, 109.0,  97.0, 100.0, 112.0, 133.0,  95.0, 107.0,  91.0, 133.0,  80.0, 114.0, 118.0, 109.0, 111.0, 115.0, 122.0,  90.0, 114.0,  93.0, 106.0, 555.0, 106.0, 106.0, 119.0,  95.0, 153.0, 118.0,  75.0, 112.0, 102.0, 103.0, 150.0, 109.0, 110.0, 206.0, 103.0, 266.0, 103.0, 104.0, 119.0, 106.0,  94.0, 94.0, 119.0, 120.0, 96.0, 252.0, 121.0, 126.0, 317.0, 170.0, 119.0,  98.0, 124.0, 137.0, 255.0, 136.0, 126.0, 0.0, 117.0, 134.0, 133.0, 102.0, 150.0, 232.0, 132.0, 112.0, 78.0, 155.0, 113.0, 122.0, 153.0,  96.0,  83.0, 154.0,  41.0,  91.0, 120.0, 134.0, 126.0, 168.0, 196.0, 125.0, 105.0, 105.0,  93.0, 88.0,  89.0,  93.0, 120.0,  93.0,  93.0, 135.0, 102.0,  86.0,  93.0, 141.0, 150.0,  96.0, 101.0,  61.0, 110.0,  81.0,  97.0, 140.0,  95.0, 106.0, 117.0, 119.0, 139.0, 110.0, 101.0, 128.0, 107.0, 160.0, 122.0, 135.0,  59.0, 102.0,  72.0, 145.0, 104.0, 134.0, 113.0, 105.0, 107.0, 100.0,  65.0, 100.0, 136.0, 110.0, 159.0, 107.0, 115.0, 101.0, 493.0,  78.0,  84.0, 229.0, 102.0, 172.0, 139.0, 102.0, 541.0,  94.0,  91.0,  88.0, 181.0, 129.0, 123.0, 108.0, 115.0,  97.0, 159.0, 124.0, 118.0, 138.0, 182.0, 115.0, 119.0, 101.0,  99.0,  88.0, 134.0, 104.0, 152.0, 130.0,  90.0, 100.0, 157.0, 112.0, 120.0, 110.0, 112.0, 104.0,  71.0, 120.0, 254.0, 118.0, 119.0, 94.0, 104.0, 183.0, 227.0, 210.0, 150.0,  93.0,  91.0,  99.0,  86.0,  90.0, 115.0, 247.0, 111.0, 109.0, 90.0, 113.0, 98.0, 80.0, 145.0, 101.0,  94.0,  94.0, 138.0, 241.0,   3.8, 106.0,  93.0, 96.0, 105.0, 181.0,  95.0,  82.0,  82.0,  18.0, 148.0, 175.0, 323.0, 118.0,  89.0, 100.0, 117.0, 175.0, 200.0, 147.0, 108.0, 210.0, 123.0, 136.0, 107.0, 117.0, 116.0, 154.0,  56.0, 102.0, 105.0, 100.0, 108.0, 226.0, 149.0, 86.0, 116.0, 140.0, 133.0, 121.0, 122.0,  98.0,  95.0, 118.0, 114.0, 113.0, 94.0,  70.0,  52.0, 110.0, 189.0,  82.0 200.0,  89.0,  95.0,  93.0, 111.0, 117.0, 110.0, 156.0,  87.0, 136.0, 336.0, 112.0, 128.0,  92.0, 61.0, 130.0, 119.0, 152.0,  90.0, 111.0, 116.0, 133.0,  94.0, 326.0, 121.0, 119.0 121.0 152.0, 113.0, 128.0, 123.0, 155.0, 176.0, 101.0, 248.0,  64.0, 90.0, 105.0, 235.0, 124.0, 142.0, 388.0, 118.0, 202.0, 156.0, 195.0)

谢谢。

0 个答案:

没有答案