【发布时间】:2020-09-20 17:07:36
【问题描述】:
我在 ggplot 中显示图例时遇到问题。我的数据每天都在变化,有时我有大约 20 个数据点,有时只有 5 个数据点。
图例超出了图表的限制。但始终将其绘制成低比例并不是一个好的选择。
这是一个情节:
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)
在方形画布上输出它会将图例写入范围之外。 是否可以创建一种自动缩放?
2020 年 6 月 8 日更新:
曲线的数量不同。所以图例改变了,我需要手动调整大小。我可能会通过计算每个元素的字符来计算大小,并尝试估计可能的缩放比例。不过如果 ggplot2 自己做就好了。
【问题讨论】: