只有一个类别的 ggplot2 图例/只有形状没有比例
ggplot2 legend with only one category / with only the shape and no scale
如何让图例的一部分只有一个类别?我试图弄乱 override.aes
但没有成功。或者,所需的输出可以被视为只有形状但没有比例的图例。
ggplot(iris) +
geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=Sepal.Length))+
scale_size_continuous("Legend with \n only 1 circle ",range = c(5,10))+
guides(size = guide_legend( override.aes=list(range= c(1,5))))
product I am trying to get to:
类型的说明
点已按比例缩放,但图例未报告比例。
只需在秤中创建一个 break
。您也可以为其添加自定义标签(此处为 ""
)。您还可以使用您选择的中断来控制图例中点的大小。
那里有 scale_color_discrete()
线,否则 1 点的图例将位于顶部,而不是您想要的图片中的内容。
require(ggplot2)
g <- ggplot(iris) + geom_point(aes(x = Sepal.Width, y = Sepal.Length,
color = Species, size = Sepal.Length)) +
scale_color_discrete(name = "Color") +
scale_size_continuous(name = "Legend with \n only 1 circle",
breaks = 5, labels = "")
虽然@choff 解决方案是我给出的示例的最佳解决方案,但我最终使用的解决方案略有不同,因为我需要控制圆圈的范围大小。
ggplot(iris) +
geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=Sepal.Length))+
scale_size_continuous("Legend with \n only 1 circle ",range = c(5,10), labels=c("","","","",""))+
guides(size =guide_legend( override.aes=list(size=c(4,0,0)))) +
theme(legend.key = element_blank())
从您的目标地图来看,您似乎希望图例按颜色和形状分隔图表符号。大小仅用于设置符号的大小。此外,您的数据可能会有一个单独的列,分别代表有投资的国家和没有投资的国家。因此,我们可以向 iris 添加一列,用两个值分隔行,将颜色和形状映射到该列,然后在单个组合图例中显示这两种美学的图例。代码如下:
sp <- ggplot(transform(iris, Flower_size = ifelse(Petal.Width < 1, "Small Flower","Big Flower")))
sp <- sp + geom_point(aes(x=Sepal.Width, y=Sepal.Length, fill=Flower_size, shape=Flower_size, size=Sepal.Length), colour=NA)
sp <- sp + scale_size_continuous(range = c(4,7))
sp <- sp + scale_shape_manual(values=c(21, 22))
sp <- sp + scale_fill_manual(values=c("grey", "orange"))
sp <- sp + labs(fill="", shape="")
sp <- sp + guides(size=FALSE, fill=guide_legend(override.aes=list(size=7)))
sp <- sp + theme(legend.text=element_text(size=12))
plot(sp)
如何让图例的一部分只有一个类别?我试图弄乱 override.aes
但没有成功。或者,所需的输出可以被视为只有形状但没有比例的图例。
ggplot(iris) +
geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=Sepal.Length))+
scale_size_continuous("Legend with \n only 1 circle ",range = c(5,10))+
guides(size = guide_legend( override.aes=list(range= c(1,5))))
product I am trying to get to:
点已按比例缩放,但图例未报告比例。
只需在秤中创建一个 break
。您也可以为其添加自定义标签(此处为 ""
)。您还可以使用您选择的中断来控制图例中点的大小。
那里有 scale_color_discrete()
线,否则 1 点的图例将位于顶部,而不是您想要的图片中的内容。
require(ggplot2)
g <- ggplot(iris) + geom_point(aes(x = Sepal.Width, y = Sepal.Length,
color = Species, size = Sepal.Length)) +
scale_color_discrete(name = "Color") +
scale_size_continuous(name = "Legend with \n only 1 circle",
breaks = 5, labels = "")
虽然@choff 解决方案是我给出的示例的最佳解决方案,但我最终使用的解决方案略有不同,因为我需要控制圆圈的范围大小。
ggplot(iris) +
geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=Sepal.Length))+
scale_size_continuous("Legend with \n only 1 circle ",range = c(5,10), labels=c("","","","",""))+
guides(size =guide_legend( override.aes=list(size=c(4,0,0)))) +
theme(legend.key = element_blank())
从您的目标地图来看,您似乎希望图例按颜色和形状分隔图表符号。大小仅用于设置符号的大小。此外,您的数据可能会有一个单独的列,分别代表有投资的国家和没有投资的国家。因此,我们可以向 iris 添加一列,用两个值分隔行,将颜色和形状映射到该列,然后在单个组合图例中显示这两种美学的图例。代码如下:
sp <- ggplot(transform(iris, Flower_size = ifelse(Petal.Width < 1, "Small Flower","Big Flower")))
sp <- sp + geom_point(aes(x=Sepal.Width, y=Sepal.Length, fill=Flower_size, shape=Flower_size, size=Sepal.Length), colour=NA)
sp <- sp + scale_size_continuous(range = c(4,7))
sp <- sp + scale_shape_manual(values=c(21, 22))
sp <- sp + scale_fill_manual(values=c("grey", "orange"))
sp <- sp + labs(fill="", shape="")
sp <- sp + guides(size=FALSE, fill=guide_legend(override.aes=list(size=7)))
sp <- sp + theme(legend.text=element_text(size=12))
plot(sp)