简体   繁体   中英

Text aesthetic with paste0 function in ggplot2 alters geom_bar fill order; How to fix it?

I'm attempting to make a stacked bar chart mapping a variable to fill on a log10 scale. I'd like to pass it through ggplotly, in order to have data inspection possible via tooltip.

There are two problems. First, when I log transform the scale of VAR.B in scale_fill_gradientn, the tooltips display transformed data, while the graph displays data in its original scale, which is unhelpful.

However, when I include the text aesthetic in ggplot to fix this issue, it breaks the order of the fill. I haven't been able to find a way to fix both issues.

I have tried log10 transforming VAR.B in the dataframe itself. In this case, the tooltips match the displayed data, but I don't think this will be easily approachable for my audience. Also, leaving the dataset in a linear scale loses an important part of the story.

Dataset

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"))

Dependencies

library(ggplot2)
library(viridis)
library(plotly)
library(scales)

This graph looks like it should, but the values displayed in hover text for VAR.B don't match the original scale

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

This graph looks disordered, but the values displayed in hover text for VAR.B do match the original scale.

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

If I leave the paste0() function out of the text aesthetic and only call VAR.B itself, then the tooltips display data in the original scale and the order of the fill is preserved. But the tooltip doesn't give a label for the data in this case.

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

It seems to me that the paste0() function in the text aesthetic is bugged. If anyone can think of another way to fix all of these problems at once, I would greatly appreciate it.

This happens, because text = paste0('VAR.B:', VAR.B))) creates a factor, that is ordered alphabetically.

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

Hope this helps :-) I edit the t, so it's more obvious which call produces what

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM