2017-10-31 2 views
-1

50points and 10points temp2 데이터 프레임의 행 수가 변경되면 직사각형 색상이 변경됩니다. 사용중인 코드가 아래에 있습니다.ggplot2를 사용하면 채우기 색상이 변경됩니다.

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.0 34,372,404,737, 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, 5321.6, 5322.1, 5322.6, 5323.1, 5323.6, 5323.7, ("OIL_PRESSURE", "LCL", "end_engine_hours" ), row.names = c (NA, -50L), 클래스 = "data.frame ("5333.3, ")

+0

몇 가지 예제 데이터를 제공해 주시겠습니까? –

+0

예 이것은 temp2 데이터 프레임 샘플입니다. –

+0

그는 "우리가 함께 복사하여 붙여 넣기 할 수있는 예"를 의미합니다. ** ** **는 ** 우리가 시간을 절약해야하는 ** ** 질문을하고 있습니다.) – Arthur

답변

0

색상이 변경되는 이유는 각 데이터 관찰에 대해 알파로 사각형을 그리기 때문입니다. 대신 다음을 시도하십시오.

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") 
관련 문제