在 R 中绘图:水平列出图例项并在绘图下方居中 [英] plotly in R: Listing legend items horizontally and centered below a plot

查看:28
本文介绍了在 R 中绘图:水平列出图例项并在绘图下方居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在调整图例和 R 中的图例.我无法弄清楚的一件事是如何(如果可能的话)重新定位图例项,以便它们水平列出并居中位于图下方.默认的图例项是垂直放置的,位于绘图的右侧,如下所示:

plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, mode = "markers", color = Species)

我可以通过以下方式获得下面的图例并以情节为中心:

plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, mode = "markers", color = Species)%>% layout(legend = list(x = 0.35, y =-0.5))

但是,我注意到此图例位置会根据我查看绘图的方式(我制作绘图窗口的尺寸等)而变化.正因为如此,图例有时会意外地与情节重叠(通过定位太高)或与情节分开一个笨拙的大距离(通过定位太低).以下是图例位置过低的示例图片:

此外,将图例放置在图下方时,将图例项目水平(而不是垂直)列出可能会更好.在此示例中,最好在图例中从左到右(而不是从上到下)列出Virginica、versicolor 和 setosa.因此,理想情况下看起来像这样:

是否有可能在水平列出其项目时获得这个 - 即位于绘图中心和下方的图例(不随窗口大小改变位置)?

解决方案

根据

I have been tweaking legends in plotly and R. One thing I am unable to figure out is how (if it is possible) to reposition legend items so that they are listed horizontally and centered below the plot. The default legend items are positioned vertically and located to the right of the plot, as shown here:

plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, mode = "markers", color = Species)

I am able to get the legend below and centered to the plot by the following:

plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, mode = "markers", color = Species) %>% layout(legend = list(x = 0.35, y = -0.5))

However, I notice that this legend position changes based on how I view the plot (the dimensions I make the plot window, etc). Because of this, the legend will sometimes accidentally overlap the plot (by being positioned too high up) or be separated from the plot by an awkwardly-large distance (by being positioned too low). Here is an example image of the legend being positioned too low:

Moreover, when placing the legend below the plot, it may look better to have legend items listed horizontally (instead of vertically). In this example, it would be great to have virginica, versicolor, and setosa listed left to right in the legend (instead of top to bottom). Hence, ideally looking like this:

Is it possible to obtain this - that is, a legend positioned centered and below the plot (that does not change location with window size) while listing its items horizontally?

解决方案

According to documentation the following should do the trick:

data(iris)
plot_ly(data = iris, 
        x = ~Sepal.Length, 
        y = ~Petal.Length, 
        mode = "markers", 
        color = ~Species) %>%
layout(legend = list(orientation = "h",   # show entries horizontally
                     xanchor = "center",  # use center of legend as anchor
                     x = 0.5))             # put legend in center of x-axis

It sets the legend orientation to "horizontal", then sets the anchor (point which position we specify) to the center of the legend, and then positions it in the middle of the x-axis. Which results in the following graph:

这篇关于在 R 中绘图:水平列出图例项并在绘图下方居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