我有以下ggplot: -
library(dplyr)
library(ggplot2)
library(ggtext)
library(ggdist)
set.seed(1)
DF = rbind(data.frame('Label' = 'A', val = rnorm(200, 5)), data.frame('Label' = 'B', val = rnorm(500, 10)))
DF %>%
ggplot(aes(x=Label, y=val, fill=Label, alpha = 1))
stat_dots()
geom_textbox(x = -Inf, y = -Inf, label = 'My text', width = unit(0.4, "npc"), height = unit(0.04, "npc"), box.margin = unit(c(1, 1, 1, 1), "pt"))
無論繪圖視窗大小如何,我都想修復視窗區域textbox處的位置bottom-left。
然而,上面的代碼未能實作相同的目標。我的繪圖視窗出現以下錯誤
Error in grid.Call.graphics(C_upviewport, as.integer(n)) :
cannot pop the top-level viewport ('grid' and 'graphics' output mixed?)
我正在將 R 與MacOS.
任何指標如何解決這個位置textbox的bottom-left位置將是非常有益的。
uj5u.com熱心網友回復:
確保 geom_textbox 中的 aes 和資料覆寫 ggplot()。
library(ggplot2)
library(ggtext)
library(ggdist)
set.seed(123)
DF <- rbind(data.frame('Label' = 'A', val = rnorm(200, 5)),
data.frame('Label' = 'B', val = rnorm(500, 10)))
ggplot(DF, aes(Label, val))
stat_dots(aes(fill = Label))
geom_textbox(aes(-Inf, -Inf, hjust = 0, vjust = 0, label = "My text"),
data.frame())

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/327580.html
上一篇:根據條件創建新變數
