How to vertically align elements of legend box - r

I have below ggplot
library(ggplot2)
library(grid)
theme_set(theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position=c(0,1),
legend.justification = c(0,1),
legend.box.margin = margin(5, l = 5, unit = 'mm'),
legend.box = 'horizontal'
))
ggplot(diamonds,aes(x,y,color=z))+
geom_point()+
scale_colour_gradient2('Time [min]',
low='lightgray',
mid='red3',
high='red4',
midpoint=15)
With this I am getting below plot,
However I wanted to horizontally align the colour definition in the legend, while colour title (i.e. Time [min]) should be on top of the colour definition. Is there any way to achieve this

By adding legend.direction = 'horizontal' and some guides,
ggplot(diamonds,aes(x,y,color=z))+
geom_point()+
scale_colour_gradient2('Time [min]',
low='lightgray',
mid='red3',
high='red4',
midpoint=15) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position=c(0,1),
legend.justification = c(0,1),
legend.box.margin = margin(5, l = 5, unit = 'mm'),
legend.box = 'horizontal',
legend.direction = 'horizontal'
) +
guides(colour = guide_colourbar(title.position="top", title.hjust = 0.5),
size = guide_legend(title.position="top", title.hjust = 0.5))

Related

Add only one panel border line between facets in ggplot

For the following sample df
df = data.frame(x = c(2,3,4),y = c(4,5,6),group.a= c("1","1","2"),group.b = c("a","b","b"))
I want to just add a horizontal line in-between the y axis facet grids and after browsing different posts here I have tried using the panel.border = element_rect() argument however that gives me all four borders (top, right, bottom, left)
ggplot(df,aes(x=x,y=y)) + facet_grid(group.a~group.b) + theme_minimal() +
theme(legend.position = "bottom",
legend.title = element_blank(),
legend.direction = "horizontal",
legend.margin = margin(-20,0,0,0),
panel.grid = element_blank(),
panel.border = element_rect(color = "black", fill = NA, size = .5)
axis.text.x = element_blank(),
axis.line.y = element_line(size = .5),
axis.line.x = element_line(size = .5),
strip.placement = "outside")
Is there a way to just have the bottom and left border of the panel borders? Thanks!
There aren't any theme elements that fit the bill here; you would need custom annotations. Fortunately, annotation_custom applies to each panel, so this can be done with a couple of lines of code
library(ggplot2)
df = data.frame(x = c(2,3,4),y = c(4,5,6),
group.a= c("1","1","2"),group.b = c("a","b","b"))
ggplot(df,aes(x=x,y=y)) +
facet_grid(group.a~group.b) +
geom_point(col = "white") +
theme_minimal() +
annotation_custom(grid::linesGrob(y = c(0, 0), gp = grid::gpar(lwd = 3))) +
annotation_custom(grid::linesGrob(x = c(0, 0), gp = grid::gpar(lwd = 3))) +
theme(panel.grid = element_blank(),
strip.placement = "outside",
axis.text.x = element_blank())

How do I print a plot with only keep the plot panel content in ggplot2?

I want to save a picture from a plot which only retains the plot panel contents.
But when I used the below code, there's a white margin at the left and bottom of the plot.
How do I get rid of it?
Thanks.
ggplot()+
stat_function(fun = ~ -(.x-2) ^ 2 + 3, size = .5, linetype = 'dashed', color='white') +
scale_x_continuous(expand = c(0, 0), limits = c(0, 4)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 4)) +
theme(
plot.margin = unit(c(0,0,0,0), "pt"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "black", colour = "black"),
axis.text = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank()
)

Adjusting the y-tick labels with hjust not working

