मेरे पास निम्न प्रतिलिपि प्रस्तुत करने योग्य कार्य उदाहरण है:

library(scales)
library(tidyverse)

Canada <- c(383.927, 387.088, 390.648, 393.926, 395.947, 393.98, 388.772, 
392.391, 391.804, 389.321, 390.583, 390.062, 390.596, 392.19, 
392.267, 397.572, 397.807, 394.64, 391.737, 392.659, 392.483, 
392.012, 391.842, 394.06, 391.661, 390.621, 392.533, 396.218, 
401.802, 397.298, 392.468, 392.056, 394.752, 392.947, 392.606, 
391.839, 392.169, 393.29, 399.993, 396.114, 403.1, 398.263, 395.066, 
397.16, 399.562, 396.865, 392.898, 396.89, 398.529, 402.269, 
-9.999e+09, 398.294, 401.033, 399.328, -9.999e+09, 400.062, 395.829, 
397.754, 395.306, 394.87, 398.469, 399.91, 405.053, 404.678, 
402.185, 396.605, -9.999e+09, 402.252, 405.295, 401.08, 400.527, 
398.38, 400.152, 396.42, 402.497, 406.855, 403.56, -9.999e+09, 
-9.999e+09, 405.773, 402.306, 403.146, 403.079, 400.712)

x.axis <- c("Jan. 2010","","","","","July 2010","","","","","",
"Dec. 2010","","","","","","July 2011","","","","","",
"Dec. 2011","","","","","","July 2012","","","","","",
"Dec. 2012","","","","","","July 2013","","","","","",
"Dec. 2013","","","","","","July 2014","","","","","",
"Dec. 2014","","","","","","July 2015","","","","","",
"Dec. 2015","","","","","","July 2016","","","","","",
"Dec. 2016","")

Canplot <- ggplot() + geom_line(aes(time,Canada))
   + scale_x_continuous(breaks = scales::pretty_breaks(n = 24))
   + theme_classic(base_size=12) + ylab("CO2 (ppm)") + xlab("Time") + ylim(380,410)
   + scale_x_discrete(limits = x.axis)
   + theme(axis.text.x = element_text(angle = 45, hjust=1))

Canada1 <- c(390.704083333333, 393.322083333333, 393.900083333333, 396.780833333333, 
398.3274, 401.312181818182, 402.45) # The second set of data

जो इस छवि का उत्पादन करता है:

enter image description here

Canada मासिक CO2 की एक सरणी है और Canada1 वार्षिक CO2 की एक सरणी है। Canplot वर्तमान में Canada प्लॉट करता है और मैं इसके ऊपर Canada1 ओवरले करना चाहता हूं लेकिन मुझे यकीन नहीं है कि Canada1 को अलग-अलग x अक्ष मानों से कैसे जोड़ा जाए जो July, (year). मेरे शुरुआती विचार या तो Canplot + geom_line() etc. करना था, मैं सोच रहा था कि क्या यह इस तक पहुंचने का सही तरीका है?

0
Brennan 14 अप्रैल 2020, 02:38

1 उत्तर

सबसे बढ़िया उत्तर

आपको प्रत्येक डेटासेट के लिए समान समय लेबल रखने की आवश्यकता है और फिर उन्हें geom_line का उपयोग करके एक साथ प्लॉट किया जा सकता है।

library(scales)
library(tidyverse)
library(ggplot2)

Canada <- c(383.927, 387.088, 390.648, 393.926, 395.947, 393.98, 388.772, 
            392.391, 391.804, 389.321, 390.583, 390.062, 390.596, 392.19, 
            392.267, 397.572, 397.807, 394.64, 391.737, 392.659, 392.483, 
            392.012, 391.842, 394.06, 391.661, 390.621, 392.533, 396.218, 
            401.802, 397.298, 392.468, 392.056, 394.752, 392.947, 392.606, 
            391.839, 392.169, 393.29, 399.993, 396.114, 403.1, 398.263, 395.066, 
            397.16, 399.562, 396.865, 392.898, 396.89, 398.529, 402.269, 
            -9.999e+09, 398.294, 401.033, 399.328, -9.999e+09, 400.062, 395.829, 
            397.754, 395.306, 394.87, 398.469, 399.91, 405.053, 404.678, 
            402.185, 396.605, -9.999e+09, 402.252, 405.295, 401.08, 400.527, 
            398.38, 400.152, 396.42, 402.497, 406.855, 403.56, -9.999e+09, 
            -9.999e+09, 405.773, 402.306, 403.146, 403.079, 400.712)

time <- c("Jan. 2010","","","","","July 2010","","","","","",
            "Dec. 2010","","","","","","July 2011","","","","","",
            "Dec. 2011","","","","","","July 2012","","","","","",
            "Dec. 2012","","","","","","July 2013","","","","","",
            "Dec. 2013","","","","","","July 2014","","","","","",
            "Dec. 2014","","","","","","July 2015","","","","","",
            "Dec. 2015","","","","","","July 2016","","","","","",
            "Dec. 2016")

Canada1 <- c(390.704083333333, 393.322083333333, 393.900083333333, 396.780833333333, 
             398.3274, 401.312181818182, 402.45) # The second set of data

यहां सभी महीनों को सूचीबद्ध किया गया है और सभी महीनों को संग्रहीत करने वाला डेटाफ्रेम, CO2 मान और आपके इच्छित लेबल रखे गए हैं।

## Set the date range and select by month
d1 <- as.Date(paste0("201001","01"), "%Y%m%d")
d2 <- as.Date(paste0("201612","01"), "%Y%m%d")

date1 <- format(seq(d1,d2,by="month"), "%Y%m%d")
dat <- data.frame(co2 = Canada, labels = as.character(time), date = date1, group = 1)

## Set the date range and select by year
d1 <- as.Date(paste0("201006","01"), "%Y%m%d")
d2 <- as.Date(paste0("201606","01"), "%Y%m%d")
date2 <- format(seq(d1,d2,by="year"), "%Y%m%d")

dat2 <- data.frame(co2 = Canada1, date = date2, group = 2)

2 डेटासेट प्लॉट किए गए हैं और x लेबल dat$labels पर सेट हैं

Canplot <- ggplot() + 
  geom_line(data = dat, aes(x = date, y = co2, group = group)) + 
  geom_line(data = dat2, aes(x = date, y = co2, group = group), color = "Red") + 
  ylim(380,410) +
  scale_x_discrete(labels = data$labels)+
  theme_classic(base_size=12) + ylab("CO2 (ppm)") + xlab("Time") +
  theme(axis.text.x = element_text(angle = 45, hjust=1))

Canplot
1
Tjn25 14 अप्रैल 2020, 03:05