使用中断时的ggplot图例大小

问题描述

如果我在这个数据框上使用 ggplot

# time = a list of times in format eg 01:23:45
x_values = [datetime.datetime.strptime(d,"%H:%M:%s").time() for d in time]
x_values = [datetime.datetime.strptime(d,"%H:%M:%s").time() for d in time]
my_day = datetime.date(2019,3,2)
x_dt = [datetime.datetime.combine(my_day,t) for t in x_values]

tick_spacing = 0.08
fig,ax = plt.subplots(1,1)
ax.plot(x_dt,magnitude_list)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
plt.title('88992 Light Curve')
plt.ylabel('Magnitude')
plt.xlabel('Time')
plt.figure(figsize=(30,10))
plt.show()

图例框在变量 I 中保留 FALSE 级别的空间。我尝试使用 library(ggplot2) date <- as.Date(c("2019-12-18","2019-12-19","2019-12-20","2019-12-23","2019-12-24","2019-12-26","2019-12-27","2019-12-30")) measures <- c( 0.0000000000,0.0012376239,-0.0024768434,0.0024768434,0.0043196611,0.0091939970,-0.0006103144,-0.0030571104) theData <- data.frame(date,measures,I=measures<0) ggplot(theData) + geom_point( aes(x=date,y=measures,col=as.factor(I),size=factor(I))) + scale_size_discrete(range = c(0.8,2),guide="none") + scale_color_manual(values = c("grey80","steelblue4"),breaks = c("TRUE"),labels = c("negative")) + labs( col="") + theme(legend.position = c(0.2,0.8)) # ) 调整图例框的大小,但没有成功。如果传说的背景和剧情的背景不一样,就很难看。有什么建议吗?

解决方法

图例顶部的空白间隙的原因不是未使用的因子水平“FALSE”留下的空间,而是由于labs( col="")而造成的空行。 消除图例标题的正确方法是legend.title = element_blank()。可以通过 legend.margin 获得对标签边距的进一步微调。 我完整的 ggplot 命令是:

ggplot(theData) +  geom_point( aes(x=date,y=measures,col=as.factor(I),size=factor(I))) +
scale_size_discrete(range = c(0.8,2),guide="none") +
scale_color_manual(values = c("grey80","steelblue4"),breaks = c("TRUE"),labels = c("negative")) + 
theme(legend.position = c(0.2,0.8),legend.title = element_blank(),legend.margin = margin(0,0.3,0.2,"cm"))