Having horizontal instead of vertical marks on a 2x1 face and a splitting y-mark

I want to have two graphs in one. They are time series, but different scales (let say heights and weights). The way I want to introduce this is to have two digits on top of each other that will share an x-tag but have different y-tags.

I have two different problems:

  • If I make a 2x1 facet, the default position for the face labels on the right is with vertical text, I would rather have them on top of the graphs as (sub) tags
  • ylab()defines one label for all faces, but I have two different variables / scales. Is it possible to “break” it and have two different labels (for example, “gram” for weight and “inches” for height)?

Thank!

EDIT: Alright, this is the code. mn is a variable that is tied to time, grouped by risk. But the value mndepends on var. For some values, varthis weightis for others height.

qplot(time, mn, data=d.plot.long,
  color=risk, group=risk,
  geom=c("line","point"),
  position=position_dodge(.2))+
geom_point(size=5, position=position_dodge(.2))+
geom_errorbar(aes(x=time, y=mn, ymin=low, ymax=hi),width=.3 ,position=position_dodge(.2))+
facet_grid(var~.,scales='free_y')+
xlab('Time')+ylab('')+
scale_color_discrete(name="The grouping variable", breaks=c("Hi","Low"), labels=(c("Group A","Group B")))
+1
source share
3 answers

As others have said, reproducibility simplifies answering questions. However, this is a question that can be easily answered by the general case.

ggplot2 offers several limited options for multiple plots on a single plate. The R Cookbook offers a pretty good and very easy to use workaround here . I can quickly guide you through this for your business:

First, some two simple sets of time series in a data frame:

year<-c(2010:2014)
data1<-c(1:5)
data2<-c(20:24)
df<-data.frame(year,data1,data2)

. , x, :

library(ggplot2)
p1<-qplot(data=df,x=year,y=data1)+theme(axis.title.x = element_blank(), axis.text.x = element_blank())
p2<-qplot(df,x=year,y=data2)

, :

# Multiple plot function ggplot objects can be passed in ..., or to plotlist
# (as a list of ggplot objects) - cols: Number of columns in layout -
# layout: A matrix specifying the layout. If present, 'cols' is ignored.  If
# the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE), then
# plot 1 will go in the upper left, 2 will go in the upper right, and 3 will
# go all the way across the bottom.
multiplot <- function(..., plotlist = NULL, file, cols = 1, layout = NULL) {
  require(grid)
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  numPlots = length(plots)
  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel ncol: Number of columns of plots nrow: Number of rows
    # needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), ncol = cols,
                     nrow = ceiling(numPlots/cols))
  }
  if (numPlots == 1) {
    print(plots[[1]])
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col))
    }
  }
}

... , .

multiplot(p1,p2,cols=1)

( , , , ! , .)

, ?

+2

gtable:::rbind_gtable; .

align_plots = function(...){
  pl <- list(...)
  ## test that only passing plots
  stopifnot(do.call(all, lapply(pl, inherits, "gg")))
  gl <- lapply(pl, ggplotGrob)
  bind2 <- function(x,y)
    gtable:::rbind_gtable(x,y,"first") # bug with pmax

  combined <- Reduce(bind2, gl[-1], gl[[1]])

  wl <- lapply(gl, "[[", "widths")
  combined$widths <- do.call(grid::unit.pmax, wl)
  grid::grid.newpage()
  grid::grid.draw(combined)
}

align_plots(qplot(1,1), 
            qplot(100, 100), 
            qplot(1000, 1000) + ylab("two\nlines"))

enter image description here

+6

@Joe, , gridExtra.

:

require(ggplot2)
require(gridExtra)

p1 <- ggplot(data=df, aes(x=year, y=data1)) + 
  geom_point() +
  theme_bw() +
  theme(
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank()
  )

p2 <- ggplot(data=df, aes(x=year, y=data2)) + 
  geom_point() + 
  theme_bw()

grid.arrange(p1, p2, ncol=1)

: enter image description here

0
source

Source: https://habr.com/ru/post/1530900/


All Articles