gtable

Save a ggplot2 time series plot grob generated by ggplotGrob

旧巷老猫 提交于 2019-11-26 22:08:53
问题 This post describes a method to create a two-line x-axis (year below months) on a time series plot. Unfortunately, the method that I use from this post ( option 2 ) is not compatible with ggsave() . library(tidyverse) library(lubridate) df <- tibble( date = as.Date(41000:42000, origin = "1899-12-30"), value = c(rnorm(500, 5), rnorm(501, 10)) ) p <- ggplot(df, aes(date, value)) + geom_line() + geom_vline( xintercept = as.numeric(df$date[yday(df$date) == 1]), color = "grey60" ) + scale_x_date

Overall Label for Facets

浪尽此生 提交于 2019-11-26 20:47:04
问题 Here is some minimal code to generate a graph with two sets of facets. library("ggplot2", quietly = TRUE, warn.conflicts = FALSE) library("RColorBrewer", quietly = TRUE, warn.conflicts = FALSE) val.a <- rnorm(10) val.b <- rnorm(10) val.c <- c("A","B","A","A","B","B","B","B","A","B") val.d <- c("D","D","E","D","E","E","E","D","D","E") val.e <- rnorm(10) maya <- data.frame(val.a,val.b,val.c,val.d,val.e) ggplot(maya, aes(x=val.a, y=val.b)) + geom_point(shape=20,size=3, aes(colour=val.e)) + facet

Align ggplot2 plots vertically

Deadly 提交于 2019-11-26 17:30:30
With the code found at https://gist.github.com/low-decarie/5886616 A dual dendodogram tile plot can be produced: dual_dendogram_tile_plot(as.matrix(USArrests), main="USA Arrests") The problem: Align the vertical dendogram with the tile plot area. (and/or improve the alignment of the horizontal dendogram) This question relates to: left align two graph edges (ggplot) Specifying ggplot2 panel width Plot correlation matrix into a graph Here's an example to align more basic grobs, library(ggplot2) library(grid) library(gtable) p <- qplot(1,1) g <- ggplotGrob(p) panel_id <- g$layout[g$layout$name ==

Force X axis text on for all facets of a facet_grid plot

牧云@^-^@ 提交于 2019-11-26 16:15:41
问题 I have the same problem as this user: I'd like to make a facet_grid plot with a discrete x-axis, and I'd like to have the x-axis labels be written under each facet rather than only underneath the bottom row of facets. For instance: # Drop some factor levels to make the plot smaller diamondSub <- subset(diamonds, (cut=="Ideal" | cut=="Premium") & (color=="E" | color=="I")) # Note that scales="free_x" has no practical effect here ggplot(diamondSub, aes(x=clarity, y=price)) + geom_blank()+ geom

Multiple ggplots of different sizes

橙三吉。 提交于 2019-11-26 15:33:55
问题 It's relatively simple using grid.arrange in the gridExtra package to arrange multiple plots in a matrix, but how can you arrange plots (the ones I'm working on are from ggplot2 ) when some plots are intended to be larger than others? In base, I can use layout() such as in the example below: nf <- layout(matrix(c(1,1,1,2,3,1,1,1,4,5,6,7,8,9,9), byrow=TRUE, nrow=3)) layout.show(nf) what is the equivalent for ggplot plots? Some plots for inclusion library(ggplot2) p1 <- qplot(x=wt,y=mpg,geom=

Seeking workaround for gtable_add_grob code broken by ggplot 2.2.0

随声附和 提交于 2019-11-26 12:46:24
问题 In plots with multiple facet variables, ggplot2 repeats the facet label for the \"outer\" variable, rather than having a single spanning facet strip across all the levels of the \"inner\" variable. I have some code that I\'ve been using to cover the repeated outer facet labels with a single spanning facet strip using gtable_add_grob from the gtable package. Unfortunately, this code no longer works with ggplot2 2.2.0 due to changes in the grob structure of the facet strips. Specifically, in

Save plot with a given aspect ratio

谁都会走 提交于 2019-11-26 12:41:33
问题 I\'m working with the really awesome library ggplot2. I figured out how to set the aspect ratio of a plot by using coord_fixed . Now, I\'d like to save the plot to a PDF with a specified width (e.g 10 cm) and let required height get calculated. I did not figure out how to achieve this. Is this even possible? 回答1: You can use grid functions to calculate the full size of the ggplot grob, but there are ( edit: at least) two caveats: an extra device window will open, to do the unit conversion the

ggplot2 - adding secondary y-axis on top of a plot

女生的网名这么多〃 提交于 2019-11-26 12:32:57
问题 For a publication I need to add a second y-axis to an existing plot. I\'ve come across a means of how to do this (https://rpubs.com/kohske/dual_axis_in_ggplot2). However, I don\'t really understand much of the coding. I cannot find a way to make it so that the right y-axis shows too, and only not the top border. What am I missing in my coding? This is my dummy data: df1 <- structure(list(month = structure(1:12, .Label = c(\"Apr\", \"Aug\", \"Dec\", \"Feb\", \"Jan\", \"Jul\", \"Jun\", \"Mar\",

How to use facets with a dual y-axis ggplot

南楼画角 提交于 2019-11-26 11:18:33
I have been trying to extend my scenario from here to make use of facets (specifically facet_grid() ). I have seen this example , however I can't seem to get it to work for my geom_bar() and geom_point() combo. I attempted to use the code from the example just changing from facet_wrap to facet_grid which also seemed to make the first layer not show. I am very much a novice when it comes to grid and grobs so if someone can give some guidance on how to make P1 show up with the left y axis and P2 show up on the right y axis that would be great. Data library(ggplot2) library(gtable) library(grid)

how to show a legend on dual y-axis ggplot

懵懂的女人 提交于 2019-11-26 09:51:07
问题 I am trying to compose a dual y-axis chart using ggplot. Firstly let me say that I am not looking for a discussion on the merits of whether or not it is good practice to do so. I find them to be particularly useful when looking at time based data to identify trends in 2 discrete variables. A further discussion of this is better suited to crossvalidated in my opinion. Kohske provides a very good example of how to do it, which I have used to great effect so far. I am however at my limits to