wilkelab / cowplot Goto Github PK
View Code? Open in Web Editor NEWcowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
Home Page: https://wilkelab.org/cowplot/
cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
Home Page: https://wilkelab.org/cowplot/
Hi,
I was just wondering if you had a method/technique to have a common legend across multiple plots using the cowplot library? This is similar to the idea listed here:
https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
Thanks,
Fong
I used plot_grid to generate a 12 (3 row by 4 col) panel figure, however, I was not able to get the alignment correct for the 3rd row after "hjust = -6".
Thank you for your time and help!
PP1 <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + geom_point(size = 2.5)
....
PP12 <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + geom_point(size = 2.5)
plot3by4 <- plot_grid(PP1, PP2, PP3, PP4, PP5, PP6, PP7, PP8, PP9, PP10, PP11, PP12, labels=c("A", "B", "C", "D","E", "F", "G", "H", "I", "J", "K", "L"), ncol = 4, nrow = 3, align = 'v', hjust=-6, label_size=17)
save_plot("plot3by4.png", plot3by4, ncol = 4, nrow = 3, base_aspect_ratio = 1 )
Hi,
I am using RSTUDIO and knitr. I use cowplot to put graphs in a custom grid. If a plot is present in the RSTUDIO UI, then not plot shows in the produced pdf. If I erase the plots (effectively using dev.off()) then the plots are now knitted.
Doing the grid manually, using grid, does not have the same issue. I like cowplot's clarity though. good job
Is this is related to #24?
Thanks
sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C LC_TIME=English_United Kingdom.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggrepel_0.6.3 plotly_4.5.6 cowplot_0.7.0 shiny_0.14.2 xtable_1.8-2 readxl_0.1.1 tidyr_0.6.0 stringr_1.1.0 dplyr_0.5.0
[10] lubridate_1.6.0 gridExtra_2.2.1 reshape2_1.4.2 scales_0.4.1 ggplot2_2.2.0 knitr_1.15
loaded via a namespace (and not attached):
[1] Rcpp_0.12.7 magrittr_1.5 munsell_0.4.3 viridisLite_0.1.3 colorspace_1.3-0 R6_2.2.0 httr_1.2.1
[8] plyr_1.8.4 tools_3.3.2 gtable_0.2.0 DBI_0.5-1 htmltools_0.3.5 lazyeval_0.2.0 assertthat_0.1
[15] digest_0.6.10 tibble_1.2 purrr_0.2.2 RColorBrewer_1.1-2 htmlwidgets_0.8 base64enc_0.1-3 evaluate_0.10
[22] mime_0.5 labeling_0.3 stringi_1.1.2 jsonlite_1.1 httpuv_1.3.3
A (very) minor suggestion to set the strip background colour to white as default. I love the clean overall look of the cowplot theme, but always end up manually changing the grey strip background whenever I am using cowplot with a faceted or gridded plot:
theme(strip.background = element_rect(fill = "white"))
Hardly a game changer, but I do think it better suits the general ethos of the theme.
some plotting functions use normal plotting instead of grid-based plotting.
it would be useful to be able to pass recordedPlot
instances or functions that draw on the R device to plot_grid
.
currently I use this function, which could be added to cowplot
internally:
gwrap_plot <- function(x) {
tree <- grid::grid.grabExpr(gridGraphics::grid.echo(x))
u <- grid::unit(1, 'null')
gtable::gtable_col(NULL, list(tree), u, u)
}
gridGraphics::grid.echo
accepts either a function like function() plot(1:10)
or a recordedPlot
object, and we could do the same.
This would lead to neat code like this:
p1 <- ggplot(...) + ...
p2 <- function() plot(1:10)
plot_grid(p1, p2)
an alternative would be to stay explicit, but export above function (with a better name than gwrap_plot
)
The vignette currently has
%\VignetteIndexEntry{Vignette Title}
Which should be edited to be an appropriate and relevant title for the vignette. Currently CRAN displays the vignette with the name "Vignette Title".
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_line(colour = "blue")+
theme(axis.title.x=element_blank(),axis.title.y=element_blank())
ggdraw(switch_axis_position(p1, axis = 'x'))
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_line(colour = "blue")+
theme(axis.title.x=element_blank(),axis.title.y=element_blank())
ggdraw(switch_axis_position(p1, axis = 'y'))
Below code illustrates the problem. When 2 plots merged using plot_grid, is it possible to make X axis to align. i.e.: xlims are derived from 2 plots.
library(ggplot2)
library(cowplot)
df1 <- iris
df2 <- iris
df2 <- df2[df2$Sepal.Length <6, ]
df2$Sepal.Width <- df2$Sepal.Width*10000
#ggplot
gg1 <- ggplot(df1, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) + geom_line()
gg2 <- ggplot(df2, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) + geom_line()
#merge
plot_grid(gg1, gg2, labels = c("A", "B"), align = "v",ncol = 1)
When I try install.packages("cowplot") on my Mac, I get this response:
package ‘cowplot’ is available as a source package but not as a binary
Warning in install.packages :
package ‘cowplot’ is not available (for R version 3.1.2)
Installing from GitHub works fine.
Hi there,
thanks for this useful package. Since one of the more recent updates (before updating to the current version I used 0.6.2) I'm experiencing some weird issues with the interaction between plot_grid()
, theme_set()
, and a theme of mine that modifies axis.text.x
.
Everything works great as long as I don't use theme_set()
.
Works as expected:
library("ggplot2")
library("cowplot")
df <- data.frame(
gp = factor(rep(letters[1:3], each = 10)),
y = rnorm(30)
)
test <- ggplot(df, aes(gp, y)) +
geom_point() +
theme_classic() +
theme(axis.text.x = element_text())
plot_grid(test)
Throws an error:
theme_set(theme_classic() + theme(axis.text.x = element_text()))
test2 <- ggplot(df, aes(gp, y)) +
geom_point()
plot_grid(test2)
Error in element_grob.element_text(el, ...) :
Text element requires non-NULL value for 'angle'.
If I set angle
I get another error:
theme_set(theme_classic() + theme(axis.text.x = element_text(angle = 0)))
plot_grid(test2)
Error in if (debug) { : Argument has length 0
Now, if I set debug = FALSE
it finally works, but I get some weird numbering on the bottom margin.
This is my session info:
Session info ---------------------------------------------------------------------------------------------------------------------------
setting value
version R version 3.3.2 (2016-10-31)
system x86_64, linux-gnu
ui RStudio (1.0.136)
language de_DE
collate de_DE.UTF-8
tz <NA>
date 2017-02-24
Packages -------------------------------------------------------------------------------------------------------------------------------
package * version date source
assertthat 0.1 2013-12-06 CRAN (R 3.3.0)
colorspace 1.2-6 2015-03-11 CRAN (R 3.3.0)
cowplot * 0.7.0 2017-02-24 Github (wilkelab/cowplot@78336ac)
devtools 1.12.0 2016-06-24 CRAN (R 3.3.1)
digest 0.6.11 2017-01-03 CRAN (R 3.3.2)
ggplot2 * 2.2.1 2016-12-30 CRAN (R 3.3.2)
gtable 0.2.0 2016-02-26 CRAN (R 3.3.0)
labeling 0.3 2014-08-23 CRAN (R 3.3.0)
lazyeval 0.2.0 2016-06-12 CRAN (R 3.3.0)
memoise 1.0.0 2016-01-29 CRAN (R 3.3.0)
munsell 0.4.3 2016-02-13 CRAN (R 3.3.0)
plyr 1.8.4 2016-06-08 CRAN (R 3.3.0)
Rcpp 0.12.9.3 2017-02-24 Github (RcppCore/Rcpp@8bf15c0)
scales 0.4.1 2016-11-09 CRAN (R 3.3.2)
tibble 1.2 2016-08-26 CRAN (R 3.3.1)
withr 1.0.2 2016-06-20 CRAN (R 3.3.1)
I realize cowplot is publication-focused,and this isn't usually something you see in publications, but it would be useful if there was a "main title" attribute you could set describing what the plots are. This would just be centered text above all the plots
"there are important but subtle difference" --> differences.
I would like to switch the position of the y axis to the right hand side of my plot using
ggdraw(switch_axis_position(p1, axis = 'y'))
However, my plot is facetted using facet_grid, and so I get the error "Error in switch_yaxis_position(gt, theme, keep.y): Can only switch axes on plots with exactly one panel."
Is there any way to work around this? Just for clarification, I'd like my y axis to be on the far right hand side of my facetted plot.. Thank you!
Thanks for cowplot. It's really been a game changer for making publication quality plots in R.
Would it be possible to implement finer control for subplot alignment in plot_grid? For example, I have a grid like the following
XXXXXXXX XX
XXXXXXXX XX
XXXXXXXX XX
I would like to separately horizontally align the plots in the left column, horizontally align the plots in the left column, and vertically align the plots in each row.
When I use align = 'hv'
, the end result is that everything has both the same height and width, which is not what I'd like.
Thanks.
Trying to load cowplot fails on R 3.2.3.
Both commands
install.packages("cowplot")
and
devtools::install_github("wilkelab/cowplot")
compile fine.
Upon loading with require(cowplot) I get the following error:
Loading required package: cowplot
Error : .onAttach failed in attachNamespace() for 'cowplot', details:
call: element_text(family = font_family, face = "plain", colour = "black",
error: unused arguments (margin = ggplot2::margin(), debug = FALSE)
Attempt to install in RStudio Version 0.99.441 Win 8.1
> install.packages("cowplot")
Warning in install.packages :
package ‘cowplot’ is not available (for R version 3.2.0)
May be lag in CRAN.
Thanks really for this great packages.
I have one suggestion.
ggplot2
uses gtable
as its backend structure for grid-based plot generation.
gtable
allows to arrange multiple plots keeping their panel sizes aligned.
Here are some examples, sorry for Japanese text, but the codes are easy to read :)
http://rpubs.com/kohske/84415
So, it is really excellent if cowplot
support this kind of arrangement powered by gtable
.
Then we can get grid plots with consistent size for all panes and axes.
Feel free please to ask anything, if you have any question regarding usage of gtable
, as I'm one of the committer of the package.
Thanks again.
-k
I had the following error
Error in Ops.unit(grid::unit(1, "npc"), grob$children[[1]]$y) :
both operands must be units
Code I used:
mtcars2 <- mtcars[1:15, ]
mtcars2$name <- row.names(mtcars2)
p3 <- ggplot(mtcars2, aes(x = name, y = mpg, fill = name)) +
geom_bar(stat = 'identity', position = "identity") +
scale_y_reverse() +
guides(fill = FALSE) +
theme(axis.text.x = element_blank(), axis.ticks.x = element_blank())
ggdraw(switch_axis_position(p3, axis = 'x'))
Any idea what went wrong? Thanks!
Tung
Is it possible to increase the spacing between the plots in plot_grid?
The spacing I found discussed in the docs all seemed to refer to labels within plots.
I am using data sourced from the PDF on this page. Specifically: table 4 on page 14. You can see that there are three categories of data in this table, which I am trying to combine in a single plot. However, the legend text has different lengths which causes the legends to look ugly - because they are all centred and it does not seem to be possible to change this. I have try to left-justify the legend using legend.justification = c(0,0)
in my call to ggplot()
but this appears to be overriden by plot_grid()
.
This package is fantastic! I like the switch axis functionality but would like to be able to combine it with vertical grid alignment and it doesn't seem to work:
plot.mpg1 <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) +
geom_point(size=2.5)
plot.mpg2 <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) +
geom_point(size=2.5)
plot.mpg2 <- switch_axis_position(plot.mpg2, "y")
plot_grid(plot.mpg1, plot.mpg2, nrow = 2, align = "v")
Hi there,
Big fan of cowplot - thanks for putting it out there. Wondering if there's a way to squish the y axis title margins when needed? In the attached figure, one can see that too much space is being allocated for the titles (sorry, obligated to obfuscate the words themselves, but i think it's good enough to understand the issue). I've tried setting theme(y.axis.title(margin = margin(0,0,0,0)) when constructing the constitutent plots, but it has no effect on cowplot's output. Any help would be greatly appreciated!
Thanks,
Alex
I have just realised that ggplot2 are not plotting axis lines by default (tidyverse/ggplot2#1565 and tidyverse/ggplot2#1567). I imagine this is a mistake, and a work around for now is to issue
theme_set(theme_cowplot() + theme(axis.line.x = element_line(), axis.line.y = element_line()))
after loading the cowplot library.
The problem seems to be just with ggplot2 2.1.0
I've noticed that by simply loading the package, the ggplot2 theme is changed
https://github.com/wilkelab/cowplot/blob/master/R/setup.R#L5-L8
I feel like this is not a good practice, and it's a bit annoying and unpredictable. Would be nice if there was an explicit way to revert back, and even better if attaching the package would not have this side effect.
Whenever I tried to use cowplot
, it changes my theme which is devastating, since a lot of effort was given to get the appearances.
In general, I really like the minimalistic theme settings of cowplot. However, I also like having the major grid, and I find it easier to set it up globally at the beginning of my scripts. It works fine for single plots, but seems to confuse plot_grid
, which plots a grid on top of the grid plot (and not only on top of single plots).
Here is a better explanation with simple iris plots:
library(cowplot)
i1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point()
i2 <- ggplot(iris, aes(Petal.Length, Petal.Width)) +
geom_point()
i3 <- ggplot(iris, aes(Sepal.Length, Petal.Length)) +
geom_point()
i4 <- ggplot(iris, aes(Sepal.Width, Petal.Width)) +
geom_point()
plot_grid(i1, i2, i3, i4)
This works perfectly. I can use background_grid
to add the grid manually to each plot:
i1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() + background_grid(major = 'xy')
i2 <- ggplot(iris, aes(Petal.Length, Petal.Width)) +
geom_point() + background_grid(major = 'xy')
i3 <- ggplot(iris, aes(Sepal.Length, Petal.Length)) +
geom_point() + background_grid(major = 'xy')
i4 <- ggplot(iris, aes(Sepal.Width, Petal.Width)) +
geom_point() + background_grid(major = 'xy')
plot_grid(i1, i2, i3, i4)
Still works as expected. Now if I update the theme to add the major grid to all plots:
theme_update(panel.grid.major = element_line(colour = "grey90",
size = 0.2))
i1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point()
i2 <- ggplot(iris, aes(Petal.Length, Petal.Width)) +
geom_point()
i3 <- ggplot(iris, aes(Sepal.Length, Petal.Length)) +
geom_point()
i4 <- ggplot(iris, aes(Sepal.Width, Petal.Width)) +
geom_point()
plot_grid(i1, i2, i3, i4)
I get the additional grid on top of the combined plot. I didn't attach a picture of it, so please let me know if it's only happening on my computer (note: using Debian Testing, with R 3.3.2, ggplot 2.2.1 and cowplot 0.7.0).
Thanks for looking at this issue!
Hello there,
I followed https://cran.r-project.org/web/packages/cowplot/cowplot.pdf
to create some plotgrids files.
However, in the pdf or svg files I get the dimensions do not seem to be computed based on definitions for the "base plot" size or ratios. (This is what the documentation suggested to me, and looked very practical to me.)
It looks like the definitions are applied to the final entire plotgrid.
In the following example:
require(dplyr)
require(ggplot2)
require(cowplot)
tmp_plot1 <- data.frame(a=1:100, b=1:100) %>%
ggplot(aes(x=a, y=b)) + geom_line()
tmp_plot2 <- tmp_plot1
tmp_plotgrid <- plot_grid(tmp_plot1, tmp_plot2, labels = c('A', 'B'))
save_plot("2-horizontal-plots_height-NA_aspect-1.pdf", tmp_plotgrid, base_aspect_ratio = 1)
save_plot("2-horizontal-plots_height-NA_aspect-2.pdf", tmp_plotgrid, base_aspect_ratio = 2)
save_plot("2-horizontal-plots_height-NA_aspect-3.pdf", tmp_plotgrid, base_aspect_ratio = 3)
tmp_plotgrid <- plot_grid(tmp_plot1, tmp_plot2, labels = c('A', 'B'), ncol = 1)
save_plot("2-vertical-plots_height-NA_aspect-0.5.pdf", tmp_plotgrid, base_aspect_ratio = 0.5)
save_plot("2-vertical-plots_height-NA_aspect-1.pdf", tmp_plotgrid, base_aspect_ratio = 1)
save_plot("2-vertical-plots_height-NA_aspect-2.pdf", tmp_plotgrid, base_aspect_ratio = 2)
save_plot("2-vertical-plots_height-NA_aspect-3.pdf", tmp_plotgrid, base_aspect_ratio = 3)
I've tried the reverse of the solutions suggested in #11, and the plots end up with parts cut off. Thanks in advance!
While answering a question on stackoverflow I found that the justification for the axis labels seems to bug out if the angle is 90 degrees.
A simple (hackish) fix is to add a small amount to the degrees.
Example:
library(reshape2)
library(ggplot2)
library(cowplot)
# some toy data
set.seed(1)
mydata <- mtcars[, c(1, 3, 4, 5, 6, 7)]
# to show difference in justification better, make names of unequal length
names(mydata) = paste0(sample(c("mtcars_", ""), 6, replace = TRUE), names(mydata))
cormat <- round(cor(mydata), 2)
melted_cormat <- melt(cormat)
head(melted_cormat)
plot <- ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) +
geom_tile() +
theme_bw(base_size=20) + xlab("") + ylab("") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 0.5))
ggdraw(switch_axis_position(plot, 'x'))
This gives weird labels (you can mess around with vjust
and hjust
).
But this version gives the desired result:
plot <- ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) +
geom_tile() + theme_bw(base_size=20) + xlab("") + ylab("")+
theme(axis.text.x=element_text(angle=90 + 1e-09, hjust = 0, vjust=1)) +
coord_equal(expand = 0)
ggdraw(switch_axis_position(plot, 'x'))
Thanks for a great package!
Hello
When I run this code :
ggplot(data.frame(a=1:5, b = 1:5), aes(a,b)) + geom_point() + labs(title = "é")
No problem
But this one make a Warning :
toto<- ggplot(data.frame(a=1:5, b = 1:5), aes(a,b)) + geom_point() + labs(title = "é")
plot_grid(toto)
Why ?
I use this in a knitr file and can not ignore them
Thank you for any help
I quite like the cowplot theme, but I ran into a problem modifying the theme to remove both axis.text.y
and .x
using theme(axis.text = element_blank())
.
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) + geom_point() + theme(axis.text = element_blank())
library(cowplot)
#> Warning: `legend.margin` must be specified using `margin()`. For the old
#> behavior use legend.spacing
#>
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#>
#> ggsave
ggplot(mtcars, aes(wt, mpg)) + geom_point() + theme(axis.text = element_blank())
#> Error in element_grob.element_text(el, ...) :
#> Text element requires non-NULL value for 'angle'.
This worked at one point, but unfortunately I don't know the version numbers when it did (sometime around the end of July, if that helps). The work around is to specify both axis.text.y
and axis.text.x
directly.
devtools::session_info()
#> Session info -------------------------------------------------------------
#> setting value
#> version R version 3.3.1 (2016-06-21)
#> system x86_64, darwin13.4.0
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> tz America/New_York
#> date 2016-11-04
#> Packages -----------------------------------------------------------------
#> package * version date source
#> assertthat 0.1 2013-12-06 CRAN (R 3.3.0)
#> clipr 0.2.1 2016-06-23 cran (@0.2.1)
#> colorspace 1.2-6 2015-03-11 CRAN (R 3.3.0)
#> cowplot * 0.6.2 2016-04-20 CRAN (R 3.3.0)
#> curl 2.1 2016-09-22 cran (@2.1)
#> devtools 1.12.0.9000 2016-10-27 Github (hadley/devtools@f0d1c7e)
#> digest 0.6.10 2016-08-02 cran (@0.6.10)
#> evaluate 0.10 2016-10-11 cran (@0.10)
#> formatR 1.4 2016-05-09 CRAN (R 3.3.0)
#> ggplot2 * 2.1.0.9001 2016-11-03 Github (hadley/ggplot2@9869339)
#> git2r 0.15.0 2016-05-11 CRAN (R 3.3.0)
#> gtable 0.2.0 2016-02-26 CRAN (R 3.3.0)
#> htmltools 0.3.5 2016-03-21 CRAN (R 3.3.0)
#> httr 1.2.1 2016-07-03 cran (@1.2.1)
#> knitr 1.14 2016-08-13 cran (@1.14)
#> lazyeval 0.2.0 2016-06-12 CRAN (R 3.3.0)
#> magrittr 1.5 2014-11-22 CRAN (R 3.3.0)
#> memoise 1.0.0 2016-01-29 CRAN (R 3.3.0)
#> munsell 0.4.3 2016-02-13 CRAN (R 3.3.0)
#> plyr 1.8.4 2016-06-08 CRAN (R 3.3.0)
#> R6 2.2.0 2016-10-05 cran (@2.2.0)
#> Rcpp 0.12.7 2016-09-05 cran (@0.12.7)
#> reprex 0.0.0.9001 2016-11-04 Github (jennybc/reprex@826ddf4)
#> rmarkdown 1.1 2016-10-16 cran (@1.1)
#> scales 0.4.0.9003 2016-11-03 Github (hadley/scales@d58d83a)
#> stringi 1.1.1 2016-05-27 CRAN (R 3.3.0)
#> stringr 1.1.0 2016-08-19 cran (@1.1.0)
#> tibble 1.2 2016-08-26 cran (@1.2)
#> withr 1.0.2 2016-06-20 cran (@1.0.2)
I just tried to use cowplot::plot_grid
with a fork of this repo, changing the minimum version of R to 3.1.2. It worked. Admittedly, I did not try all the functions of cowplot, but it worked enough for my case, and I avoided upgrading R to 3.2, which might break some other things. So I was just wondering, is the 3.2 version really necessary, or is it possible to relax this requirement so that older versions might profit from this package?
Cheers
Hi there,
I asked this on stackoverflow (http://stackoverflow.com/questions/39530116/arrange-multiple-ggplots-but-avoid-squishing-due-to-axis-label/39532814), but wasn't able to resolve it, so am coming here.
I've been using cowplot::plot_grid to arrange multiple ggplot figures, which has been working great in general. However, I've now run into a problem where I want to include the x axis labels on just the bottom plot, but it ends up squishing all the plots (with align = "vh") or the lowest plot (with align = "v") in the column.
Is there any way to perhaps extract the x-axis label grob, and add that as a another plot in plot_grid (perhaps similar to https://github.com/wilkelab/cowplot/blob/master/vignettes/shared_legends.Rmd for legends)? Or another approach?
Thanks very much!
library(ggplot2)
library(cowplot)
a = data.frame(a = rnorm(100), b = rep(c("laskjdflsakjd", "cknmlokfjoweijojf"), 50))
b = data.frame(a = rnorm(100), b = rep(c("laskjdflsakjd", "cknmlokfjoweijojf"), 50))
c = data.frame(a = rnorm(100), b = rep(c("laskjdflsakjd", "cknmlokfjoweijojf"), 50))
aplot = ggplot(a, aes(x=b, y=a)) + geom_bar(stat = "identity")
bplot = ggplot(b, aes(x=b, y=a)) + geom_bar(stat = "identity")
cplot = ggplot(c, aes(x=b, y=a)) + geom_bar(stat = "identity") + theme(axis.text.x = element_text(angle=90, vjust = 0.5))
plot_grid(aplot + theme(axis.text.x = element_blank(), axis.title.x = element_blank()),
bplot + theme(axis.text.x = element_blank(), axis.title.x = element_blank()),
cplot, ncol = 1, align = "vh")
I've just started to use ggplot2 and cowplot so I might be wrong.
For some reason the color of the axis.line element cannot be changed.
I can change the color of other elements, even axis.ticks.
I personally prefer (and I believe some journals demand) that the figure number is printed on the figure. My reason is that it creates less confusion when figures are bundled into one pdf or printed. It is possible to do this now but only by combinations of ggdraw(), draw_plot, and draw_label().
I suggest that the argument plot_label = NULL
is added to the plot_grid(), the addition to the function body could be
if(!isnull(plot_label)){
p <- p + draw_label(plot_label, 1, 0, hjust = 1, vjust = 0)
}
The x, y, hjust, and vjust could also be given as arguments to the function, but I think that would be too much.
I'm having an issue trying to reduce the margins around my plots in plot_grid.
Using align='hv'
perfectly aligns and scales my axes but leaves unwanted whitespace between plots, like so:
Whereas defining margins within each plot reduces the whitespace as desired, but leaves the y axes misaligned due to their different sizes, like so:
I've included a reproducible example below.
Thanks in advance for any help on this matter -- I appreciate the work you're doing with cowplot.
#my data
mydat <- structure(list(sex = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("F", "M"), class = "factor"),
treat = structure(c(3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L,
2L, 2L), .Label = c("nylon", "stab", "no treat"), class = "factor"),
pre_post = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L), .Label = c("pre", "post"), class = "factor"),
meanPO = c(0.271111104444444, 2.32355555333333, 0.288222211111111,
0.15, 0.254444435555555, 1.86595237619048, 0.164901954901961,
0.247333333333333, 0.794901970588235, 0.6429166625, 0.390196078431373,
0.339777768888889), meanproPO = c(1.17066665333333, 3.96755554888889,
1.68466666666667, 0.750888877777778, 1.11022221777778, 2.90190476428571,
0.269411770588235, 0.493777764444444, 0.790392158823529,
0.926458333333333, 0.529583329166667, 0.803111106666667)), .Names = c("sex",
"treat", "pre_post", "meanPO", "meanproPO"), class = "data.frame", row.names = c(13L,
15L, 17L, 19L, 21L, 23L, 1L, 3L, 5L, 7L, 9L, 11L))
#individual plots
#set common themes
my_theme <- theme(
legend.position="none",
axis.line = element_line(colour = "black")
)
#plot 1
gg_TL <- ggplot(subset(mydat, sex=="F"), aes(x=treat, y=meanPO, fill=pre_post)) +
geom_bar(position="dodge", stat="identity", width=0.75) +
scale_y_continuous(limits=c(0,my_ylim), expand=c(0,0) ) +
labs(x="") +
my_theme + theme(
axis.text.x = element_blank()
)
#plot 2
gg_BL <- ggplot(subset(mydat, sex=="F"), aes(x=treat, y=meanproPO, fill=pre_post)) +
geom_bar(position="dodge", stat="identity", width=0.75) +
scale_y_continuous(limits=c(0,my_ylim), expand=c(0,0) ) +
labs(x="") +
my_theme
#plot 3
gg_TR <- ggplot(subset(mydat, sex=="M"), aes(x=treat, y=meanPO, fill=pre_post)) +
geom_bar(position="dodge", stat="identity", width=0.75) +
scale_y_continuous(limits=c(0,my_ylim), expand=c(0,0) ) +
labs(x="", y="") +
my_theme + theme(
axis.text.x = element_blank()
)
#plot 4
gg_BR <- ggplot(subset(mydat, sex=="M"), aes(x=treat, y=meanproPO, fill=pre_post)) +
geom_bar(position="dodge", stat="identity", width=0.75) +
scale_y_continuous(limits=c(0,my_ylim), expand=c(0,0) ) +
labs(y="", x="") +
my_theme
# cowplot
# Plot #1: hv align
plot_grid(gg_TL, gg_TR, gg_BL, gg_BR, labels = LETTERS[1:4], label_size = 18, align="hv")
# Plot #2: define margins of each plot, no hv align
gg_TL <- gg_TL + theme(plot.margin=unit(c(0.6,-0.2,-0.5,0.2), "cm"))
gg_BL <- gg_BL + theme(plot.margin=unit(c(0.6,-0.2,-0.5,0.2), "cm"))
gg_TR <- gg_TR + theme(plot.margin=unit(c(0.4,0.2,-0.5,0.2), "cm"))
gg_BR <- gg_BR + theme(plot.margin=unit(c(0.4,0.2,-0.5,0.2), "cm"))
plot_grid(gg_TL, gg_TR, gg_BL, gg_BR, labels = LETTERS[1:4], label_size = 18)
I like the defaults set by the package, plot look much more publication-ready. I have a small change which you could add to a vignette if setting it globally would hurt:
theme_update(panel.margin = grid::unit(0.05,"lines"))
Without this, faceted graphs look confusing.
I was wondering if there is a functional reason for this version to require R 3.2. I manually installed it on my R 3.1.2 and all functions I tried seems to be compatible. No errors on installation and past scripts work without issues. Is there a test suite that I could run if information isn't available on this?
Hey,
I run into the (much-reported) issue "package ‘cowplot’ is not available (for R version 3.2.3)" when I run install.packages("cowplot", repos="http://cran.r-project.org/") on my Windows 10 machine.
Can you help solve this?
Thanks, best
Jana
Hi everyone
I've just started to use ggplot2 and cowplot so I might be overlooking something. I've set the font family for cowplot to helvetica, but it seems that the large figure labels for cowplot are still arial. (shown in the attached file, there is a difference between the G of the X axis and the large label for the actual figure) There doesn't seem to be an option to change the font under plot grid.
Relevant code:
theme_set(theme_cowplot(font_size=12,font_family = "helvetica"))
plot_grid(g.fwd, g.rev, labels = c('G', 'B'), ncol=1)
I'm wondering if it's possible to add a common main title for grouped plot? Also is there an option to place each label (e.g. "A", "B") inside individual plots
Thanks!
See below issue for ggforce
package:
Could we have an argument showZoom = TRUE
and show the shading on how vertically aligned plots related through xaxis?
I have some issues with 'cowplot', I guess, compatability with 'qqplot2'
When I 'cowplot' package uploaded, I constantly get an error for every plot that I try to run:
Error in FUN("text"[[1L]], ...) : Theme element 'text' has NULL property: margin, debug
I have read some recommendations about this issue to add
margin =margin(), debug=F
so I added it as following:
element_text(size=14, vjust=2, face="bold", margin =margin(), debug=F)
but it did not help.
This is very weird, because 2 weeks ago everything worked perfectly fine, but yesterday and today whenever I download 'cowplot', I get this errors for all the plots, disregarding of whether I use 'cowplot' function or not.
The plot_grid() is a great way of making plots ready for publication. However, I think it would make sense if the subplots are labelled with upper-case letters by default.
The option labels = NULL
in plot_grid() could be changed labels = "auto"
and by adding
if("auto" %in% labels){
labels <- LETTERS[1:num_plots]
}
to the body of the function, the subplot labels will be generated automatically.
The num_plots
is already used in the function.
When I run the following I get an unexpected blank page when I save to pdf. Include the theme(legend.position = "none")
or save via ggsave()
everything is fine.
library(cowplot)
plot.df <- data.frame(value = rnorm(10), variable = c("A", "B"))
p <- ggplot(plot.df, aes(x = variable, y = value, fill = variable))
p <- p + geom_boxplot()
pdf(file = "with_legend.pdf")
plot_grid(p)
dev.off()
pdf(file = "without_legend.pdf")
plot_grid(p + theme(legend.position = "none"))
dev.off()
p
ggsave(filename = "by_gsave.pdf")
My sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 15.10
locale:
[1] LC_CTYPE=de_DE.UTF-8 LC_NUMERIC=C LC_TIME=de_DE.UTF-8 LC_COLLATE=de_DE.UTF-8 LC_MONETARY=de_DE.UTF-8
[6] LC_MESSAGES=de_DE.UTF-8 LC_PAPER=de_DE.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] cowplot_0.6.0 ggplot2_2.0.0
loaded via a namespace (and not attached):
[1] labeling_0.3 colorspace_1.2-6 scales_0.3.0 plyr_1.8.3 tools_3.2.3 gtable_0.1.2 Rcpp_0.12.3 grid_3.2.3
[9] digest_0.6.9 munsell_0.4.3
install.packages("cowplot", repos="http://cran.r-project.org/")
Installing package into '/project/csbio/wwang/Rpackages'
(as 'lib' is unspecified)
Warning message:
package 'cowplot' is not available (for R version 3.1.2)
I see:
checking examples ... ERROR
Running examples in ‘cowplot-Ex.R’ failed
The error most likely occurred in:
> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: add_sub
> ### Title: Add annotation underneath a plot
> ### Aliases: add_sub
>
> ### ** Examples
>
> p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_line(colour = "blue") + background_grid(minor='none')
> ggdraw(add_sub(p1, "This is an annotation.\nAnnotations can span multiple lines."))
Warning: `axis.ticks.margin` is deprecated. Please set `margin` property of `axis.text` instead
Error in FUN(X[[i]], ...) :
Theme element 'text' has NULL property: margin, debug
Calls: ggdraw ... element_render -> calc_element -> lapply -> FUN -> lapply -> FUN
Execution halted
Can you please look into ASAP? I'm submitting to CRAN on Friday
There has been some discussion of creating plots with dual axes to highlight the equivalance between scales. For example, a plot of temperature vs. time with a left axis in Celcius and the right in Fahrenheit. There have been some ok solutions like the one here and others from the SO discussion here.
Given the way the backend is set up in cowplot
, would there be a good way of implementing this as a feature? If so, I'd be happy to try and help out by submitting a PR if you have a suggestion of how to implement it within the existing code.
Nice package! Looks to be very useful. However, the cowplot implementation of ggsave
seems to cause two problems with my OSX system.
ggsave(.., type="cairo")
. Since the cowplot ggsave
doesn't allow extra parameters to be passed through, I can't do this.save_plot2
to pass extra arguments through to ggsave
, but I still get an error message about useDingbats
:Error in check.options(new, name.opt = ".X11.Options", envir = .X11env) : c("invalid argument name ‘useDingbats’ in 'grDevices::png(..., width = width, height = height, res = dpi, '", "invalid argument name ‘useDingbats’ in ' units = \"in\")'")
I think the problem is that only the .pdf device recognizes useDingbats
in X11, attempting to use save_plot
to create a png, for instance, fails.
I'd be happy to rewrite around this issue; have put in a pull request.
I'm using ggplot2 1.0.0, R 3.0.3
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.