Control ggplot2 legend look without affecting the plot

To change line width only in the legend you should use function guides() and then for colour= use guide_legend() with override.aes= and set size=. This will override size used in plot and will use new size value just for legend. ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw()+ guides(colour = guide_legend(override.aes = list(size=3))) To get points in legend and lines in plot … Read more

ggplot2 keep unused levels barplot

You need to set drop=FALSE on both scales (fill and x) like this: library(ggplot2) df <- data.frame(type=c(“A”, “A”, “A”, “B”, “B”), group=rep(“group1”, 5)) df1 <- data.frame(type=c(“A”, “A”, “A”, “B”, “B”, “A”, “A”, “C”, “B”, “B”), group=c(rep(“group1”, 5),rep(“group2”, 5))) df$type <- factor(df$type, levels=c(“A”,”B”, “C”)) df1$type <- factor(df1$type, levels=c(“A”,”B”, “C”)) plt <- ggplot(df, aes(x=type, fill=type)) + geom_bar(position=’dodge’) … Read more

Is there a way to change the spacing between legend items in ggplot2?

ggplot2 v3.0.0 released in July 2018 has working options to modify legend.spacing.x, legend.spacing.y and legend.text. Update Dec 2021 – to make legend.spacing.y work, you will need to set byrow = TRUE in the corresponding guide_legend. See also this thread. Example below. Example: Increase horizontal spacing between legend keys library(ggplot2) ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) + … Read more

Moving matplotlib legend outside of the axis makes it cutoff by the figure box

Sorry EMS, but I actually just got another response from the matplotlib mailling list (Thanks goes out to Benjamin Root). The code I am looking for is adjusting the savefig call to: fig.savefig(‘samplefigure’, bbox_extra_artists=(lgd,), bbox_inches=”tight”) #Note that the bbox_extra_artists must be an iterable This is apparently similar to calling tight_layout, but instead you allow savefig … Read more

Add a common Legend for combined ggplots

You may also use ggarrange from ggpubr package and set “common.legend = TRUE”: library(ggpubr) dsamp <- diamonds[sample(nrow(diamonds), 1000), ] p1 <- qplot(carat, price, data = dsamp, colour = clarity) p2 <- qplot(cut, price, data = dsamp, colour = clarity) p3 <- qplot(color, price, data = dsamp, colour = clarity) p4 <- qplot(depth, price, data = … Read more

Plot labels at ends of lines

A newer solution is to use ggrepel: library(ggplot2) library(ggrepel) library(dplyr) temp.dat %>% mutate(label = if_else(Year == max(Year), as.character(State), NA_character_)) %>% ggplot(aes(x = Year, y = Capex, group = State, colour = State)) + geom_line() + geom_label_repel(aes(label = label), nudge_x = 1, na.rm = TRUE)

Secondary axis with twinx(): how to add to legend?

You can easily add a second legend by adding the line: ax2.legend(loc=0) You’ll get this: But if you want all labels on one legend then you should do something like this: import numpy as np import matplotlib.pyplot as plt from matplotlib import rc rc(‘mathtext’, default=”regular”) time = np.arange(10) temp = np.random.random(10)*30 Swdown = np.random.random(10)*100-10 Rn … Read more