trans="log10" and labels = scales::dollar problem

I have a peculiar problem involving the use of the trans and label commands in scale_y_continuous within ggplot2.

With toy data, things seem to work correctly, combining the scale_y_continus trans function with labels = scales::dollar. So this code

y <- c(0,2,4,6,3,2,6,1000)
x <- c(1,2,3,4,5,6,7,8)
z <- c("One", "One", "One", "One", "two", "two", "two", "two")
df <- data.frame(x,y, z)


df%>%
  ggplot()+
  geom_col(aes(x=x, y=y, fill=z))+
  scale_y_continuous(trans="log10", labels=scales::dollar)

The result is this: which is right.

image

However, when I try this with my data (cannot be shared, unfortunately) the scale for the y axis goes ballistic (I ended with like trillions on Y - 1e52), when the real scale is up to a max in the billions. When I plot without the transformation (that is, no trans="log10", things work fine; the dollar label is applied, and the data is correct.

Here is an example of the code I am running that produces the issue (data cannot be shared). And again, dropping the transformation (trans="log10"), everything works fine so it is not a data issue, per se. Also note the data does not include NAs.

test_df%>%
  ggplot()+
  geom_col(aes(x=name, y= dollar_value, fill=factor_variable))+
  labs(title = "main title",
       x = "",
       y= "Dollars (log10)",
       fill = "Factor Variable Name")+
  scale_y_continuous(trans="log10", labels=scales::dollar)+
  theme_classic2()

Any ideas what is happening here?

It is hard to debug this without the data. Can you change your data frame to remove the confidential information? You can replace the columns of x and fill values with values from the built in LETTERS vector and you can multiply the dollar_value column by a random number. Check that the modified data still show the problem and then post them here.

This tells me you are making an assumption about your data, rather than reporting a verified fact about your data.
I recommend you simply run max() on the field in question, and see what the largest value in fact is.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.