如何正确使用guide_legend来控制图例的行数
how to correctly use guide_legend to control the number of rows of legend
我正在使用 ggplot2 在 R 中绘制图形。图中的图例有两行,但我想将它们展开并排成一行。我四处搜索,发现 guide_legned()
可能是要走的路。我尝试了几种不同的方法,唯一不会出错的方法是这样的:
fig <- ggplot(data, aes(y=y, x=x, shape=z))+
geom_point(size = 4)+
scale_shape_manual(values = c(0,1,2,3,4,6))+
geom_abline('somestuff in here')+
scale_fill_continuous(guide=guide_legend(nrow=1))+
annotate('somestuff in here')+
theme_bw()+
theme(legend.title = element_blank(),
panel.grid = element_blank(),
legend.position='top',
legend.key = element_blank())
然而,图例仍然是2行。我想知道为什么 guide_legend(nrow=1)
不工作(即使没有错误)。正确的方法是什么?谢谢!
你有shape
审美,但你的情节没有fill
审美,所以scale_fill_continuous
在这里不适用。这是您要格式化的 shape
图例。两个选项:
fig <- ggplot(data, aes(y=y, x=x, shape=z))+
geom_point(size = 4)+
scale_shape_manual(values = c(0,1,2,3,4,6), guide=guide_legend(nrow=1)+
geom_abline('somestuff in here')+
annotate('somestuff in here')+
theme_bw()+
theme(legend.title = element_blank(),
panel.grid = element_blank(),
legend.position='top',
legend.key = element_blank())
fig <- ggplot(data, aes(y=y, x=x, shape=z))+
geom_point(size = 4)+
scale_shape_manual(values = c(0,1,2,3,4,6))+
geom_abline('somestuff in here')+
annotate('somestuff in here')+
theme_bw()+
theme(legend.title = element_blank(),
panel.grid = element_blank(),
legend.position='top',
legend.key = element_blank()) +
guides(shape=guide_legend(nrow=1))
我正在使用 ggplot2 在 R 中绘制图形。图中的图例有两行,但我想将它们展开并排成一行。我四处搜索,发现 guide_legned()
可能是要走的路。我尝试了几种不同的方法,唯一不会出错的方法是这样的:
fig <- ggplot(data, aes(y=y, x=x, shape=z))+
geom_point(size = 4)+
scale_shape_manual(values = c(0,1,2,3,4,6))+
geom_abline('somestuff in here')+
scale_fill_continuous(guide=guide_legend(nrow=1))+
annotate('somestuff in here')+
theme_bw()+
theme(legend.title = element_blank(),
panel.grid = element_blank(),
legend.position='top',
legend.key = element_blank())
然而,图例仍然是2行。我想知道为什么 guide_legend(nrow=1)
不工作(即使没有错误)。正确的方法是什么?谢谢!
你有shape
审美,但你的情节没有fill
审美,所以scale_fill_continuous
在这里不适用。这是您要格式化的 shape
图例。两个选项:
fig <- ggplot(data, aes(y=y, x=x, shape=z))+
geom_point(size = 4)+
scale_shape_manual(values = c(0,1,2,3,4,6), guide=guide_legend(nrow=1)+
geom_abline('somestuff in here')+
annotate('somestuff in here')+
theme_bw()+
theme(legend.title = element_blank(),
panel.grid = element_blank(),
legend.position='top',
legend.key = element_blank())
fig <- ggplot(data, aes(y=y, x=x, shape=z))+
geom_point(size = 4)+
scale_shape_manual(values = c(0,1,2,3,4,6))+
geom_abline('somestuff in here')+
annotate('somestuff in here')+
theme_bw()+
theme(legend.title = element_blank(),
panel.grid = element_blank(),
legend.position='top',
legend.key = element_blank()) +
guides(shape=guide_legend(nrow=1))