GithubHelp home page GithubHelp logo

Comments (7)

Ax3man avatar Ax3man commented on July 30, 2024 1

I think this issue is fixed with the new ggplot2. Using theme(legend.justification = c(0,0.5) will now correctly justify the legends.

from cowplot.

clauswilke avatar clauswilke commented on July 30, 2024 1

This seems to be fixed, so I'm closing this issue. Please reopen if I'm mistaken.

from cowplot.

clauswilke avatar clauswilke commented on July 30, 2024

It would help if you could provide the code that generates this figure, or a different one with the same issue. Fundamentally, this is due to how ggplot2 creates the legend grob, so outside of my control. But there's probably a work-around that can be found.

from cowplot.

aviast avatar aviast commented on July 30, 2024

Sure, here it is.

library(cowplot)
library(plyr)

x <- seq(from = -pi, to = pi, by = 0.01)
y1 <- cos(x)
y2 <- sin(x)
y3 <- tan(x)

df <- data.frame(x = rep(x, 3),
                 y = c(y1, y2, y3),
                 label = rep(c("short","mmeeddiiuumm","looooooooooooooooooooooong"), each = length(x)),
                 stringsAsFactors = FALSE)

plots <- dlply(df, .(label), function(trig) {
  ggplot(trig, aes(x = x, y = y, colour = label)) +
    geom_line() +
    theme(legend.justification = c(0,0),
          legend.title = element_blank())
})

plot_grid(plotlist = plots, align = "v", ncol = 1)

ggsave(filename = "cowplot.png", width = 10, height = 10, units = "cm", scale = 2)

cowplot

from cowplot.

clauswilke avatar clauswilke commented on July 30, 2024

Workaround: Do the alignment manually. Importantly, this sets the panel width to a fixed value, when normally in ggplot the panel expands as needed. So you need to choose the appropriate panel width given the size at which you save the figure.

These kinds of problems come up all the time, and we'll probably need convenience functions that make this kind of alignment manipulation easier. Not quite sure yet of the best API, though.

# we need grid for these manipulations
require(grid)

# convert to gtables
grobs <- lapply(plots, ggplot2::ggplotGrob)

# width of panel is hardcoded, right margin is set to expand
panel_width <- unit(4, "inches")
r_margin_width <- unit(1, "null")

if(getRversion() < "3.3.0"){
  # the following conversions are necessary
  # because there is no `[<-`.unit method
  # so promoting to unit.list allows standard list indexing
  for ( i in 1:length(grobs) )
  {
    grobs[[i]]$widths <- grid:::unit.list(grobs[[i]]$widths)
  }

  panel_width <- grid:::unit.list(panel_width)
  r_margin_width <- grid:::unit.list(r_margin_width)
}

max_widths <- do.call(grid::unit.pmax, lapply(grobs, function(x){x$widths}))

# now assing to all graphs
for ( i in 1:length(grobs) )
{
  grobs[[i]]$widths[1:3] <- max_widths[1:3]   # align y axis
  grobs[[i]]$widths[4] <- panel_width         # set panel size
  grobs[[i]]$widths[6] <- r_margin_width      # set right margin
}

p <- plot_grid(plotlist = grobs, ncol = 1)

save_plot("plot.png", p, base_width = 8, base_aspect_ratio = 2.5, nrow = 3)

plot

from cowplot.

aviast avatar aviast commented on July 30, 2024

Thanks Claus. That seems to be heading in the right direction. I'll need to play around with the plot width to avoid the years running into each other and it looks like the bottom chart is not aligned with the top two.

plot

from cowplot.

aviast avatar aviast commented on July 30, 2024

BTW, here is my data as extracted from the PDF.

library(cowplot)
library(plyr)

df <- structure(list(Measure = c("Incidents", "Serious incidents", 
"Serious injury accidents", "Fatal accidents", "Total accidents", 
"Serious injuries", "Fatalities", "Accidents per million departures", 
"Fatal accidents per million departures", "Incidents", "Serious incidents", 
"Serious injury accidents", "Fatal accidents", "Total accidents", 
"Serious injuries", "Fatalities", "Accidents per million departures", 
"Fatal accidents per million departures", "Incidents", "Serious incidents", 
"Serious injury accidents", "Fatal accidents", "Total accidents", 
"Serious injuries", "Fatalities", "Accidents per million departures", 
"Fatal accidents per million departures", "Incidents", "Serious incidents", 
"Serious injury accidents", "Fatal accidents", "Total accidents", 
"Serious injuries", "Fatalities", "Accidents per million departures", 
"Fatal accidents per million departures", "Incidents", "Serious incidents", 
"Serious injury accidents", "Fatal accidents", "Total accidents", 
"Serious injuries", "Fatalities", "Accidents per million departures", 
"Fatal accidents per million departures", "Incidents", "Serious incidents", 
"Serious injury accidents", "Fatal accidents", "Total accidents", 
"Serious injuries", "Fatalities", "Accidents per million departures", 
"Fatal accidents per million departures", "Incidents", "Serious incidents", 
"Serious injury accidents", "Fatal accidents", "Total accidents", 
"Serious injuries", "Fatalities", "Accidents per million departures", 
"Fatal accidents per million departures", "Incidents", "Serious incidents", 
"Serious injury accidents", "Fatal accidents", "Total accidents", 
"Serious injuries", "Fatalities", "Accidents per million departures", 
"Fatal accidents per million departures", "Incidents", "Serious incidents", 
"Serious injury accidents", "Fatal accidents", "Total accidents", 
"Serious injuries", "Fatalities", "Accidents per million departures", 
"Fatal accidents per million departures", "Incidents", "Serious incidents", 
"Serious injury accidents", "Fatal accidents", "Total accidents", 
"Serious injuries", "Fatalities", "Accidents per million departures", 
"Fatal accidents per million departures"), Description = c("Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of people involved", "Number of people involved", "Rate of aircraft involved", 
"Rate of aircraft involved", "Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of people involved", "Number of people involved", 
"Rate of aircraft involved", "Rate of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of people involved", "Number of people involved", "Rate of aircraft involved", 
"Rate of aircraft involved", "Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of people involved", "Number of people involved", 
"Rate of aircraft involved", "Rate of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of people involved", "Number of people involved", "Rate of aircraft involved", 
"Rate of aircraft involved", "Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of people involved", "Number of people involved", 
"Rate of aircraft involved", "Rate of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of people involved", "Number of people involved", "Rate of aircraft involved", 
"Rate of aircraft involved", "Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of people involved", "Number of people involved", 
"Rate of aircraft involved", "Rate of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of people involved", "Number of people involved", "Rate of aircraft involved", 
"Rate of aircraft involved", "Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of aircraft involved", 
"Number of aircraft involved", "Number of people involved", "Number of people involved", 
"Rate of aircraft involved", "Rate of aircraft involved"), Year = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), .Label = c("2005", 
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", 
"2014"), class = "factor"), Count = c(3332, 32, 2, 2, 12, 2, 
18, 9.2, 1.5, 3081, 16, 0, 1, 12, 0, 2, 9.5, 0.8, 3152, 47, 1, 
2, 23, 1, 2, 17.5, 1.5, 3337, 52, 3, 3, 29, 15, 6, 22.1, 2.3, 
3162, 27, 4, 0, 14, 6, 0, 11, 0, 3477, 37, 2, 1, 23, 2, 2, 16.6, 
0.7, 4002, 28, 2, 2, 21, 2, 2, 14.9, 1.4, 4222, 47, 2, 1, 14, 
2, 1, 9.5, 0.7, 4402, 54, 2, 2, 15, 4, 3, 10.4, 1.4, 4308, 37, 
5, 0, 27, 7, 0, NA, NA)), row.names = c(NA, -90L), .Names = c("Measure", 
"Description", "Year", "Count"), class = "data.frame")

PLOT <- dlply(df, .(Description), function(DATA) {
  ggplot(DATA, aes(x = Year, y = Count, group = Measure, colour = Measure)) +
    geom_line() +
    theme(legend.position = "right",
          legend.justification = c(0,0),
          legend.title = element_blank()) +
    background_grid(major = "xy", minor = "none")
})

plot_grid(plotlist = PLOT, align = "v", ncol = 1)

from cowplot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.