I have a problem showing the legend in a ggplot. My data changes every day, and sometimes I have about 20 data points, sometimes only 5 data points.
The plot legend exceeds the limits of the diagram. But plotting it low scaled all the time is not a good option.
Here is a plot:
require(data.table)
require(ggplot2)
df <- data.table(name = c("A12345678901234567890", "B12345678901234567890", "C12345678901234567890", "D12345678901234567890", "E12345678901234567890", "F12345678901234567890", "G12345678901234567890", "H12345678901234567890", "I12345678901234567890"), val = runif(n = 9))
plotcolors <- rainbow(df[,.N, by = name][,.N], end = 0.65)
p <- ggplot(data = df)
p <- p +
scale_fill_manual(values = plotcolors) +
aes(x = name, y = val, fill = name) +
theme(axis.text.x=element_blank(), legend.position = "bottom", text = element_text(size = 10), legend.text = element_text(size = 10), legend.title = element_text(size = 10), plot.margin = margin(10, 10, 10, 10, "pt")) +
xlab(NULL) +
scale_y_continuous()
p <- p + geom_bar(stat = "identity", colour = "black")
print(p)
Outputting it on a square canvas writes the legend off range. Is it possible to create a kind of auto scale?
Update 2020-06-08:
This is how the plot looks like:
The amount of curves differs. So the legend changes, and I need to adjust the size manually. I might calculate the size by counting the characters for each element and try to estimate the possible scaling. But it would be nice, if ggplot2 does it by itself.