Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.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 ggplot2能否在一个图例中分别控制点尺寸和线尺寸(线宽)?_R_Graph_Ggplot2_Legend - Fatal编程技术网

R ggplot2能否在一个图例中分别控制点尺寸和线尺寸(线宽)?

R ggplot2能否在一个图例中分别控制点尺寸和线尺寸(线宽)?,r,graph,ggplot2,legend,R,Graph,Ggplot2,Legend,使用ggplot2绘制连接每组平均值的数据点和线组的示例,对于形状和线型,使用相同的aes进行映射: p <- ggplot(mtcars, aes(gear, mpg, shape = factor(cyl), linetype = factor(cyl))) + geom_point(size = 2) + stat_summary(fun.y = mean, geom = "line", size = 1) + scale_shape_manual(values = c

使用
ggplot2
绘制连接每组平均值的数据点和线组的示例,对于
形状和
线型,使用相同的
aes
进行映射:

p <- ggplot(mtcars, aes(gear, mpg, shape = factor(cyl), linetype = factor(cyl))) + 
  geom_point(size = 2) +
  stat_summary(fun.y = mean, geom = "line", size = 1) +
  scale_shape_manual(values = c(1, 4, 19))
这只是一个警告

> Warning message:
In guide_merge.legend(init, x[[i]]) : Duplicated override.aes is ignored.
如果我将
线型从
ggplot()
移到
stat\u summary()
中,似乎也没有什么区别。如果我只需要点符号,我可以从图例中删除线


p2独立设置这些属性似乎确实很困难。我只能想出一个办法。如果您的实际数据有很大不同,则可能需要对其进行调整。但我所做的是使用
override.aes
来设置点的大小。然后我进入并构建绘图,然后手动更改实际低级栅格对象中的线宽设置。这是密码

pp<-ggplot(mtcars, aes(gear, mpg, shape = factor(cyl), linetype = factor(cyl))) + 
  geom_point(size = 3) +
  stat_summary(fun.y = mean, geom = "line", size = 1) +
  scale_shape_manual(values = c(1, 4, 19)) +
  guides(shape=guide_legend(override.aes=list(size=5)))

build <- ggplot_build(pp)
gt <- ggplot_gtable(build)

segs <- grepl("geom_path.segments", sapply(gt$grobs[[8]][[1]][[1]]$grobs, '[[', "name"))
gt$grobs[[8]][[1]][[1]]$grobs[segs]<-lapply(gt$grobs[[8]][[1]][[1]]$grobs[segs], 
    function(x) {x$gp$lwd<-2; x})
grid.draw(gt)

pp确保单独图例的一种方法是为它们指定不同的名称(或其他可能妨碍它们组合在一起的差异)

下面是一个基于您提供的代码的示例:

    p <- ggplot(mtcars, aes(gear, mpg, shape = factor(cyl), linetype = factor(cyl))) + 
      geom_point(size = 2) +
      stat_summary(fun.y = mean, geom = "line", size = 1) +
      scale_shape_manual(name="Name 1", values = c(1, 4, 19))+
      scale_linetype_discrete(name="Name2")
    p

p我明白你的意思。我认为,这是一个适合您所寻找的解决方案。它将两个图例分开,但将它们并排放置。形状的标签和标题被省略,因此最右侧的标签同时对应于形状和线型

我将此作为一个单独的答案发布,因为我认为这两种方法对未来的读者都是有效的

   p2 <- ggplot(mtcars, aes(gear, mpg, shape = factor(cyl), 
                 linetype = factor(cyl))) + 
     geom_point(size = 2) +
     stat_summary(fun.y = mean, geom = "line", size = 1) +
     # blank labels for the shapes
     scale_shape_manual(name="", values = c(1, 4, 19), 
                        labels=rep("", length(factor(mtcars$cyl))))+
     scale_linetype_discrete(name="Cylinders")+
     # legends arranged horizontally
     theme(legend.box = "horizontal")+
     # ensure that shapes are to the left of the lines
     guides(shape = guide_legend(order = 1), 
            linetype = guide_legend(order = 2))
   p2

p2使用
grid
函数
grid.force()。因此,可以应用
grid.gedit
,并且可以使用一行代码实现对绘图的所需编辑。此外,我增加了图例关键点的宽度,以便清楚显示线段的不同线型

library(ggplot2)
library(grid)
p <- ggplot(mtcars, aes(gear, mpg, shape = factor(cyl), linetype = factor(cyl))) + 
  geom_point(size = 2) +
  stat_summary(fun.y = mean, geom = "line", size = 1) +
  scale_shape_manual(values = c(1, 4, 19)) +
  theme(legend.key.width = unit(1, "cm"))

p

grid.ls(grid.force())    # To get the names of all the grobs in the ggplot

# The edit - to set the size of the point in the legend to 4 mm
grid.gedit("key-[-0-9]-1-1", size = unit(4, "mm"))    
库(ggplot2)
图书馆(网格)

p+1用于深入了解这一点。至少你给了我一个编程的方法来做这件事——我已经厌倦了在r之外编辑每个情节只是为了调整图例。我将在其他数据上尝试你的方法,看看是否有其他想法同时出现。进一步尝试后,我现在明白你为什么说“神奇数字”。所以这里的诀窍是如何索引“引导框”,它并不总是grobs列表中的第8个元素。除此之外,这个方法在一个图例中给出了我想要的结果。你可以创建一个助手函数来进行查找。例如,
getgtables现在对于helper函数非常有用。它在plot函数中也能很好地工作。帮助我保存
grob
输出。注意:为了使事情更加复杂,
geom_路径。片段有时可能隐藏在
gTree
中(因此在这个答案中的
sapply()
命令中找不到)。我花了很长时间才弄明白如何在
gTree
中访问嵌套的
grob
;他们是它的
孩子
。因此语法看起来像
gt$grobs[[mn]][[1]][[1]]]$grobs[[i]]]$children
,其中
i
gt
中对每个
grob
命名的
“GRID.gTree”
进行索引。然后可以迭代回答中使用的类似过程来调整嵌套的
段。但在这里,我想保留一个传奇。毕竟,如果“名字1”和“名字2”都指的是同一个因素(本例中为cyl),那么它们又有什么区别呢?将两个传说伪装成一个传说是一个聪明的想法。它允许为每个图例单独设置
覆盖.aes
等。但是,由于线没有覆盖点符号,因此可能不再需要这样做。将图例标题偏移到一侧有点不令人满意-它看起来更适合居中,只是为了挑剔。但是感谢您的解决方案。您的建议很有效,但是当我使用
ggsave(plot=p,file=“test.PDF”)
将图形保存为PDF时,符号与应用
grid.gedit()。一种方便的方法是对屏幕上的对象执行grid.grab()操作,然后保存。即插图g
    p <- ggplot(mtcars, aes(gear, mpg, shape = factor(cyl), linetype = factor(cyl))) + 
      geom_point(size = 2) +
      stat_summary(fun.y = mean, geom = "line", size = 1) +
      scale_shape_manual(name="Name 1", values = c(1, 4, 19))+
      scale_linetype_discrete(name="Name2")
    p
   p2 <- ggplot(mtcars, aes(gear, mpg, shape = factor(cyl), 
                 linetype = factor(cyl))) + 
     geom_point(size = 2) +
     stat_summary(fun.y = mean, geom = "line", size = 1) +
     # blank labels for the shapes
     scale_shape_manual(name="", values = c(1, 4, 19), 
                        labels=rep("", length(factor(mtcars$cyl))))+
     scale_linetype_discrete(name="Cylinders")+
     # legends arranged horizontally
     theme(legend.box = "horizontal")+
     # ensure that shapes are to the left of the lines
     guides(shape = guide_legend(order = 1), 
            linetype = guide_legend(order = 2))
   p2
library(ggplot2)
library(grid)
p <- ggplot(mtcars, aes(gear, mpg, shape = factor(cyl), linetype = factor(cyl))) + 
  geom_point(size = 2) +
  stat_summary(fun.y = mean, geom = "line", size = 1) +
  scale_shape_manual(values = c(1, 4, 19)) +
  theme(legend.key.width = unit(1, "cm"))

p

grid.ls(grid.force())    # To get the names of all the grobs in the ggplot

# The edit - to set the size of the point in the legend to 4 mm
grid.gedit("key-[-0-9]-1-1", size = unit(4, "mm"))    
  g <- grid.grab()
  ggsave(plot=g, file="test.pdf")