How to reduce blank space between legend and edge of the window? - r

I have this code:
ggplot(subset(sub, paired =='1'), aes(cond, fill = corr)) +
geom_bar(position = "fill")+
theme_minimal()+
xlab ('Тест') +
ylab ('Доля ответов') +
facet_wrap(biling_type~biling_group, 1, labeller=labeller(paired = c('1'= 'Парные', "0" = "Непарные"), biling_type = labels1, biling_group = labels2)) +
labs(title="Парные понятия")+
theme(strip.text = element_text(size=12),
axis.title.x = element_text(size=14),
axis.text.x = element_text(size=14),
axis.title.y = element_text(size=14),
axis.text.y = element_text(size=14),
legend.title = element_text(size=14),
legend.text = element_text(size=14))+
scale_y_continuous(breaks=c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1))+
scale_x_discrete (labels = c("Житейский", "Научный"))+
scale_fill_discrete(name="Правильность употребления",
labels=c("Правильное", "Неправильное"))
And this plot:
It seems that because of some legend features there's a blank space between the plot and the edge of the window. And titles aren't fully shown.

Related

Sjplot: I dont want to label those bars which have 0% value. How can I do that?

Here is my code for barplot using Sjplot package.
gplot_rad<- sjPlot::plot_xtab(child_tech_up$accss_rad, child_tech_up$level21,
margin = "row", bar.pos = "dodge", coord.flip = TRUE,
legend.title = "Education Level",
title = "Access to Radio", show.n = FALSE) +
theme(axis.text.x =element_blank(), #remove y axis labels
axis.ticks.x =element_blank(),
axis.title.x = element_blank(),
legend.position = "bottom",
plot.title = element_text(hjust = .5, size = 12),
axis.title.y = element_blank())

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?

The mirror_ticks function not working for ggplot

I want to place ticks on all four sides of my graph. The way suggested for this is mirror_ticks.
library(ggplot2)
library(ggplotTicks)
sp6<-ggplot(Anna_Smooth, aes(y=log10(Prob2), x=log10(AvSize)))+
geom_point( data=Anna_Smooth, aes(y=log10(Prob2), x=log10(AvSize), color=PART) )+
guides( color=FALSE)
sp8<-sp6+ labs(x=expression(paste(log(s))))+
labs(y=expression(paste(log(P(s)))) )+
theme(axis.text.y = element_text(size=14),
axis.text.x = element_text(size=14),
axis.title.y = element_text(size=15),
axis.title.x = element_text(size=15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=2)
)
sp10<-mirror_ticks(sp10, allPanels=TRUE)
My output sp10 has no ticks on opposite panels, same result if I put allPanels=TRUE
Is there a fix? I am open to learn how one does this with theme settings?
As of ggplot2 version 2.2.0 (2016-11-11), the scale_x_continuous() and scale_y_continuous() can display a secondary axis which is positioned opposite to the primary axis and which can be controlled with the sec.axis argument.
This can be used to mirror the tick marks.
The OP hasn't provided reproducible data so we use the mpg dataset which comes with the ggplot2 package:
Chart without mirrored tick marks
library(ggplot2)
g1 <- ggplot(mpg, aes(log10(displ), log10(hwy))) +
geom_point() +
theme(
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(size = 15),
axis.title.x = element_text(size = 15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(
colour = "black",
fill = NA,
size = 2
)
)
g1
Chart with secondary axes
g1 +
scale_x_continuous(sec.axis = dup_axis()) +
scale_y_continuous(sec.axis = dup_axis())
g1 +
scale_x_continuous(sec.axis = dup_axis(name = NULL)) +
scale_y_continuous(sec.axis = dup_axis(name = NULL))
g1 +
scale_x_continuous(sec.axis = dup_axis(name = NULL, labels = NULL)) +
scale_y_continuous(sec.axis = dup_axis(name = NULL, labels = NULL))
Mirrored tick marks with log10 scales
The secondary axes are also available with the scale_x_log10() and scale_x_log10() functions.
So, it can be avoided to use the log() function within the call to aes() but by specifying an appropriate log scale:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme(
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(size = 15),
axis.title.x = element_text(size = 15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(
colour = "black",
fill = NA,
size = 2
)
) +
scale_x_log10(sec.axis = dup_axis(name = NULL, labels = NULL)) +
scale_y_log10(sec.axis = dup_axis(name = NULL, labels = NULL))

How can I resize the plot in order to be used in R Markdown?

I've created the attached plot using the mentioned code, however, the height of the whole plot looks a bit large and the blank area between the plot and the legend does not seem suitable.
I want to resize the plot in order to use it later in R Markdown presentation. I've seen some posts about resizing the plots but they have all been about resizing the plot for saving purposes. I want to get a plot that looks similar to the one created by Urban Institute Style Guide.
ggplot(top_five, aes(x= Year, y = Production, fill = Country)) +
geom_bar(stat = "identity", position = "dodge") +
theme_minimal() +
scale_fill_manual(values = c("#375E97", "#FB6542", "#FFBB00", "#3F681C", "#98DBC6")) +
scale_y_continuous(limits = c(0,30000), breaks = c(0, 2000, 4000, 6000, 8000, 10000, 12000, 14000), labels = scales::comma) +
theme(axis.title.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.title = element_blank(),
axis.text.y = element_text(size = 12, colour = "black"),
axis.text.x = element_text(size = 12, colour = "black"),
axis.title.y = element_blank(),
legend.position = "top",
legend.key.height = unit(0.8, "lines"),
legend.key.width = unit(0.8, "lines"),
legend.text = element_text(size = 12, colour = "black", face = "bold")) +
labs(title= "Top Five Oil-Producing Countries (2012-2017)", subtitle = "Thousand Barrels Daily",
caption = "Source: BP Statistical Review") +
theme(title = element_text(size = 12)) +
theme(legend.position=c(0,1),
legend.direction="horizontal",
legend.justification=c(0, 1))
My Plot
Urban Institute's Plot

Resources