Comments (7)
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.
This seems to be fixed, so I'm closing this issue. Please reopen if I'm mistaken.
from cowplot.
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.
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)
from cowplot.
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)
from cowplot.
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.
from cowplot.
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)
- Cannot convert object of class LayerInstanceLayerggprotogg into a grob?
- changing size of the second plot in plot_grid() HOT 2
- ggarange common.legend positioning overwritten
- No side effect from plot_grid when used inside a function returning an object
- get_legend doesn't match original position
- Blank plot using plot_grid with RNotebook
- Feature request: options for anchors of labels in plot_grid HOT 1
- warning message: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0. HOT 5
- Explanation of how align_plot, ggdraw, and draw_plot work together for dual-y-axis plots.
- `draw_image` only works when background themes are set to `ggplot2::element_blank()` HOT 1
- Aligning legends?
- plot_grid not working HOT 1
- Cowplot breaks dates? HOT 2
- Cowplot() plot_grid() squishing axes and legend
- Mix bold and plain text in `cowplot::draw_label`
- Labels in `plot_grid()` seemed not affected by `byrow` HOT 2
- Is geom_image (ggimage) not working anymore with Kaggle? HOT 1
- Shared legend feature stopped workin with ggplot2 v3.5.0 HOT 10
- Calling save_plot( ) non-interactively via Rscript creates empty Rplots.pdf file
- `get_legend` only works with default legend position, will not detect if legend is "bottom" for instance HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cowplot.