Error: stat_smooth requires the following missing aesthetics

  geom_smooth(stat_smooth)에 상속된 aes()가 잘못됐을 경우 발생  http://stulp.gmw.rug.nl/ggplotworkshop/twocontinuousvariables.html  ::  12.1.1.1 inherite.aes ### 동력전달타입(drv)별 배기량(displ)이 도심 주행연비(cty)에 미치는 영향 p <- mpg %>% ggplot(aes(displ, cty, color=drv)) p + geom_point() + geom_smooth(method=”lm”, se=F) ### same above —- p + geom_point(inherit.aes=T) + geom_smooth(method=”lm”, se=F, inherit.aes=T) p + geom_point( data=mpg, aes(x=displ, y=cty, colour=drv)) + geom_smooth(data=mpg, Read more…

facet_wrap ylim

https://chrischizinski.github.io/rstats/using_geom_blank/ https://stackoverflow.com/questions/18046051/setting-individual-axis-limits-with-facet-wrap-and-scales-free-in-ggplot2 set.seed(666) foo.dat<-rbind(data.frame(group=”A”, x = runif(50), y=rnorm(50,mean=5)), data.frame(group=”B”, x = runif(50), y=rnorm(50,mean=5,sd=3)+20), data.frame(group=”C”, x = runif(50), y=rnorm(50,mean=5,sd=5)+30)) ## Plot it in ggplot ggplot() + geom_point(data=foo.dat,aes(x=x,y=y,colour=group),size=4) + facet_wrap(~group,scales=”free_y”) + ## using scales=”free_y” allows the y-axis to vary while keeping x-axis constant among plots theme_bw() ggplot() + geom_point(data = foo.dat, aes(x Read more…

stat_debug_group in ggpmisc

    library(ggplot2) library(ggpmisc) library(tibble) set.seed(4321) # generate artificial data my.data <- data.frame(x=1:100, y=(x+x^2+x^3)+rnorm(length(x),mean=0,sd=mean(x^3)/4) class=c(“A”,”B”), y2=y*c(0.5, 2), block=c(“a”,”a”,”b”,”b”)) old_theme <- theme_set(theme_bw()) p1 <- my.data %>% ggplot(aes(x, y)) p1 + stat_debug_group() p1 + geom_point() + stat_debug_group() p1 + geom_point() + stat_debug_group() p1 + geom_point() + stat_debug_group(summary.fun=as_tibble) p1 + geom_point() + stat_debug_group(summary.fun=function(x){x}) Read more…

cowplot package

https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html a simple add-on to ggplot2 Plot design provide a publication-ready theme for ggplot2 Arranging graphs into a grid plot_grid(plot.mpg, NULL, NULL, plot.diamonds, labels = c(“A”, “B”, “C”, “D”), ncol = 2) Generic plot annotations   ggdraw(plot.mpg) + draw_plot_label(“A”, size = 14) + draw_label(“DRAFT!”, angle = 45, size = 80, alpha = Read more…

ggplot2 default color palette

https://stackoverflow.com/questions/25211078/what-are-the-default-plotting-colors-in-r-or-ggplot2/25211125#25211125 https://stackoverflow.com/questions/8197559/emulate-ggplot2-default-color-palette https://stackoverflow.com/questions/33221794/separate-palettes-for-facets-in-ggplot-facet-grid ggplot 정보 확인 p <- ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species)) + geom_point() p summary(p) ggplot_build(p)$data ggplot_build(p)$layout ggplot_build(p)$plot ggplot 정보 확인 (data) ggplot_build(p)$data colour x y PANEL group shape size fill alpha stroke 1 #F8766D 5.1 1.4 1 1 19 1.5 NA NA 0.5 … 51 #00BA38 7.0 4.7 Read more…

ggplot Mouse click dblclick brush hover

https://stackoverflow.com/questions/30527977/ggplot2-how-to-differentiate-click-from-brush   library(shiny) library(ggplot2) runApp(shinyApp( ui = fluidPage( plotOutput(“plot”, click = “click”, brush = “brush”, dblclick = “dClick”) ), server = function(input, output, session) { output$plot <- renderPlot({ ggplot(mtcars, aes(wt, mpg)) + geom_point() }) observeEvent(input$brush, { cat(“brushed\n”) }) observeEvent(input$click, { cat(“clicked\n”) }) observeEvent(input$dClick, { cat(“Doublie clicked\n”) }) } ))   Read more…

shinycssloaders

https://github.com/andrewsali/shinycssloaders #devtools::install_github(‘andrewsali/shinycssloaders’) #options(spinner.color=”#0dc5c1″) #options(spinner.size=my_size) library(shinycssloaders) … withSpinner(plotOutput(“my_plot”)) plotOutput(“my_plot”) %>% withSpinner() plotOutput(“my_plot”) %>% withSpinner(color=”#0dc5c1″) … ex> library(shiny) library(shinycssloaders) library(ggplot2) options(spinner.color.background=”#F5F5F5″) ui <- fluidPage( checkboxInput(“show_plot”, “Show tables”, value=TRUE), actionButton(“redraw_plot”,”Re-draw”), fluidRow( column(width=6, plotOutput(“plotWithSpin”) %>% withSpinner(type=7) ) ) ) server <- function(input, output,session) { output$plotWithSpin <- renderPlot({ shiny::validate(need(input$show_plot,”Show table is unchecked. Check to see Read more…

ggpmics

http://www.ggplot2-exts.org/gallery/   https://bitbucket.org/aphalo/ggpmisc http://docs.r4photobiology.info/ggpmisc/index.html R package ‘ggpmisc’  The package manual describes in more detail the items listed above, and the vignette gives several examples of plots produced with the package. Please, see the web site r4photobiology for details and update notices. Other packages, aimed at easing photobiology-related calculations including the quantification of biologically effective radiation in Read more…

package :: ggpmisc

http://www.ggplot2-exts.org/ggpmisc.html https://cran.r-project.org/web/packages/ggpmisc/index.html   : Miscellaneous Extensions to ‘ggplot2’   User Guide: 1 Plotting User Guide: 2 Density-based filtering User Guide: 3 Debugging ggplots User Guide: 4 Manipulation of plot layers  

Shiny ggplot2

http://databall.co/shiny/shinyggplot/ https://github.com/AlienDeg/shinyexplorer       Error in get: invalid first argument object ‘Sepal.Length’ not found Stack trace (innermost first): Error in : Aesthetics must be either length 1 or the same as the data (150): x, y  

ggpairs() vs. 함수활용 scatterPlot

https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/pairs.html   DATA Advertising <- read.table(“http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv”, header=T, sep=”,”) ggpairs() library(GGally) ggpairs(Advertising[, c(2:4)])   함수활용 scatterPlot # scatter plot matrix # put histograms on the diagonal panel.hist <- function(x, …) { usr <- par(“usr”); on.exit(par(usr)) par(usr = c(usr[1:2], 0, 1.5) ) h <- hist(x, plot = FALSE) breaks <- h$breaks; nB Read more…

ggplot2 Error :: Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) : invalid graphics state

