r ggplot2图例:如何在符号下方显示线型?

r ggplot2图例:如何在符号下方显示线型?,r,plot,ggplot2,legend,R,Plot,Ggplot2,Legend,我正在绘制一个图形,它使用符号和线型来区分组(分别是点和95%置信椭圆) 以下是具有类似图例的示例图: bplot<-ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,group=Species,shape=Species,lty=Species))+ geom_point(size=3)+geom_smooth(method="lm",se=F,color="black")+ theme_minimal()+theme(lege

我正在绘制一个图形,它使用符号和线型来区分组(分别是点和95%置信椭圆)

以下是具有类似图例的示例图:

  bplot<-ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,group=Species,shape=Species,lty=Species))+
    geom_point(size=3)+geom_smooth(method="lm",se=F,color="black")+
    theme_minimal()+theme(legend.key.size=unit(1.2,"cm"))
  bplot

bplot您需要
指南
功能。请仔细阅读文档以了解更多自定义信息

bplot<-ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,group=Species,shape=Species,lty=Species))+
  geom_point(size=3)+geom_smooth(method="lm",se=F,color="black")+
  theme_minimal()+theme(legend.key.size=unit(1.2,"cm")) + 
  guides(
    shape = guide_legend(order = 1),
    size = guide_legend(order = 2)
  )
bplot

bplot您需要
指南
功能。请仔细阅读文档以了解更多自定义信息

bplot<-ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,group=Species,shape=Species,lty=Species))+
  geom_point(size=3)+geom_smooth(method="lm",se=F,color="black")+
  theme_minimal()+theme(legend.key.size=unit(1.2,"cm")) + 
  guides(
    shape = guide_legend(order = 1),
    size = guide_legend(order = 2)
  )
bplot

bplot我认为如果钥匙稍微加宽一点,图例是可读的

library(ggplot2)

bplot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, group = Species,
                          shape = Species,lty = Species)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = F, color = "black") +
  theme_minimal() +
  theme(legend.key.width = unit(1.5, "cm"))

bplot
库(ggplot2)

如果钥匙稍微加宽一点,我认为图例是可读的

library(ggplot2)

bplot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, group = Species,
                          shape = Species,lty = Species)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = F, color = "black") +
  theme_minimal() +
  theme(legend.key.width = unit(1.5, "cm"))

bplot
库(ggplot2)

bplot在这里的一篇文章中给出了一个在ggplot grob中编辑特定grob的选项。它包括沿着一条路径通过列表中的长列表到达相关的grob。有时,使用
grid
编辑功能编辑相关的grob更容易(尽管可能只是稍微容易)

library(ggplot2)
library(grid)

# The plot
bplot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, group = Species,
                          shape = Species,lty = Species)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = F, color = "black") +
  theme_minimal() +
  theme(legend.key.width = unit(1.5, "cm"),
        legend.key.height = unit(1, "cm"),
        legend.key = element_rect(colour = "grey50", size = .5))

# Get the plot grob
g = ggplotGrob(bplot)

# Get a list of the grobs 
grid.ls(grid.force(g)) 

这里的一篇文章中给出了在ggplot grob中编辑特定grob的一个选项。它包括沿着一条路径通过列表中的长列表到达相关的grob。有时,使用
grid
编辑功能编辑相关的grob更容易(尽管可能只是稍微容易)

library(ggplot2)
library(grid)

# The plot
bplot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, group = Species,
                          shape = Species,lty = Species)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = F, color = "black") +
  theme_minimal() +
  theme(legend.key.width = unit(1.5, "cm"),
        legend.key.height = unit(1, "cm"),
        legend.key = element_rect(colour = "grey50", size = .5))

# Get the plot grob
g = ggplotGrob(bplot)

# Get a list of the grobs 
grid.ls(grid.force(g)) 

试试
legend.key.width=unit(1.5,“cm”)
legend.key.width控制图例背景的宽度,在这种情况下没有帮助。legend.key.width控制图例背景的宽度。通过使键变宽,可以看到更多的线条,因此线条类型是可读的。这不是你想要的吗?如果你想用一个标签把形状和线型分开,我想你需要深入研究绘图区的结构。我相信这是可以做到的。是的,我希望用一个标签将形状与线型分开。图例。键。宽度=单位(1.5,“cm”)
图例。键。宽度控制图例背景的宽度,在这种情况下没有帮助。图例。键。宽度控制图例键背景的宽度。通过使键变宽,可以看到更多的线条,因此线条类型是可读的。这不是你想要的吗?如果你想用一个标签把形状和线型分开,我想你需要深入研究绘图区的结构。我相信这是可以做到的。是的,我想用一个标签把形状和线型分开。我想让它们保持在同一个图例中。我想让它们保持在同一个图例中