2015-09-15 11 views
5

मैं इस डेटासाजिश आयतों - अमान्य इनपुट: time_trans वर्ग POSIXct की वस्तुओं के साथ काम करता है केवल

df <- structure(list(IndID = structure(c(16L, 15L, 14L, 13L, 12L, 11L, 
10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), .Label = c("16", "15", 
"14", "13", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", 
"2", "1"), class = "factor"), StartDate = structure(c(1313042400, 
1312956000, 1313560800, 1363672800, 1374040800, 1374040800, 1374040800, 
1374040800, 1374040800, 1374040800, 1374040800, 1365832800, 1365919200, 
1366178400, 1395727200, 1395813600), class = c("POSIXct", "POSIXt" 
)), EndDate = structure(c(1377928800, 1378015200, 1378015200, 
1386572400, 1410760800, 1410760800, 1410760800, 1410674400, 1410760800, 
1406959200, 1399356000, 1427868000, 1394517600, 1428213600, 1428040800, 
1420959600), class = c("POSIXct", "POSIXt"))), .Names = c("IndID", 
"StartDate", "EndDate"), row.names = c(NA, -16L), class = "data.frame") 

    IndID StartDate EndDate 
1  1 2011-08-11 2013-08-31 
2  2 2011-08-10 2013-09-01 
3  3 2011-08-17 2013-09-01 
4  4 2013-03-19 2013-12-09 
5  5 2013-07-17 2014-09-15 
6  6 2013-07-17 2014-09-15 

है और इस साजिश

library(lubridate) 
    require(gglopt2) 
    df$IndID <- factor(df$IndID, levels = rev(df$IndID)) 



p1 <- ggplot(df, aes(x=IndID, fill = IndID))+ 
     geom_rect(aes(x = IndID, xmin = as.numeric(IndID) - 0.45, xmax = as.numeric(IndID) + 0.45, ymin = StartDate, ymax = EndDate))+ 
     coord_flip()+ 
     xlab("IndID")+ 
     ylab("Time")+ 
     ggtitle("Individual Monitoring Periods")+ 
     geom_text(aes(y = StartDate + as.difftime(8, unit = "weeks"), label = paste(month(StartDate, label = T), year(StartDate), sep = "-"))) + 
     geom_text(aes(y = EndDate - as.difftime(9, unit = "weeks"), label = paste(month(EndDate, label = T), year(EndDate), sep = "-"))) 

    p1 

enter image description here

मैं इसके अलावा कर सकते हैं, मैं इस क्षेत्र को 1 9 जून और 1 9 अक्टूबर के बीच हर साल के लिए छायांकित करना चाहता हूं। ऐसा करने के लिए, मैंने data.frame तिथियों की तारीख बनाई जिन्हें बाद में data.frame में POSIXct प्रारूप में परिवर्तित कर दिया गया। (हाँ, यह भद्दा है ..)

temp <- data.frame(
    start = as.Date(c('2011-06-19', '2012-06-19', '2013-06-19', '2014-06-19', '2015-06-19')), 
    end = as.Date(c('2011-10-19', '2012-10-19', '2013-10-19', '2014-10-19', '2015-10-19'))) 
str(temp) 
dateRanges <- data.frame(
    start = as.POSIXct(temp [,1], "%Y-%m-%d") + hours(6), 
    end = as.POSIXct(temp [,2], "%Y-%m-%d") + hours(6)) 
str(dateRanges) 

जब मैं निम्न कोड के साथ साजिश करने के लिए नए आयतों को जोड़ने का प्रयास, मैं पोस्ट शीर्षक में संकेत त्रुटि मिलती है।

p1 + geom_rect(data = dateRanges, aes(xmin = start , xmax = end, ymin = -Inf, ymax = Inf), inherit.aes= F, alpha = 0.4, fill = c("lightblue")) 

जहां तक ​​मेरा dateRanges की str() को देखकर बता सकते हैं, वे सही ढंग से POSIXct वर्ग के रूप में स्वरूपित हैं।

मैंने similar post here देखा था लेकिन अभी भी संबंधित मुद्दों के बीच बिंदुओं को जोड़ने और मेरी समस्या को ठीक करने के लिए संघर्ष कर रहा हूं। किसी सुझाव के लिए अग्रिम धन्यवाद।

+1

शायद आपको टैग 'गैंट-चार्ट' जोड़ना चाहिए या सिर्फ शब्द का उल्लेख करना चाहिए, जो भविष्य की खोजों में मदद कर सकता है। – maj

उत्तर

4

आपकी समस्या यह है कि आप अपनी प्रारंभिक कॉल में coord_flip() चला चुके हैं। साजिश फिसल गई है, लेकिन ggplot अभी भी सोचता है कि एक्स और वाई मूल एक्स और वाई हैं।

तो, ठीक करने के लिए, बस अंतिम geom_rect में अपनी x और y aes स्विच:

p1 + geom_rect(data = dateRanges, aes(ymin = start , ymax = end, xmin = -Inf, xmax = Inf), inherit.aes= F, alpha = 0.4, fill = c("lightblue")) 
p1 

enter image description here

संपादित करें: सलाखों बनाने के पीछे नगण्य का एक छोटा सा की आवश्यकता है। पहले हम geom_rect जो सलाखों बनाता है तो इसके पीछे है कॉल करने के लिए है, और हम एईएस से बाहर xmin और xmax लिए कदम अक्ष और कारकों के स्तर के साथ खिलवाड़ से बचने के लिए कॉल करें:

p1 <- ggplot(df, aes(x=IndID, fill = IndID))+ 
    geom_rect(data = dateRanges, aes(ymin = start, ymax = end), xmin = -Inf, xmax = Inf, alpha = 0.4, inherit.aes=FALSE, fill = "lightblue")+ 
    geom_rect(data = df, aes(x = IndID, xmin = as.numeric(IndID) - 0.45, xmax = as.numeric(IndID) + 0.45, ymin = StartDate, ymax = EndDate))+ 
    coord_flip()+ 
    xlab("IndID")+ 
    ylab("Time")+ 
    ggtitle("Individual Monitoring Periods")+ 
    geom_text(aes(y = StartDate + as.difftime(8, unit = "weeks"), label = paste(month(StartDate, label = T), year(StartDate), sep = "-"))) + 
    geom_text(aes(y = EndDate - as.difftime(9, unit = "weeks"), label = paste(month(EndDate, label = T), year(EndDate), sep = "-"))) 

p1 

enter image description here

+0

बिल्कुल सही! बहुत उपयोगी। क्या साजिश के "पीछे" में नई छायांकन को मजबूर करने का कोई तरीका है ताकि पहली साजिश के आयत शीर्ष पर हों। –

+0

प्रारंभिक कॉल में कोड की अपनी नई पंक्ति जोड़ना (उदाहरण: पी 1 <- ggplot (डीएफ, एईएस (एक्स = इंडिड, भर = इंडिड)) + \t geom_rect (डेटा = दिनांक रेंज, एईएस (ymin = start, ymax = end , xmin = -Inf, xmax = inf), legisl.aes = F, अल्फा = 0.4, fill = c ("lightblue")) + geom_rect (aes (x = इंडिड, xmin = as.numeric (इंडिड) - 0.45 , xmax = as.numeric (इंडिड) + 0.45, ymin = StartDate, ymax = endDate)) + ...) त्रुटि में परिणाम "त्रुटि: निरंतर पैमाने पर असतत मूल्य प्रदान किया गया।" मुझे कुछ और याद आना चाहिए ... –

+0

बिल्कुल सही! अतिरिक्त समाधान के लिए धन्यवाद! –

संबंधित मुद्दे