Ogbos Okike
2020-May-01 16:46 UTC
[R] Datetime misrepresented on x-axis of a multiple plot
Dear Contributors,
I am trying to do a plot with multiple y-axis on a common x-axis. I have
made some progress but I am having difficulties with datetime appearing on
x-axis. Instead of as date, it appears as large numbers.
Sample of my data:
78 09 28 0 6.7 -40.4 -3.5 2.3 -3.6 278036 5.8 612
78 09 28 1 5.7 7.3 -4.4 1.4 0.6 261169 4.9 623
78 09 28 2 5.6 -20.9 -3.6 3.1 -1.8 269323 4.8 625
78 09 28 3 5.8 -46.9 -3.4 0.2 -3.6 254221 4.7 626
78 09 28 4 6.2 -41.4 -3.9 1.2 -3.6 248567 4.2 618
78 09 28 5 6.1 -54.6 -3 1 -4.4 252669 4.4 629
78 09 28 6 NA NA NA NA NA 220480 4.3 606
78 09 28 7 7.1 26 -4.8 3.6 2.9 216887 4.4 613
78 09 28 8 7.1 -21.4 -4.7 3.5 -2.3 249928 4 643
78 09 28 9 7 -13.6 -2.7 5.2 -1.4 268806 4 656
78 09 28 10 6.6 -18.5 -3.7 3.6 -1.7 285608 4.2 651
78 09 28 11 6.7 -6.6 -2.1 4.7 -0.6 280446 4.2 661
78 09 28 12 6.7 -29.7 -1.9 4 -2.5 281928 4.2 659
78 09 28 13 6.2 -8.7 -2.1 4.7 -0.8 273465 4.4 651
78 09 28 14 6.1 31.8 -4.7 0.9 3 212374 4.2 631
78 09 28 15 5.4 9.3 -4.5 -0.9 0.8 219662 3.9 636
78 09 28 16 4.9 6.3 -4.5 -0.5 0.5 220610 3.8 628
78 09 28 17 4.7 4.5 -4.3 -0.6 0.3 214432 3.7 628
78 09 28 18 4.9 3.1 -4.5 0.3 0.2 202199 3.5 623
78 09 28 19 5 -13 -4.6 -0.3 -1.1 192859 3.2 619
78 09 28 20 5.1 -26 -3.5 2 -2 193868 3.1 627
78 09 28 21 10.1 -37.5 -5.1 4.9 -5.4 284122 5.9 683
78 09 28 22 8.4 -7.3 -3.6 5.5 -0.8 367499 6.2 694
78 09 28 23 8.2 -17.7 -4.7 4.4 -2.1 346644 4.9 689
78 09 29 0 8 6.3 -4.3 5.8 0.8 269569 4.7 708
78 09 29 1 8.6 35 -3 6.2 4.8 187132 3.2 709
78 09 29 2 8.1 24.8 -4.1 6 3.3 166644 3.5 689
78 09 29 3 15.9 29.6 -2.4 9.6 5.6 720902 6.6 866
78 09 29 4 14.9 18.9 -2.8 13.8 4.8 1587324 8.1 912
78 09 29 5 14.1 8.2 -10.2 8.3 1.9 1509336 8.1 871
78 09 29 6 15.6 -2.9 -6.1 11.5 -0.7 968890 7.8 878
78 09 29 7 19.6 -49.7 1.2 12.1 -14.4 574978 5.4 906
78 09 29 8 23.5 -44.2 -1.4 11.7 -11.5 287721 4.1 865
78 09 29 9 25.3 -71.8 -2.4 7.5 -23.9 58521 5.1 859
78 09 29 10 24.8 -63.1 -2.3 10.9 -22 74956 1.5 821
78 09 29 11 23.8 -50 -2 15.1 -18.1 64510 3 807
78 09 29 12 21.4 -34.3 0.1 17.6 -12 50247 2.3 795
78 09 29 13 18.4 -20 -2 17 -6.3 59939 2.9 802
78 09 29 14 16.4 -13.9 -2.8 15.6 -3.9 56499 1.4 799
78 09 29 15 15.3 -7.9 -2 14.9 -2.1 49346 1.3 759
78 09 29 16 14.1 -0.5 -2.9 13.8 -0.1 57732 0.5 730
78 09 29 17 12.6 14.7 -3.1 11.5 3.1 44351 1.7 722
78 09 29 18 11.2 74.3 0.6 2.9 10.4 68224 0.4 712
78 09 29 19 10.5 60.5 3.2 4 9.1 48082 0.2 706
78 09 29 20 10 54.7 3.9 4.2 8.1 56404 0.2 710
78 09 29 21 9.3 61.1 3.4 2.9 8.1 68824 0.2 694
78 09 29 22 8.6 64.4 2.6 2.7 7.8 44579 0.2 683
78 09 29 23 7.9 63.3 2 3 7.1 39760 0.2 661
78 09 30 0 7.3 59.8 2.3 2.8 6.3 NA NA NA
78 09 30 1 6.6 65.6 1.7 2.1 6 57382 0.5 664
78 09 30 2 6 70.8 1.3 1.4 5.5 63540 0.5 654
78 09 30 3 4.4 45.1 1 2.7 2.9 60856 2.5 635
78 09 30 4 3.6 -28 -1 -0.3 -0.6 80967 1.9 633
78 09 30 5 4 -47.6 -2.2 -0.1 -2.4 48391 1.2 642
78 09 30 6 4.1 -28.9 -3.1 -1.4 -1.9 45012 1.1 643
78 09 30 7 4.2 -42.7 -2.7 1.2 -2.7 45562 1.1 633
and part of my code is:
Sys.setenv( TZ="GMT" )
dta<-read.table("IFEDIFIG1D",col.names=c("year",
"month", "day",
"hour","B","LAT","BX","BZ","BY","SWT","SWD","SW"))
dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900))
dta$datetime <- with( dta, as.POSIXct(ISOdatetime(year,
month,day,hour,0,0)))
x = dta$datetime
B=dta$B
LAT=dta$LAT
BX=dta$BX
BZ=dta$BZ
par(mfcol = c(3, 1), mar = numeric(4), oma = c(4, 4, 1, 1),
mgp = c(2, 0.5, 0))#0.5 here controls x-axis margins
plot(x, B,type="l", axes = FALSE)
abline(v=-3)
axis(2L)
plot(x, LAT,type="l", axes = FALSE)
abline(v=-3)
axis(2L)
plot(x, BX, type="l",axes = FALSE)
abline(v=-3)
axis(1L)
axis(2L)
mtext("A1", side = 1, outer = TRUE, line = 2.2)
mtext("B1", side = 2, outer = TRUE, line = 2.2,at =0.2)
mtext("B2", side = 2, outer = TRUE, line = 2.2,at =0.5)
mtext("B2", side = 2, outer = TRUE, line = 2.2,at =0.8)
If I do a simple plot of x and B, the x-axis will be fine, appearing as
Thu, Fri, Sat, Sun.
Please let me know what I am doing wrong with the multiple plot code above.
I want the same Thu, Fri, Sat, Sun to appear on the common x-axis.
I am most grateful for your kind response.
Warmest regards
Ogbos
[[alternative HTML version deleted]]
Rui Barradas
2020-May-01 17:29 UTC
[R] Datetime misrepresented on x-axis of a multiple plot
Hello,
If you don't mind a ggplot2 solution, here it is.
As usual with ggplot, it's better to have the data in long format so I
also load packages dplyr and tidyr. The reshaping accounts for half the
code, the code for the graph itself is not very complicated.
library(ggplot2)
library(scales)
library(dplyr)
library(tidyr)
dta %>%
select(datetime, B, BX, BZ) %>%
rename(x = datetime, B1 = B, B2 = BX, B3 = BZ) %>%
pivot_longer(
cols = starts_with("B"),
names_to = "variable",
values_to = "value"
) %>%
ggplot(aes(x, value)) +
geom_line() +
xlab("") + ylab("") +
scale_x_datetime(
breaks = seq(min(x), max(x), by = "days"),
labels = time_format("%a")) +
facet_grid(rows = vars(variable)) +
theme_bw()
Hope this helps,
Rui Barradas
?s 17:46 de 01/05/20, Ogbos Okike escreveu:> Dear Contributors,
> I am trying to do a plot with multiple y-axis on a common x-axis. I have
> made some progress but I am having difficulties with datetime appearing on
> x-axis. Instead of as date, it appears as large numbers.
>
> Sample of my data:
> 78 09 28 0 6.7 -40.4 -3.5 2.3 -3.6 278036 5.8 612
> 78 09 28 1 5.7 7.3 -4.4 1.4 0.6 261169 4.9 623
> 78 09 28 2 5.6 -20.9 -3.6 3.1 -1.8 269323 4.8 625
> 78 09 28 3 5.8 -46.9 -3.4 0.2 -3.6 254221 4.7 626
> 78 09 28 4 6.2 -41.4 -3.9 1.2 -3.6 248567 4.2 618
> 78 09 28 5 6.1 -54.6 -3 1 -4.4 252669 4.4 629
> 78 09 28 6 NA NA NA NA NA 220480 4.3 606
> 78 09 28 7 7.1 26 -4.8 3.6 2.9 216887 4.4 613
> 78 09 28 8 7.1 -21.4 -4.7 3.5 -2.3 249928 4 643
> 78 09 28 9 7 -13.6 -2.7 5.2 -1.4 268806 4 656
> 78 09 28 10 6.6 -18.5 -3.7 3.6 -1.7 285608 4.2 651
> 78 09 28 11 6.7 -6.6 -2.1 4.7 -0.6 280446 4.2 661
> 78 09 28 12 6.7 -29.7 -1.9 4 -2.5 281928 4.2 659
> 78 09 28 13 6.2 -8.7 -2.1 4.7 -0.8 273465 4.4 651
> 78 09 28 14 6.1 31.8 -4.7 0.9 3 212374 4.2 631
> 78 09 28 15 5.4 9.3 -4.5 -0.9 0.8 219662 3.9 636
> 78 09 28 16 4.9 6.3 -4.5 -0.5 0.5 220610 3.8 628
> 78 09 28 17 4.7 4.5 -4.3 -0.6 0.3 214432 3.7 628
> 78 09 28 18 4.9 3.1 -4.5 0.3 0.2 202199 3.5 623
> 78 09 28 19 5 -13 -4.6 -0.3 -1.1 192859 3.2 619
> 78 09 28 20 5.1 -26 -3.5 2 -2 193868 3.1 627
> 78 09 28 21 10.1 -37.5 -5.1 4.9 -5.4 284122 5.9 683
> 78 09 28 22 8.4 -7.3 -3.6 5.5 -0.8 367499 6.2 694
> 78 09 28 23 8.2 -17.7 -4.7 4.4 -2.1 346644 4.9 689
> 78 09 29 0 8 6.3 -4.3 5.8 0.8 269569 4.7 708
> 78 09 29 1 8.6 35 -3 6.2 4.8 187132 3.2 709
> 78 09 29 2 8.1 24.8 -4.1 6 3.3 166644 3.5 689
> 78 09 29 3 15.9 29.6 -2.4 9.6 5.6 720902 6.6 866
> 78 09 29 4 14.9 18.9 -2.8 13.8 4.8 1587324 8.1 912
> 78 09 29 5 14.1 8.2 -10.2 8.3 1.9 1509336 8.1 871
> 78 09 29 6 15.6 -2.9 -6.1 11.5 -0.7 968890 7.8 878
> 78 09 29 7 19.6 -49.7 1.2 12.1 -14.4 574978 5.4 906
> 78 09 29 8 23.5 -44.2 -1.4 11.7 -11.5 287721 4.1 865
> 78 09 29 9 25.3 -71.8 -2.4 7.5 -23.9 58521 5.1 859
> 78 09 29 10 24.8 -63.1 -2.3 10.9 -22 74956 1.5 821
> 78 09 29 11 23.8 -50 -2 15.1 -18.1 64510 3 807
> 78 09 29 12 21.4 -34.3 0.1 17.6 -12 50247 2.3 795
> 78 09 29 13 18.4 -20 -2 17 -6.3 59939 2.9 802
> 78 09 29 14 16.4 -13.9 -2.8 15.6 -3.9 56499 1.4 799
> 78 09 29 15 15.3 -7.9 -2 14.9 -2.1 49346 1.3 759
> 78 09 29 16 14.1 -0.5 -2.9 13.8 -0.1 57732 0.5 730
> 78 09 29 17 12.6 14.7 -3.1 11.5 3.1 44351 1.7 722
> 78 09 29 18 11.2 74.3 0.6 2.9 10.4 68224 0.4 712
> 78 09 29 19 10.5 60.5 3.2 4 9.1 48082 0.2 706
> 78 09 29 20 10 54.7 3.9 4.2 8.1 56404 0.2 710
> 78 09 29 21 9.3 61.1 3.4 2.9 8.1 68824 0.2 694
> 78 09 29 22 8.6 64.4 2.6 2.7 7.8 44579 0.2 683
> 78 09 29 23 7.9 63.3 2 3 7.1 39760 0.2 661
> 78 09 30 0 7.3 59.8 2.3 2.8 6.3 NA NA NA
> 78 09 30 1 6.6 65.6 1.7 2.1 6 57382 0.5 664
> 78 09 30 2 6 70.8 1.3 1.4 5.5 63540 0.5 654
> 78 09 30 3 4.4 45.1 1 2.7 2.9 60856 2.5 635
> 78 09 30 4 3.6 -28 -1 -0.3 -0.6 80967 1.9 633
> 78 09 30 5 4 -47.6 -2.2 -0.1 -2.4 48391 1.2 642
> 78 09 30 6 4.1 -28.9 -3.1 -1.4 -1.9 45012 1.1 643
> 78 09 30 7 4.2 -42.7 -2.7 1.2 -2.7 45562 1.1 633
> and part of my code is:
> Sys.setenv( TZ="GMT" )
> dta<-read.table("IFEDIFIG1D",col.names=c("year",
"month", "day",
>
"hour","B","LAT","BX","BZ","BY","SWT","SWD","SW"))
> dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900))
> dta$datetime <- with( dta, as.POSIXct(ISOdatetime(year,
> month,day,hour,0,0)))
> x = dta$datetime
> B=dta$B
> LAT=dta$LAT
> BX=dta$BX
> BZ=dta$BZ
> par(mfcol = c(3, 1), mar = numeric(4), oma = c(4, 4, 1, 1),
> mgp = c(2, 0.5, 0))#0.5 here controls x-axis margins
> plot(x, B,type="l", axes = FALSE)
> abline(v=-3)
> axis(2L)
> plot(x, LAT,type="l", axes = FALSE)
> abline(v=-3)
> axis(2L)
> plot(x, BX, type="l",axes = FALSE)
> abline(v=-3)
> axis(1L)
> axis(2L)
> mtext("A1", side = 1, outer = TRUE, line = 2.2)
> mtext("B1", side = 2, outer = TRUE, line = 2.2,at =0.2)
> mtext("B2", side = 2, outer = TRUE, line = 2.2,at =0.5)
> mtext("B2", side = 2, outer = TRUE, line = 2.2,at =0.8)
>
> If I do a simple plot of x and B, the x-axis will be fine, appearing as
> Thu, Fri, Sat, Sun.
> Please let me know what I am doing wrong with the multiple plot code above.
> I want the same Thu, Fri, Sat, Sun to appear on the common x-axis.
>
> I am most grateful for your kind response.
>
> Warmest regards
> Ogbos
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Rui Barradas
2020-May-01 17:35 UTC
[R] Datetime misrepresented on x-axis of a multiple plot
Hello, I have just noticed that the facets' labels are on the right, in your code example they are on the left. Change the following: facet_grid(rows = vars(variable), switch = "y") Hope this helps, Rui Barradas ?s 18:29 de 01/05/20, Rui Barradas escreveu:> Hello, > > If you don't mind a ggplot2 solution, here it is. > As usual with ggplot, it's better to have the data in long format so I > also load packages dplyr and tidyr. The reshaping accounts for half the > code, the code? for the graph itself is not very complicated. > > > > library(ggplot2) > library(scales) > library(dplyr) > library(tidyr) > > dta %>% > ? select(datetime, B, BX, BZ) %>% > ? rename(x = datetime, B1 = B, B2 = BX, B3 = BZ) %>% > ? pivot_longer( > ??? cols = starts_with("B"), > ??? names_to = "variable", > ??? values_to = "value" > ? ) %>% > ? ggplot(aes(x, value)) + > ? geom_line() + > ? xlab("") + ylab("") + > ? scale_x_datetime( > ??? breaks = seq(min(x), max(x), by = "days"), > ??? labels = time_format("%a")) + > ? facet_grid(rows = vars(variable)) + > ? theme_bw() > > > > Hope this helps, > > Rui Barradas > > ?s 17:46 de 01/05/20, Ogbos Okike escreveu: >> Dear Contributors, >> I am trying to do a plot with multiple y-axis on a common x-axis. I have >> made some progress but I am having difficulties with datetime >> appearing on >> x-axis. Instead of as date, it appears as large numbers. >> >> Sample of my data: >> 78 09 28? 0 6.7 -40.4 -3.5 2.3 -3.6 278036 5.8 612 >> 78 09 28? 1 5.7 7.3 -4.4 1.4 0.6 261169 4.9 623 >> 78 09 28? 2 5.6 -20.9 -3.6 3.1 -1.8 269323 4.8 625 >> 78 09 28? 3 5.8 -46.9 -3.4 0.2 -3.6 254221 4.7 626 >> 78 09 28? 4 6.2 -41.4 -3.9 1.2 -3.6 248567 4.2 618 >> 78 09 28? 5 6.1 -54.6 -3 1 -4.4 252669 4.4 629 >> 78 09 28? 6 NA NA NA NA NA 220480 4.3 606 >> 78 09 28? 7 7.1 26 -4.8 3.6 2.9 216887 4.4 613 >> 78 09 28? 8 7.1 -21.4 -4.7 3.5 -2.3 249928 4 643 >> 78 09 28? 9 7 -13.6 -2.7 5.2 -1.4 268806 4 656 >> 78 09 28? 10 6.6 -18.5 -3.7 3.6 -1.7 285608 4.2 651 >> 78 09 28? 11 6.7 -6.6 -2.1 4.7 -0.6 280446 4.2 661 >> 78 09 28? 12 6.7 -29.7 -1.9 4 -2.5 281928 4.2 659 >> 78 09 28? 13 6.2 -8.7 -2.1 4.7 -0.8 273465 4.4 651 >> 78 09 28? 14 6.1 31.8 -4.7 0.9 3 212374 4.2 631 >> 78 09 28? 15 5.4 9.3 -4.5 -0.9 0.8 219662 3.9 636 >> 78 09 28? 16 4.9 6.3 -4.5 -0.5 0.5 220610 3.8 628 >> 78 09 28? 17 4.7 4.5 -4.3 -0.6 0.3 214432 3.7 628 >> 78 09 28? 18 4.9 3.1 -4.5 0.3 0.2 202199 3.5 623 >> 78 09 28? 19 5 -13 -4.6 -0.3 -1.1 192859 3.2 619 >> 78 09 28? 20 5.1 -26 -3.5 2 -2 193868 3.1 627 >> 78 09 28? 21 10.1 -37.5 -5.1 4.9 -5.4 284122 5.9 683 >> 78 09 28? 22 8.4 -7.3 -3.6 5.5 -0.8 367499 6.2 694 >> 78 09 28? 23 8.2 -17.7 -4.7 4.4 -2.1 346644 4.9 689 >> 78 09 29? 0 8 6.3 -4.3 5.8 0.8 269569 4.7 708 >> 78 09 29? 1 8.6 35 -3 6.2 4.8 187132 3.2 709 >> 78 09 29? 2 8.1 24.8 -4.1 6 3.3 166644 3.5 689 >> 78 09 29? 3 15.9 29.6 -2.4 9.6 5.6 720902 6.6 866 >> 78 09 29? 4 14.9 18.9 -2.8 13.8 4.8 1587324 8.1 912 >> 78 09 29? 5 14.1 8.2 -10.2 8.3 1.9 1509336 8.1 871 >> 78 09 29? 6 15.6 -2.9 -6.1 11.5 -0.7 968890 7.8 878 >> 78 09 29? 7 19.6 -49.7 1.2 12.1 -14.4 574978 5.4 906 >> 78 09 29? 8 23.5 -44.2 -1.4 11.7 -11.5 287721 4.1 865 >> 78 09 29? 9 25.3 -71.8 -2.4 7.5 -23.9 58521 5.1 859 >> 78 09 29? 10 24.8 -63.1 -2.3 10.9 -22 74956 1.5 821 >> 78 09 29? 11 23.8 -50 -2 15.1 -18.1 64510 3 807 >> 78 09 29? 12 21.4 -34.3 0.1 17.6 -12 50247 2.3 795 >> 78 09 29? 13 18.4 -20 -2 17 -6.3 59939 2.9 802 >> 78 09 29? 14 16.4 -13.9 -2.8 15.6 -3.9 56499 1.4 799 >> 78 09 29? 15 15.3 -7.9 -2 14.9 -2.1 49346 1.3 759 >> 78 09 29? 16 14.1 -0.5 -2.9 13.8 -0.1 57732 0.5 730 >> 78 09 29? 17 12.6 14.7 -3.1 11.5 3.1 44351 1.7 722 >> 78 09 29? 18 11.2 74.3 0.6 2.9 10.4 68224 0.4 712 >> 78 09 29? 19 10.5 60.5 3.2 4 9.1 48082 0.2 706 >> 78 09 29? 20 10 54.7 3.9 4.2 8.1 56404 0.2 710 >> 78 09 29? 21 9.3 61.1 3.4 2.9 8.1 68824 0.2 694 >> 78 09 29? 22 8.6 64.4 2.6 2.7 7.8 44579 0.2 683 >> 78 09 29? 23 7.9 63.3 2 3 7.1 39760 0.2 661 >> 78 09 30? 0 7.3 59.8 2.3 2.8 6.3 NA NA NA >> 78 09 30? 1 6.6 65.6 1.7 2.1 6 57382 0.5 664 >> 78 09 30? 2 6 70.8 1.3 1.4 5.5 63540 0.5 654 >> 78 09 30? 3 4.4 45.1 1 2.7 2.9 60856 2.5 635 >> 78 09 30? 4 3.6 -28 -1 -0.3 -0.6 80967 1.9 633 >> 78 09 30? 5 4 -47.6 -2.2 -0.1 -2.4 48391 1.2 642 >> 78 09 30? 6 4.1 -28.9 -3.1 -1.4 -1.9 45012 1.1 643 >> 78 09 30? 7 4.2 -42.7 -2.7 1.2 -2.7 45562 1.1 633 >> ? and part of my code is: >> Sys.setenv( TZ="GMT" ) >> dta<-read.table("IFEDIFIG1D",col.names=c("year", "month", "day", >> "hour","B","LAT","BX","BZ","BY","SWT","SWD","SW")) >> dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900)) >> dta$datetime <- with( dta, as.POSIXct(ISOdatetime(year, >> month,day,hour,0,0))) >> x =? dta$datetime >> B=dta$B >> LAT=dta$LAT >> BX=dta$BX >> BZ=dta$BZ >> ? par(mfcol = c(3, 1), mar = numeric(4), oma = c(4, 4, 1, 1), >> ???? mgp = c(2, 0.5, 0))#0.5 here controls x-axis margins >> plot(x, B,type="l", axes = FALSE) >> abline(v=-3) >> axis(2L) >> plot(x, LAT,type="l", axes = FALSE) >> abline(v=-3) >> axis(2L) >> ? plot(x, BX, type="l",axes = FALSE) >> abline(v=-3) >> axis(1L) >> axis(2L) >> mtext("A1", side = 1, outer = TRUE, line = 2.2) >> mtext("B1", side = 2, outer = TRUE, line = 2.2,at =0.2) >> mtext("B2", side = 2, outer = TRUE, line = 2.2,at =0.5) >> mtext("B2", side = 2, outer = TRUE, line = 2.2,at =0.8) >> >> If I do a simple plot of x and B, the x-axis will be fine, appearing as >> Thu, Fri, Sat, Sun. >> Please let me know what I am doing wrong with the multiple plot code >> above. >> I want the same Thu, Fri, Sat, Sun to appear on the common x-axis. >> >> I am most grateful for your kind response. >> >> Warmest regards >> Ogbos >> >> ????[[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Rolf Turner
2020-May-01 21:13 UTC
[R] [FORGED] Datetime misrepresented on x-axis of a multiple plot
On 2/05/20 4:46 am, Ogbos Okike wrote:> Dear Contributors, > I am trying to do a plot with multiple y-axis on a common x-axis.<SNIP> I would strongly advise you *not* to. This, although often done, is a bad practice, and can sometimes (often) give misleading impressions. See https://blog.datawrapper.de/dualaxis/ cheers, Rolf -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
Hi Ogbos,
The following code may get you close to what you want. I have used the
names of the columns in "dta" as it is less confusing for me. I think
you meant to request horizontal ablines as none appeared in the
example. In order to get the axis tick label "Sun" you would have to
increase the x axis limits beyond your data.
plot(dta$datetime,dta$B,type="l",axes=FALSE)
# this is out of range and doesn't appear
abline(h=-3,col="red")
axis(2)
plot(dta$datetime,dta$LAT,type="l",axes=FALSE)
abline(h=-3,col="red")
axis(2)
plot(dta$datetime,dta$BX,type="l",axes=FALSE)
abline(h=-3,col="red")
axis(2)
axis.POSIXct(1,dta$datetime,format="%a")
mtext("A1", side = 1, outer = TRUE, line = 2.2)
mtext("B1", side = 2, outer = TRUE, line = 2.2,at =0.2)
mtext("B2", side = 2, outer = TRUE, line = 2.2,at =0.5)
mtext("B2", side = 2, outer = TRUE, line = 2.2,at =0.8)
Jim
On Sat, May 2, 2020 at 2:47 AM Ogbos Okike <giftedlife2014 at gmail.com>
wrote:>
> Dear Contributors,
> I am trying to do a plot with multiple y-axis on a common x-axis. I have
> made some progress but I am having difficulties with datetime appearing on
> x-axis. Instead of as date, it appears as large numbers.
> ...
> If I do a simple plot of x and B, the x-axis will be fine, appearing as
> Thu, Fri, Sat, Sun.
> Please let me know what I am doing wrong with the multiple plot code above.
> I want the same Thu, Fri, Sat, Sun to appear on the common x-axis.
>
Ogbos Okike
2020-May-02 02:36 UTC
[R] Datetime misrepresented on x-axis of a multiple plot: Problem Fixd
Dear Jim and Rolf Turner,
This is great!!!!!!!!!!!!!!!!
The labels on x-axis are now correctly displayed. Jim, your singular code:
axis.POSIXct(1,dta$datetime,format="%a")
made all the required adjustment, making life quite easy for me.
The abline commands are indeed meant to draw a common vertical line across
the different panels. I have quickly achieved that with:
abline(v=as.POSIXct("1978-09-29 00:00:00"))
I have successfully added the rest of the data, displaying seven/more
variables on a single plot and using some common vertical lines to indicate
the variations in several solar wind plasma data/cosmic rays at Earth at
the time of coronal mass ejections from the sun. It makes an interesting
plot to those in our field.
Confusion or "misleading impressions" do not arise at all as the data
used
as well as date plotted are usually online for easy verification by
experts/anyone.
Many thanks for all your contributions.
Warmest regards
Ogbos
On Fri, May 1, 2020 at 11:47 PM Jim Lemon <drjimlemon at gmail.com> wrote:
> Hi Ogbos,
> The following code may get you close to what you want. I have used the
> names of the columns in "dta" as it is less confusing for me. I
think
> you meant to request horizontal ablines as none appeared in the
> example. In order to get the axis tick label "Sun" you would have
to
> increase the x axis limits beyond your data.
>
> plot(dta$datetime,dta$B,type="l",axes=FALSE)
> # this is out of range and doesn't appear
> abline(h=-3,col="red")
> axis(2)
> plot(dta$datetime,dta$LAT,type="l",axes=FALSE)
> abline(h=-3,col="red")
> axis(2)
> plot(dta$datetime,dta$BX,type="l",axes=FALSE)
> abline(h=-3,col="red")
> axis(2)
> axis.POSIXct(1,dta$datetime,format="%a")
> mtext("A1", side = 1, outer = TRUE, line = 2.2)
> mtext("B1", side = 2, outer = TRUE, line = 2.2,at =0.2)
> mtext("B2", side = 2, outer = TRUE, line = 2.2,at =0.5)
> mtext("B2", side = 2, outer = TRUE, line = 2.2,at =0.8)
>
> Jim
>
> On Sat, May 2, 2020 at 2:47 AM Ogbos Okike <giftedlife2014 at
gmail.com>
> wrote:
> >
> > Dear Contributors,
> > I am trying to do a plot with multiple y-axis on a common x-axis. I
have
> > made some progress but I am having difficulties with datetime
appearing
> on
> > x-axis. Instead of as date, it appears as large numbers.
> > ...
> > If I do a simple plot of x and B, the x-axis will be fine, appearing
as
> > Thu, Fri, Sat, Sun.
> > Please let me know what I am doing wrong with the multiple plot code
> above.
> > I want the same Thu, Fri, Sat, Sun to appear on the common x-axis.
> >
>
[[alternative HTML version deleted]]