使用ggplot2

时间:2017-10-31 18:47:43

标签: r ggplot2

50points and 10points当temp2数据框中的行数没有变化时,矩形颜色会发生变化。下面是我正在使用的代码。

ggplot(temp2,
       aes(x = temp2$end_engine_hours, 
           y = temp2$OIL_PRESSURE)) + 
  geom_point(color = ifelse(temp2$OIL_PRESSURE > temp2$LCL,"green","red")) + 
  geom_rect(aes(xmin=min(temp2$end_engine_hours) - 0.5, 
                xmax= max(temp2$end_engine_hours) + 0.5, 
                ymin= 0, 
                ymax=temp2$LCL),
            color = "black", alpha = 0.02, fill = 'red')

temp2数据框:

structure(list(OIL_PRESSURE = c(126.229996, 116.689995, 123.451, 

117.28,121.909,125.96,126,122.119995,116.799995,127.038994, 129.418,114,125.465996,123.364,124.635994,133.5,128.42699, 123.20799,127.278,114.856995,122.63,124.147995,118.588, 120.13799,122.758995,116.433,116.409996,125.022995,130.756, 115.90199,117.5,126.175995,122.562,142.086,121,124.978, 127.545,121.06699,122.189995,126.940994,125.036995,122.884995, 123,122.824,121.80499,122.356995,125.253,126.94399,120.329994, 123.76199),LCL = c(231.034372404737,231.034372404737,231.034372404737, 231.034372404737,231.034372404737,231.034372404737,231.034372404737, 231.034372404737,231.034372404737,231.034372404737,231.034372404737, 231.034372404737,231.034372404737,231.034372404737,231.034372404737, 231.034372404737,231.034372404737,231.034372404737,231.034372404737, 231.034372404737,231.034372404737,231.034372404737,231.034372404737, 231.034372404737,231.034372404737,231.034372404737,231.034372404737, 231.034372404737,231.034372404737,231.034372404737,231.034372404737, 231.034372404737,231.034372404737,231.034372404737,231.034372404737, 231.034372404737,231.034372404737,231.034372404737,231.034372404737, 231.034372404737,231.034372404737,231.034372404737,231.034372404737, 231.034372404737,231.034372404737,231.034372404737,231.034372404737, 231.034372404737,231.034372404737,231.034372404737),end_engine_hours = c(5301.9, 5303.1,5303.6,5304.6,5305.1,5305.5,5305.8,5306.8,5307.2, 5308.2,5308.7,5309.7,5310.2,5310.7,5312.7,5313.2,5313.7, 5314.2,5315.2,5316.7,5317.2,5317.7,5318.1,5319.1,5319.6, 5320.1,5320.6,5321.6,5322.1,5322.6,5323.1,5323.6,5323.7, 5324.4,5324.9,5325.9,5326.4,5327.4,5327.5,5328,5328.4, 5328.8,5330.3,5331.3,5331.8,5333.3,5334.7,5335.2,5335.7, 5336.2)),。Name = c(“OIL_PRESSURE”,“LCL”,“end_engine_hours” ),row.names = c(NA,-50L),class =“data.frame”)

1 个答案:

答案 0 :(得分:0)

颜色似乎改变的原因是因为您正在为每个数据观察绘制一个带有alpha的矩形。相反,试试这个:

temp2$flag <- with(temp2, OIL_PRESSURE > LCL)

require(ggplot2)

ggplot() + 
  geom_rect(aes(xmin = (min(temp2$end_engine_hours) - 0.5), 
                xmax = (max(temp2$end_engine_hours) + 0.5), 
                ymin = 0, 
                ymax = max(temp2$LCL)),
            color = "black", fill = 'red', alpha = .2) +
  geom_point(data = temp2, aes(x = end_engine_hours , 
                               y = OIL_PRESSURE, 
                               color = flag)) + 
  scale_colour_manual(values = c("red", "green")) + 
  theme(legend.position = "none")