I want to move the y-axis tick labels to the left (as i did with the axis.text.y to the bottom).
The weird thing is with hjust = -2.5, although the theme_set() part runs without errors, the text on the y-axis does not get moved. However, vjust = 2does affect the y labels just fine.
I am creating a bug with all my options in the theme?
library(tidyverse)
library(ggplot2)
theme_set(
theme_classic() +
theme(
axis.ticks.length = unit(-0.25, "cm"),
axis.text.x = element_text(vjust = -2.5),
axis.text.y = element_text(hjust = -2.5), # this does not do anything.
# axis.text.y = element_text(vjust = -2.5), # while vjust does affect the text placement.
axis.line = element_blank(),
panel.grid.major.y = element_line(linetype = 2),
plot.title = element_text(hjust = 0.5),
text = element_text(family = "serif"),
legend.justification = c(1, 1),
legend.position = c(1, 1),
panel.border = element_rect(fill = NA, size = 2)))
mpgdat <- tibble(mtcars)
mpgdat %>% ggplot(aes(disp, mpg)) + geom_point()
This does seem to be a bug in how hjust works for axis text. A workaround would be to use margin instead of hjust:
theme_set(
theme_classic() +
theme(
axis.ticks.length = unit(-0.25, "cm"),
axis.text.x = element_text(vjust = -2.5),
axis.text.y = element_text(margin = unit(c(0,0.4,0,0), "cm")),
panel.border = element_rect(fill = NA, size = 2)))
'
Hat tip to this related answer: How do I make my axis ticks face Inwards in ggplot2

How to move legend up closer to the x-axis label in ggplot2

I have am creating a function to create dumbbell graphs with the legend positioned on the bottom. However, it's too far away from the title of the x-axis. I wanted to move it up slightly so that it is 10 pixels below the x-axis.
Here's the code:
vertical_theme = theme_bw(base_family = "Georgia") +
theme(
panel.border = element_rect(color = "black", fill=NA),
axis.title.x = element_text(hjust=0.5, size = 10, margin=margin(t=10, b=10)),
axis.text.y = element_text(size=10, margin=margin(r=10), color="black", hjust=0),
axis.text.x = element_text(size=10, margin=margin(t=10), color="black"),
axis.title.y = element_blank(),
legend.title = element_blank(),
legend.position= "bottom",
legend.text = element_text(size = 10, margin = margin(r = 10)),
panel.grid.major.y = element_blank() ,
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_line(size=1),
panel.grid.minor.x = element_blank(),
plot.margin = margin(10, 30, 10, 10, "pt"))
dumbbell = function(df) {
ggplot(df, aes(pct_responses, Domain)) +
geom_line(aes(group=Domain)) +
geom_point(aes(shape=race), size=5, color="#3bbae0" ) +
vertical_theme +
scale_shape_manual(labels = c("Black Students", "White Students"),
values=c(15, 19)) +
scale_x_continuous(expand = c(0, 0),
limits=c(0,100),
breaks = seq(0, 100, by=20),
labels = function(x) paste0(x,"%")) +
labs(x = "% of Responses") +
scale_y_discrete(labels = wrap_format(40))
}
dumbbell(df)
Here's a screenshot (labels on y-axis removed because that data isn't public yet):
I tried to adjust the legend.position manually with legend.position = c(0.5, 0) (playing around with various different numbers) but then the legend overlaps with "% of Responses."
Use theme(legend.margin=margin(-10, 0, 0, 0)) to move the legend up. Adjust -10 as needed.

Increase the space between legend keys and align them in ggplot

I want to align my legend positions when those are customised and want to be at the bottom of the graph. In addition I want to increase the space between them. Here is a my gig-lot code:
p<-ggplot(aggregate(cbind(Tax_Burden,debt_GDP) ~ cou, data=df, mean, na.rm=TRUE), aes(x=log(debt_GDP), y=log(Tax_Burden))) +
geom_point(aes(color = Tax_Burden, size=debt_GDP))+
labs(color = "Tax Burden\n[% Total\nRevenues]", size = "Public Debt [% GDP]")+
geom_smooth(method=lm, se=FALSE, linetype="dashed")+
geom_text_repel(aes(label = cou), size = 2)+
ylab("Log of Tax Burden") +
xlab("Log of Public Debt [% GDP]") +
theme_minimal() +
theme(text =element_text(family="Times New Roman"),
legend.background = element_blank(),
legend.key = element_blank(),
legend.text = element_text(size = 4),
legend.title = element_text(size=6),
legend.position=c(0.75, 0.15),
legend.box = "horizontal",
legend.spacing.x = unit(-0.05, 'cm'),
legend.margin = margin(-0.5,0,0,0, unit="cm"),
panel.border = element_blank(),
panel.background = element_blank(),
axis.title.x = element_text(size = 8),
axis.title.y = element_text(size = 8))+
guides(colour=guide_legend(nrow=2))+
guides(size=guide_legend(nrow=2))
print(p)
and here is the picture:
As you see in the picture, legends are two close to each other, while are not very well aligned.
How I can improve this in ggplot?

Resources