에러내용: Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) : invalid graphics state 해결방법 dev.off()   http://statkclee.github.io/r-novice-inflammation/04-cond-kr.html dev.off 실행하지 않고 pdf함수를 여러번 실행하게 되면, 가장 최근에 열린 파일에 그래프가 저장된다. 하지만, 연결(connection)이 닫혀지지 않아서, 이전 pdf 파일은 열 수가 없다. 이 상황을 벗어나기 위해서, 모든 pdf 연결(connection)을 끊을 때, dev.off를 실행할 필요가 있다.  dev.cur 함수를 사용해서 현재 Read more…

ggplot2 error – Error in fetch(key) : lazy-load database

http://stackoverflow.com/questions/30424608/error-in-fetchkey-lazy-load-database > library(ggplot2) Error in get(Info[i, 1], envir = env) : lazy-load database ‘/Users/onesixx/R/x86_64-pc-linux-gnu-library/3.2/ggplot2/R/ggplot2.rdb’ is corrupt In addition: Warning message: In get(Info[i, 1], envir = env) : internal error -3 in R_decompress1 Error: package or namespace load failed for ‘ggplot2’ R이 해당 package의 압축을 풀어낼때 발생.  침착하게 R을 restart.  해당 Read more…

ggplot2 additional legend

    library(ggplot2) mtcar <- data.frame(mtcars, cl=factor(mtcars$cyl)) p <- ggplot(mtcar, aes(mpg, wt)) + geom_point() p <- p+ geom_point(data=mtcar[mtcar$cl==8,], mapping=aes_string(x=”mpg”, y=”wt”), color=’red’, shape=0, size=3)+ geom_point(aes(fill=””)) + guides(fill=guide_legend(“Class A”, override.aes=list(size=3, shape=0, color= “red”), order=1)) p <- p+ geom_point(data=mtcar[mtcar$cl==4,], mapping=aes_string(x=”mpg”, y=”wt”), color=’blue’,shape=2, size=3)+ geom_point(aes(size=””)) + guides(size=guide_legend(“Class B”, override.aes=list(size=3, shape=2, color=”blue”), order=2)) p dummy Read more…

annotation

    library(data.table) set.seed(666) DT <- data.table( A=rep(c(“a”,”b”,”c”),each=2), B=c(1:3), C=sample(6), D=sample(6)) setkey(DT, A)   DT %>% ggplot(aes(A,B)) + geom_point()+ annotate(geom=’text’, label=”Top right”, x= Inf, y= Inf, hjust= 1.1, vjust= 2) + annotate(geom=’text’, label=”Bottom right”, x= Inf, y=-Inf, hjust= 1.1, vjust=-1) + annotate(geom=’text’, label=”Bottom left” , x=-Inf, y=-Inf, hjust=-0.1, vjust=-1) + Read more…