簡體   English   中英

R lattice levelplot,更改背景顏色和自定義色標標簽

[英]R lattice levelplot, changing the background color and custom colorscale labels

我有一個格子 plot,我試圖改變它的背景,但似乎無法讓它工作。 我也在嘗試添加自定義熱圖標簽(從 1 到 5,並想添加一些文本。)

par(bg = "yellow")
levelplot(as.matrix(x = c(1:5),y = c(5:1))
)

在此處輸入圖像描述

感謝您的時間!

library(lattice)
trellis.par.set(background = list(col="yellow"))
levelplot(as.matrix(x = c(1:5),y = c(5:1)), 
          scales=list(x=list(at=1:5, labels=LETTERS[1:5])))

reprex package (v2.0.1) 創建於 2022-04-26

ggplot一樣, lattice是基於網格圖形,而不是基於 R 圖形,因此設置par不會有任何影響。 您可以更改晶格參數,或者您可以在事后使用grid.edit進行更改:

library(lattice)
library(grid)

levelplot(as.matrix(x = c(1:5), y = c(5:1)))
grid.edit("plot_01.background", gp = gpar(fill = "yellow"))

在此處輸入圖像描述

不用說,如果您正在尋找多種格式更改、簡單的注釋、自定義比例和豐富的文檔,您應該切換到 ggplot:

library(ggplot)

ggplot(data.frame(x = 1:5), aes(x, 0.5, fill = x)) +
  geom_tile() +
  geom_text(aes(label = x), size = 10) +
  coord_equal() +
  scale_x_continuous(limits = c(0.5, 5.5), expand = c(0, 0)) +
  scale_y_continuous(limits = c(0, 1), expand = c(0, 0)) +
  scale_fill_gradient2(low = "#ff77ff", mid = "white", high = "cyan",
                       midpoint = 3) +
  theme_classic(base_size = 18) +
  theme(plot.background = element_rect(fill = "#FFFF80"),
        plot.margin = margin(150, 50, 150, 50),
        legend.background = element_blank())

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM