Anone know how to change the position of the legend title in ggplot?
I have used the following code to move the legend to the bottom and make it horizontal
p <- p + opts(legend.position = 'bottom', legend.direction = 'horizontal')
But now I want the title to be to the left of the legend instead of above. I've looked in the follwing places but cant find it or figure it out:
https://github.com/hadley/ggplot2/wiki/Legend-Attributes
http://had.co.nz/ggplot2/book/toolbox.r
Any assistance would be greatly appreciated
Using the transition guide to version 0.9 as a reference, you might try the following (assuming you want to change the title position for the colour legend):
library(scales)
+ guides(colour = guide_legend(title.position = "left"))
For a continuous scale you'd use guide_colorbar instead of guide_legend.
Just to provide a concrete example to prove I'm not just making this up,
library(ggplot2)
library(scales)
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point(aes(colour = qsec)) +
guides(colour = guide_legend(title.position = "right"))
Related
I was wondering if you can help me out with a (probably silly) problem with my code that is driving me nuts. I'm using R and my problems are with the package sf + ggplot2. Is there anyone out there that can help me?. I found a similar question here, but the solution is not exactly what I need.
I'm trying to plot a numeric variable on a map using the sf package. The variable is numeric and I want the legend to be on a continuous scale. I managed to do that, the problem started when I used the "guides" option to position the title of the legend on top. Here is a reproducible example:
library(sf)
demo(nc, ask = F, echo = F)
ggplot() +
geom_sf(data = nc, aes(fill = BIR74)) +
scale_y_continuous(breaks = 34:36) +
theme(legend.position="bottom",
legend.direction = "horizontal")
This code produces this map:. You can see that the scale of the Bir74 is continuous. If I use guides(fill = guide_legend(title.position = “top”), it will work for placing the title of the legend on top, but the legend label is on a discrete scale now. This is an example
ggplot() +
geom_sf(data = nc, aes(fill = BIR74)) +
scale_y_continuous(breaks = 34:36) +
theme(legend.position="bottom",
legend.direction = "horizontal") +
guides(fill = guide_legend(title.position = "top"))
and this is the map resulting:
What I really want is the legend placed at the bottom with the title on top as in map 2, but with the legend on the scale of map 1.
Any help on how to solve this would be massively appreciated!
Cheers!
Here is my reproducible code:
This is an example of what I want my actual figure to look like.
library(tidyverse)
p <- mtcars %>%
mutate(cyl = factor(cyl)) %>%
ggplot(aes(carb)) +
geom_bar(aes(fill = cyl)) +
scale_fill_manual(values = c("Red","Green","Blue"))
Resulting figure:
Problem:
What I want to change is in the legend. The boxes depicting the color of the bars on the histogram are too big and I want to reduce the size.
Attempted Solutions:
I have tried this code from another stackoverflow question and it does not work:
p <- p + guides(fill = guide_legend(override.aes = list(width = .5)))
In the reference stackoverflow question, another user suggested making a dummy geom_point variable and then using that legend as the legend and removing the fill legend. I would rather not have to do that if possible.
Thank you for the help.
Use legend.key.size (or legend.key.height and legend.key.width). E.g., add
theme(legend.key.size = unit(0.1, "cm"))
to your plot
I'm working with a plot analogous to the following:
ggplot(data=mtcars, aes(x=wt, y=mpg, color=carb)) +
geom_line() + facet_grid(gear ~ .) +
ggtitle(expression("Title")) +
labs(caption = "Sources: Compustat, Author's Calculations") +
theme(plot.title = element_text(size = 20, hjust = 0.5),
plot.caption=element_text(size=8, hjust=.5),
strip.background = element_blank(),
strip.text = element_blank(),
legend.title = element_blank())
I'm trying to do the following:
Insert a legend beneath each of the 3 facets, each legend specific to the facet above it.
Insert one plot title (as opposed to the same title above each facet).
Insert one caption beneath the final facet (as opposed to three captions beneath each facet).
I was able to reproduce this example on assigning a legend to each facet.
However, the plot title was placed above and the caption below each facet. Also, this example uses facet_wrap and not facet_grid.
Thank you in advance.
library(dplyr)
library(ggplot2)
tempgg <- mtcars %>%
group_by(gear) %>%
do(gg = {ggplot(data=., aes(x=wt, y=mpg, color=carb)) +
geom_point() +
labs(x = NULL) +
guides(color = guide_colorbar(title.position = "left")) +
theme(plot.title = element_text(size = 20, hjust = 0.5),
plot.caption=element_text(size=8, hjust=.5),
legend.position = "bottom")})
tempgg$gg[1][[1]] <- tempgg$gg[1][[1]] + labs(title = "Top title")
tempgg$gg[3][[1]] <- tempgg$gg[3][[1]] + labs(x = "Axis label", caption = "Bottom caption")
tempgg %>% gridExtra::grid.arrange(grobs = .$gg)
This isn't the most elegant way to do it. Each of the three grobs gets an equal space when you grid.arrange them, so the first and last ones are squished from the title and caption taking up space. You could add something like heights = c(3,2,3) inside the grid.arrange call, but you'd have to fiddle with each of the heights to get it to look right, and even then it would be a visual approximation, not exact.
To do it the more precise way, you'd need to look at the underlying gtables in each of the grobs. https://stackoverflow.com/users/471093/baptiste is the expert on that.
Update:
I used a #baptiste solution, which is still not particularly elegant, but gives you the same plot space for each panel. Use this snippet in place of the last line above.
tempggt <- tempgg %>% do(ggt = ggplot_gtable(ggplot_build(.$gg))) %>% .$ggt
gg1 <- tempggt[[1]]
gg2 <- tempggt[[2]]
gg3 <- tempggt[[3]]
gridExtra::grid.arrange(gridExtra::rbind.gtable(gg1, gg2, gg3))
I'm having a similar problem as described in here under "2- After having the two legends...", but instead of increasing the point size (which eventually also enlarges the legend itself), I would like fill each box in the legend with the corresponding color. Like in a bar plot's legend. Data & code examples here.
Looking through several other questions here, the ggplot docu, etc., I tried variations of code-snippets I found, but couldn't figure out a solution. The legend always retained the point symbols.
Therefore: If possible, how to tweak or replace the legend of a point/scatter/bubble plot so that it looks like the legend of a bar plot? Or, more generally, how to replace the legend of a given geom in ggplot2 with that of a different one? Thank you for any hints!
Edit: Example with mtcars data
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(colour = factor(cyl), size = qsec))
p
Adding what I gathered from other SO-answers...
p <- p + guides(colour = guide_legend(override.aes = list(fill = unique(mtcars$cyl))))
p
...keeps the points, instead of expanding the color to fill the legend box, no matter arguments and datasources I try for guides() and list().
On the other hand:
ggplot(mtcars, aes(wt, mpg)) + geom_bar(aes(fill = factor(cyl)), stat="identity")
...draws nicely color-filled boxes to the legend. That's what I'm trying to do for a bubble plot.
You won't be able to get a fill-type legend per se, but you can easily emulate it:
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(colour = factor(cyl), size = qsec)) +
guides(col = guide_legend(override.aes = list(shape = 15, size = 10)))
Edit: The problem was due to my incorrectly attempting to alter theme(title = element_text()), when I needed to alter theme(plot.title(element_text()). I would have noticed this had I more carefully scrutinized the theme() documentation.
Original Post:
Changing the title vertical justification alters the position of the x and y axis labels as well. Is this a bug? Or am I misinterpreting the function of theme()? I am running ggplot2 version 0.9.3.1
Minimal reproducible example.
require(ggplot2)
set.seed(12345)
x <- rnorm(100,10,0.5)
y <- x * 3 + rnorm(100)
df <- data.frame(y,y)
The default title is too close to the graph for my taste....
ggplot(df,aes(x,y)) +
geom_point() +
labs(title="My Nice Graph")
When I try to move the title, the axis labels move also, and illegibly plot on the graph.
ggplot(df,aes(x,y)) +
geom_point() +
labs(title="My Nice Graph") +
theme(title = element_text(vjust=2))
You want plot.title not title:
labs(title="My Nice Graph") + theme(plot.title = element_text(vjust=2))
Alternate quick fix is adding a line break:
labs(title="My Nice Graph\n")
vjust does not work for me (I also think values should be in [0, 1]). I use
... +
theme(
plot.title = element_text(margin=margin(b = 10, unit = "pt"))
)