[英]Text aesthetic with paste0 function in ggplot2 alters geom_bar fill order; How to fix it?
我正在嘗試制作一個堆疊條形圖來映射一個變量以在 log10 尺度上填充。 我想通過 ggplotly 傳遞它,以便通過工具提示進行數據檢查。
有兩個問題。 首先,當我在 scale_fill_gradientn 中記錄轉換 VAR.B 的比例時,工具提示顯示轉換后的數據,而圖形以其原始比例顯示數據,這是無用的。
但是,當我在 ggplot 中包含文本美學以解決此問題時,它會破壞填充順序。 我一直無法找到解決這兩個問題的方法。
我已經嘗試在數據幀本身中對 VAR.B 進行 log10 轉換。 在這種情況下,工具提示與顯示的數據相匹配,但我認為這對我的觀眾來說並不容易。 此外,將數據集保留為線性比例會丟失故事的重要部分。
數據集
a<-structure(list(VAR.A = c("A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"),
VAR.B = c(1, 2, 3, 5, 8, 9, 10, 12, 13, 15, 1, 10, 30, 35,
40, 60, 80, 100, 140, 160), rel.freq = c(3.076923077, 4.615384615,
7.692307692, 12.30769231, 15.38461538, 6.153846154, 30.76923077,
3.076923077, 7.692307692, 9.230769231, 1.754385965, 3.50877193,
26.31578947, 1.754385965, 17.54385965, 35.0877193, 3.50877193,
5.263157895, 3.50877193, 1.754385965)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -20L), spec = structure(list(
cols = list(VAR.A = structure(list(), class = c("collector_character",
"collector")), VAR.B = structure(list(), class = c("collector_double",
"collector")), counts = structure(list(), class = c("collector_double",
"collector")), rel.freq = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
依賴關系
library(ggplot2)
library(viridis)
library(plotly)
library(scales)
此圖看起來應該如此,但 VAR.B 的懸停文本中顯示的值與原始比例不匹配
f <- ggplot(a, aes(x=VAR.A, y= rel.freq, fill = VAR.B)) +
geom_bar(width = 1, size = 1, stat = "identity") +
scale_fill_gradientn(colors = viridis(10, option = 'inferno'), limits = c(0.1, 160), breaks = c(0.1,0.3, 1, 3, 10, 30, 100),
trans = "log10", guide = guide_colorbar(draw.llim = FALSE, draw.ulim = FALSE), oob = squish) +
theme_classic()
f<- ggplotly(f)
f
此圖看起來無序,但 VAR.B 的懸停文本中顯示的值確實與原始比例相符。
g <- ggplot(a, aes(x=VAR.A, y= rel.freq, fill = VAR.B, text = paste0('VAR.B:', VAR.B))) + geom_bar(width = 1, size = 1, stat = "identity") +
scale_fill_gradientn(colors = viridis(10, option = 'inferno'), limits = c(0.1, 160), breaks = c(0.1,0.3, 1, 3, 10, 30, 100),
trans = "log10", guide = guide_colorbar(draw.llim = FALSE, draw.ulim = FALSE), oob = squish) +
theme_classic()
g <- ggplotly(g, tooltip = c('VAR.A','VAR.B','text'))
g
如果我將 paste0() 函數排除在文本美學之外而只調用 VAR.B 本身,那么工具提示會以原始比例顯示數據,並保留填充順序。 但是在這種情況下,工具提示沒有為數據提供標簽。
h <- ggplot(a, aes(x=VAR.A, y= rel.freq, fill = VAR.B, text = VAR.B)) +
geom_bar(width = 1, size = 1, stat = "identity") +
scale_fill_gradientn(colors = viridis(10, option = 'inferno'), limits = c(0.1, 160), breaks = c(0.1,0.3, 1, 3, 10, 30, 100),
trans = "log10", guide = guide_colorbar(draw.llim = FALSE, draw.ulim = FALSE), oob = squish) +
theme_classic()
h <- ggplotly(h, tooltip = c('VAR.A','VAR.B','text'))
h
在我看來,文本美學中的 paste0() 函數被竊聽了。 如果有人能想出另一種方法同時解決所有這些問題,我將不勝感激。
發生這種情況是因為text = paste0('VAR.B:', VAR.B)))
創建了一個按字母順序排列的因子。
i <- ggplot(a, aes(x=VAR.A, y= rel.freq, fill = VAR.B,
text = factor(paste0('VAR.Bt:', VAR.B)[order(VAR.A,VAR.B)],
levels=unique(paste0('VAR.Bt:', VAR.B)[order(VAR.A,VAR.B)]),
ordered = T) #makes the factor specifically ordered
)
) +
geom_bar(width = 1, size = 1, stat = "identity",
position = position_stack(reverse = T) #has to be reversed, so high values of VAR.B appear on top
) +
scale_fill_gradientn(colors = viridis(10, option = 'inferno'),
limits = c(0.1, 160),
breaks = c(0.1,0.3, 1, 3, 10, 30, 100),
trans = "log10",
guide = guide_colorbar(draw.llim = FALSE, draw.ulim = FALSE),
oob = squish) +
theme_classic()
i <- ggplotly(i, tooltip = c('VAR.A','VAR.B','text'))
i
希望這會有所幫助:-) 我編輯了 t,所以更明顯的是哪個調用產生了什么
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.