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?
Related
I have visualized some data as a donut chart. Here is the code:
ggplot(region_average, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=state_code)) +
geom_rect() +
geom_label_repel( x=3.5, aes(y=labelPosition, label=label), size=3) +
scale_fill_brewer(palette=4) +
coord_polar(theta="y") +
xlim(c(2, 4)) +
theme(legend.position = "none",
panel.grid = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank()) +
labs(title = "Regional Distribution of Ad Reach",
subtitle = "Overall distribution for all ads studied")+
theme(
panel.grid = element_blank(),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
axis.text = element_text(color=txt_col, size=8, hjust = 0.4),
axis.ticks = element_blank(),
axis.title.y.left = element_blank(),
panel.background = element_rect(fill = bg, color = bg),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.border=element_blank(),
plot.title = element_text(hjust=0,size=24, color=txt_col,lineheight=.8, face="bold", margin=margin(0,0,0,0)),
plot.subtitle = element_text(hjust=0,size=14, color=txt_col, margin=margin(5,0,20,0)),
plot.title.position = "plot",
plot.caption = element_text(hjust=.5, margin=margin(20,0,0,0), size=8, color=txt_col, face="bold"),
plot.background = element_rect(color=bg, fill=bg),
plot.margin = margin(30,30,30,30),
legend.title = element_blank(),
legend.position = "none"
)
This is the picture it produces
How can I get rid of the 2.0 ... 4.0 on the left hand side and the 0.00/1 ...0.75 around the donut?
Thank you!
As #Allan Cameron says in the comments, axis.text = element_blank()resolves the issue.
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.
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
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.
I'm using the code below to try to create a PNG image that is only the image but there is still some text and a white border.
theplot <- data %>% ggplot(mapping = aes(x,y)) +
geom_point(mapping = aes(color=z), alpha = alpha, size = 0.75) +
scale_color_gradient(low="green", high="blue") +
theme_void() + theme(legend.position="none") + theme(axis.title = element_blank())
I've also tried the following.
theplot <- data %>% ggplot(mapping = aes(x,y)) +
geom_point(mapping = aes(color=z), alpha = alpha, size = 0.75) +
scale_color_gradient(low="green", high="blue") +
theme_void() + theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank())
I'm not very familiar with R so I'm not sure if ggplot is what I should be using to create just an image.
Is it ok like this ?
library(ggplot2)
library(ggthemes)
ggplot(mtcars, aes(mpg, wt)) + geom_point() +
theme(plot.margin=unit(c(0,0,0,0), unit="mm"))
+ theme_fivethirtyeight()
ggsave("myplot.png")
One can take a look at the code of theme_fivethirtyeight:
> theme_fivethirtyeight
function (base_size = 12, base_family = "sans")
{
(theme_foundation(base_size = base_size, base_family = base_family) +
theme(line = element_line(colour = "black"), rect = element_rect(fill = ggthemes_data$fivethirtyeight["ltgray"],
linetype = 0, colour = NA), text = element_text(colour = ggthemes_data$fivethirtyeight["dkgray"]),
axis.title = element_blank(), axis.text = element_text(),
axis.ticks = element_blank(), axis.line = element_blank(),
legend.background = element_rect(), legend.position = "bottom",
legend.direction = "horizontal", legend.box = "vertical",
panel.grid = element_line(colour = NULL), panel.grid.major = element_line(colour = ggthemes_data$fivethirtyeight["medgray"]),
panel.grid.minor = element_blank(), plot.title = element_text(hjust = 0,
size = rel(1.5), face = "bold"), plot.margin = unit(c(1,
1, 1, 1), "lines"), strip.background = element_rect()))
}
I must say I don't understand why that doesn't work with your custom theme. I don't see a major difference.