我有以下ggplot
library(ggplot2)
library(grid)
theme_set(theme_bw()
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position=c(0,1),
legend.justification = c(0,1),
legend.box.margin = margin(5, l = 5, unit = 'mm'),
legend.box = 'horizontal'
))
ggplot(diamonds,aes(x,y,color=z))
geom_point()
scale_colour_gradient2('Time [min]',
low='lightgray',
mid='red3',
high='red4',
midpoint=15)
有了這個,我得到了下面的情節,

但是我想水平對齊圖例中的顏色定義,而顏色標題(即 Time [min])應該在顏色定義的頂部。有什么辦法可以做到這一點
uj5u.com熱心網友回復:
通過添加legend.direction = 'horizontal'和一些guides,
ggplot(diamonds,aes(x,y,color=z))
geom_point()
scale_colour_gradient2('Time [min]',
low='lightgray',
mid='red3',
high='red4',
midpoint=15)
theme_bw()
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position=c(0,1),
legend.justification = c(0,1),
legend.box.margin = margin(5, l = 5, unit = 'mm'),
legend.box = 'horizontal',
legend.direction = 'horizontal'
)
guides(colour = guide_colourbar(title.position="top", title.hjust = 0.5),
size = guide_legend(title.position="top", title.hjust = 0.5))

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/487993.html
上一篇:將總數添加到ggplot圖例
