tidyverse filter() statement make plot display horizontal?

When I use filter statement with ggplot2 the plot display horizontal?

library(data.table)
library(tidyverse)
sidc<-fread("http://sidc.be/silso/DATA/SN_d_tot_V2.0.csv",sep = ';')
colnames(sidc) <- c("Year","Month","Day", "Fdate","Spots", "Sd","Obs" ,"Defin"  )
sidc$Ymd <- as.Date(paste(sidc$Year, sidc$Month, sidc$Day, sep = "-"))
sidc<-sidc[Year>=2014,]

## Plot columns print hoizontal with filter statement:
A <- sidc %>% filter(Spots >=21 & Spots <=130)
ggplot(data=A) + geom_col(aes(x=Ymd,y=Spots)) + ggtitle("Plot with filter statement")

## But print as expected with out filter statement:
ggplot(data=sidc,aes(x=Ymd,y=Spots)) + geom_col() + ggtitle("Plot without filter statment")

Below is what I get running your code.

library(data.table)
#> Warning: package 'data.table' was built under R version 3.5.3
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 3.5.3
#> Warning: package 'tibble' was built under R version 3.5.3
#> Warning: package 'tidyr' was built under R version 3.5.3
#> Warning: package 'readr' was built under R version 3.5.3
#> Warning: package 'purrr' was built under R version 3.5.3
#> Warning: package 'dplyr' was built under R version 3.5.3
#> Warning: package 'stringr' was built under R version 3.5.3
#> Warning: package 'forcats' was built under R version 3.5.3
sidc<-fread("http://sidc.be/silso/DATA/SN_d_tot_V2.0.csv",sep = ';')
colnames(sidc) <- c("Year","Month","Day", "Fdate","Spots", "Sd","Obs" ,"Defin"  )
sidc$Ymd <- as.Date(paste(sidc$Year, sidc$Month, sidc$Day, sep = "-"))
sidc<-sidc[Year>=2014,]

## Plot columns print hoizontal with filter statement:
A <- sidc %>% filter(Spots >=21 & Spots <=130)
ggplot(data=A) + geom_col(aes(x=Ymd,y=Spots)) + ggtitle("Plot with filter statement")


## But print as expected with out filter statement:
ggplot(data=sidc,aes(x=Ymd,y=Spots)) + geom_col() + ggtitle("Plot without filter statment")

Created on 2019-12-08 by the reprex package (v0.3.0.9000)

And here is the top of my sessionInfo()

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

1 Like

Thanks for taking the time to respond.
The problem appears to resolved after upgrading to the lastest version of RStudio.

Thanks again,
David

If your question's been answered (even if by you), would you mind choosing a solution? (See FAQ below for how).

Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.

Thanks

Well, I spoke to soon, I updated to the lastest RStudio(1.2.5019) and building tidyverse from github but that didn't solve the problem. The problem maybe be with the "geom_col()" function, because when I use geom_point() or geom_line() the display the plot with the correct orientation ? Here's my setup:

sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

library(tidyverse)
-- Attaching packages ----------------------------------
tidyverse 1.3.0.9000 --
v ggplot2 3.2.1.9000 v purrr 0.3.3
v tibble 2.1.3 v dplyr 0.8.3
v tidyr 1.0.0 v stringr 1.4.0
v readr 1.3.1 v forcats 0.4.0
-- Conflicts --------------------------

Can you make a Reprex out of your code? It will then be run in its own environment and you can post the exact inputs and outputs. In the post below, you just have to do the Your Final Reprex section.

Thanks again for following up. What should I select for "target venue"?

I don't know whether the Target Venue matters much. I always just leave it on GitHub. Someone here may be shocked.:grinning:

Rplot

A couple of notes: (1) I seems to only effect geom_col() feature, also It seems to only effect the Spots filed i.e If I "filter" on "Year: or "Ymd "the plot runs without error? and finally I have a linux box running R-3.4.4 and the plot runs fine. Here's some output for str() and sessionInfo().

Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame':
Year : num 1850 1850 1850 1850 1850 1850 1850 1850 1850 1850 ... Month: chr "01" "01" "01" "01" ...
Day : chr "01" "02" "03" "04" ... Fdate: num 1850 1850 1850 1850 1850 ...
Spots: num 253 162 217 99 108 203 142 76 95 124 ... Sd : chr " 19.6" " 15.5" " 18.1" " 12.0" ...
Obs : chr " 1" " 1" " 1" " 1" ... Defin: num 1 1 1 1 1 1 1 1 1 1 ...
$ Ymd : Date, format: "1850-01-01" ...

R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base

other attached packages:
[1] forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3
[4] purrr_0.3.3 readr_1.3.1 tidyr_1.0.0
[7] tibble_2.1.3 ggplot2_3.2.1.9000 tidyverse_1.3.0.9000

loaded via a namespace (and not attached):
[1] Rcpp_1.0.3 cellranger_1.1.0 pillar_1.4.2
[4] compiler_3.6.1 dbplyr_1.4.2 tools_3.6.1
[7] zeallot_0.1.0 packrat_0.5.0 lubridate_1.7.4
[10] jsonlite_1.6 lifecycle_0.1.0 nlme_3.1-140
[13] gtable_0.3.0 lattice_0.20-38 pkgconfig_2.0.3
[16] rlang_0.4.2 reprex_0.3.0 cli_1.1.0
[19] DBI_1.0.0 rstudioapi_0.10 curl_4.3
[22] haven_2.2.0 withr_2.1.2 xml2_1.2.2
[25] httr_1.4.1 fs_1.3.1 generics_0.0.2
[28] vctrs_0.2.0 hms_0.5.2 grid_3.6.1
[31] tidyselect_0.2.5 glue_1.3.1 R6_2.4.1
[34] readxl_1.3.1 modelr_0.1.5 magrittr_1.5
[37] backports_1.1.5 scales_1.1.0 rvest_0.3.5
[40] assertthat_0.2.1 colorspace_1.4-1 stringi_1.4.3
[43] munsell_0.5.0 broom_0.5.2 crayon_1.3.4

This is happening because you are using the development version of ggplot2 which now supports horizontal column plots natively and automatically chooses the orientation, it seems like in this case is not giving the expected result so you would have to manually set it.

library(data.table)
library(tidyverse)

sidc <- fread("http://sidc.be/silso/DATA/SN_d_tot_V2.0.csv",sep = ';')
colnames(sidc) <- c("Year","Month","Day", "Fdate","Spots", "Sd","Obs" ,"Defin"  )
sidc$Ymd <- as.Date(paste(sidc$Year, sidc$Month, sidc$Day, sep = "-"))
sidc<-sidc[Year>=2014,]

## Plot columns print hoizontal with filter statement:
A <- sidc %>%
  filter(Spots >=21 & Spots <=130)

ggplot(data=A, mapping = aes(x=Ymd,y=Spots)) +
  geom_col(orientation = "x") +
  ggtitle("Plot with filter statement")

If you think this behaviour is odd and should be changed, I suppose you could file a GitHub issue and make your case.

Thanks for the explanation, that makes sence. I took the easy way out and removed and re-installing ggplot2 and now its working as expected..... It appears that Kaggle is the development version also:
https://www.kaggle.com/davidjackson0513/fun-w-stats/edit/run/24901017

Thanks again,