Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何通过设置“删除剩余空间”;主题(legend.title=element_blank())&x201C;在ggplot2中_R_Ggplot2_Title_Legend - Fatal编程技术网

R 如何通过设置“删除剩余空间”;主题(legend.title=element_blank())&x201C;在ggplot2中

R 如何通过设置“删除剩余空间”;主题(legend.title=element_blank())&x201C;在ggplot2中,r,ggplot2,title,legend,R,Ggplot2,Title,Legend,那里 我想删除ggplot2中的图例标题。我使用了legend.title=element_blank(),但在图例框的顶部,图例标题最初所在的位置仍然有一些额外的空间。事实上,我不想要那个空间。有人有办法解决这个问题吗?谢谢 下面是复制多余空间的代码: library(ggplot2) library(grid) df1 <- data.frame( sex = factor(c("Female","Female","Male","Male")), time = factor(c

那里

我想删除ggplot2中的图例标题。我使用了legend.title=element_blank(),但在图例框的顶部,图例标题最初所在的位置仍然有一些额外的空间。事实上,我不想要那个空间。有人有办法解决这个问题吗?谢谢

下面是复制多余空间的代码:

library(ggplot2)
library(grid)
df1 <- data.frame(
  sex = factor(c("Female","Female","Male","Male")),
  time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
  total_bill = c(13.53, 16.81, 16.24, 17.42)
)

# A basic graph
lp <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex)) + geom_line() + geom_point()

lp + theme(legend.title=element_blank(),
           legend.background = element_rect(colour = "black", size = 0.1),
           legend.key.size = unit(0.4, "lines"))
库(ggplot2)
图书馆(网格)
df1

p + theme(legend.position="none")

其中
p
是ggplot。

这是我有史以来的第一个答案。 主题(图例。边距=边距(t=-0.25,l=0.05,b=0.0,r=0.05,单位为厘米))
负片将空格向内移动。

是否有代码明确显示您尝试过的任何内容?顶部的空格略多于底部,但这不是因为调整了键的大小。例如,如果将
fill=“red”
添加到
legend.background=element\u rect()
中,则更容易看出这是任意大小的(并且
legend.title=element\u blank()。您应该提到,您已经从中获取了绘图示例,并且需要运行
library(grid)
才能使用
unit()
@LJW谢谢您的评论。我现在添加url和库(网格)。你的意思是没有办法减少顶部的空间吗?我不确定这是否可能——我想一定是某种原因。不幸的是,这超出了我的技能水平。这可能有助于缩小空白的来源:。这将删除整个图例,OP希望保留图例,但删除标题,尤其是它留下的空白。