-cowplot get_legend()当ggplot传说位置不在默认位置

问题描述 投票:0回答:1
cowplot

的传说,但是当我尝试使用

grid.draw()
包装中绘制the Legend时,它会产生一个空白。
grid
	
发现没有提取传说,因为
library(cowplot); library(patchwork); library(grid); library(tidyverse) #Data to create legend legend_data <- data.frame( julian = 299:366, PM25 = c(3.4,1.3,1.2,1.2,0.4,3.4,1.0,0.8,0.3,13.5,0.9,5.3,4.4,3.4,98.6,0.7,350.6,0.8,0.3,0.9,0.9,4.1,0.7,0.3,0.4,2.1,1.4,5.2,4.2,3.9,1.4,0.8,0.7,0.8,0.3,1.9,0.8,0.7,1.2,1.7,67.9,3.8,6.1,5.9,225.3,0.7,0.3,0.6,2.9,37.5,1.1,33.2,0.9,1.5,1.1,0.8,1.5,0.8,2.2,4.6,1.2,1.0,3.3,0.9,0.9,4.6,1.2,2.8), site_name = "community 1" ) legend_data$AQI <- case_when( legend_data$PM25 <= 9.0 ~ "1", legend_data$PM25 >= 9.1 & legend_data$PM25 <= 35.4 ~ "2", legend_data$PM25 >= 35.5 & legend_data$PM25 <= 55.4 ~ "3", legend_data$PM25 >= 55.5 & legend_data$PM25 <= 125.4 ~ "4", legend_data$PM25 >= 125.5 & legend_data$PM25 <= 225.4 ~ "5", legend_data$PM25 >= 225.5 ~ "6", TRUE ~ NA_character_ ) #Create plot legendplot <- ggplot(legend_data, aes(x=julian, y=AQI, fill=AQI)) + geom_tile() + scale_fill_manual( values = c("green", "yellow", "orange", "red", "purple", "maroon"), labels = c("1" = "Good", "2"="Moderate", "3" = "Unhealthy for\nSensitive Groups", "4" = "Unhealthy", "5" = "Very Unhealthy", "6" = "Hazardous") ) + theme(legend.position = "bottom") legendplot #Extract legend legend<-cowplot::get_legend(legendplot) grid.newpage() grid.draw(legend)

被设置为
legend.position
r ggplot2 cowplot
1个回答
0
投票
"bottom"

而不是

legend.position = "bottom"
时获得传奇
将值更改为以下值之一,以匹配

cowplot::get_plot_component(legendplot,"guide-box-bottom")

中提供的位置,如果您不使用默认值

cowplot::get_legend(legendplot)

guide-box

当,

theme(legend.position="")
legend.position
当,

guide-box-right
  1. legend.position="right"
    当,
    guide-box-left
  2. legend.position="left"
    当,
    guide-box-bottom
  3. legend.position="bottom"
  4. guide-box-top
  5. 
    
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.