ggplot图例:键相对于标签的位置

问题描述:

我正在使用ggplot制作一个图例,其图例水平位于图的上方.我的变量有多个图例(例如颜色,形状,线型).

I'm using ggplot to make a graph with the legend positioned horizontally above the plot. There are multiple legends for my variables (ie color, shape, linetype).

+ theme(legend.position = 'top', legend.direction = "horizontal", legend.box = "horizontal")

无论如何,是否有将每个图例的标题放在描述上方而不是侧面

Is there anyway to put the title of each legend above the descriptions rather than to the side

和/或

将键放在标签文本上方.

place the key above the label text.

是为每种美学效果添加guides(...)之类的东西吗?

Something like adding guides(...) for each aesthetic?

ggplot(diamonds, aes(depth, price, color = cut)) + 
  geom_point() +  
  theme(legend.position = 'top', 
        legend.direction = "horizontal", 
        legend.box = "horizontal") + 
  guides(color = guide_legend(title.position = "top", 
                              # hjust = 0.5 centres the title horizontally
                              title.hjust = 0.5,
                              label.position = "bottom"))