如何强制图中的图例水平而不是垂直?

sac*_*cvf 2 r legend ggplot2

我希望情节中的图例是水平的(每个都彼此并排,而不是一个在另一个之上)

有没有办法从垂直变为水平?例子:

    mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight"))
    ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
   opts(legend.position = c(0, 1), title="Legend placement makes me sad")
Run Code Online (Sandbox Code Playgroud)

Did*_*rts 6

您可以通过legend.direction="horizontal"theme()or 中添加参数来获得水平图例opts()

theme(legend.direction="horizontal")
Run Code Online (Sandbox Code Playgroud)

旧版本

opts(legend.direction="horizontal")
Run Code Online (Sandbox Code Playgroud)


小智 5

创建图例时,您可以使用参数“horiz”,该参数自动为 FALSE

legend( ... , horiz=TRUE)
Run Code Online (Sandbox Code Playgroud)