0

I have looked at possible previous questions about this, and I have found this one: Error in FUN(X[[i]], ...) : object not found when adding geom_text with facet, and unfortunately, I must be too much of a beginner, since I could not solve the issue.

Therefore, I ask here, hoping that somebody will tell me how to do it...

I use the diamonds dataset from R to get a density plot, either with fill=clarity or fill=cut. It works fine.

When I try to add text, I get errors, different errors depending on whether it is "cut" or "clarity" I use on the fill, by the way.

I read in the mentioned answer that I have to merge the datasets.

How do I do that?

The diamonds dataset has a great many rows, and the text dataset only a few.

Here is the code:

if (!require("tidyverse")) install.packages("tidyverse", dependencies = TRUE)
library("tidyverse") 

diams2 <-diamonds

annot2 <- data.frame(
  text = c("IF", "WS1", "WS2", "VS1", "VS2", "SI1", "SI2", "I1"),
  xx =    c(  10,    20,    30,    40,    50,    60,    70,   80),
  yy =    c(   5,    10,    15,    20,    25,    30,    35,   40)
)

# THIS RUNS FINE:
diams2 %>% 
  ggplot(aes(x=price, fill=cut))+
  geom_density(adjust=1.5, position = "stack")+
  theme_void()

# THIS RUNS FINE:
diams2 %>% 
  ggplot(aes(x=price, fill=clarity))+
  geom_density(adjust=1.5, position = "stack")+
  theme_void()

# THIS WON'T RUN, AND GIVES THE FOLLOWING ERROR MESSAGE:
# Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
# Error in `f()`:
# ! Aesthetics must be valid data columns. Problematic aesthetic(s): fill = cut. 
# Did you mistype the name of a data column or forget to add after_stat()?
diams2 %>% 
  ggplot(aes(x=price, fill=cut))+
  geom_text(data=annot2,
            aes(x=x, y=y, label=text,
                color=text), hjust=0, size=4)+
  geom_density(adjust=1.5, position = "stack")+
  theme_void()

# THIS WON'T RUN, AND GIVES THE FOLLOWING ERROR MESSAGE:
# Error in FUN(X[[i]], ...) : object 'clarity' not found
diams2 %>% 
  ggplot(aes(x=price, fill=clarity))+
  geom_text(data=annot2,
            aes(x=x, y=y, label=text,
                color=text), hjust=0, size=4)+
  geom_density(adjust=1.5, position = "stack")+
  theme_void()

Thank you!

Jean-Paul Bardou
  • 107
  • 2
  • 11
  • 2
    Are you sure that's the error you are getting? If I copy/paste your code, I see a different error about `x` not found. Which makes sense because you are using `x` and `y` in the `aes()` for your `geom_text` but there's no `x` or `y` columns defined in the `annot2` dataframe. So you maybe you meant `xx` and `yy`. Those need to match exactly. Also you only want the `fill=` to work on the density, not the text. So move the `aes(fill=)` out of the of the global `ggplot()` call and move it to the `geom_density()` layer. – MrFlick Aug 08 '22 at 14:47
  • My bad, the code I uploaded was not the final version: replace "x=x" and "y=y" with respectively "x=xx" and "y=yy" in the call to geom_text... I had noticed that the diamonds data frame had an x and an y column... – Jean-Paul Bardou Aug 08 '22 at 21:44
  • I had missed the part about moving the aes(fill=) out of the global ggplot() call. I moved it to geom_density() as suggested, and now, the plot shows only the text, not the density curves. The new code is: `diams2 %>% ggplot()+ geom_density(adjust=1.5, position = "stack", aes(x=price, fill=clarity))+ geom_text(data=annot2, aes(x=xx, y=yy, label=text, color=text), hjust=0, size=4)+ theme_void()` (Sorry, I could not get it to look like code) – Jean-Paul Bardou Aug 09 '22 at 11:19
  • Then the problem is the values you are using for x and y for the labels are on a wildly different scale than the density curve. The price of diamonds go from 0-20,000 and the y values from 0-0.001. Basically the curves are just getting squished along the bottom. – MrFlick Aug 09 '22 at 13:14
  • @MrFlick, thanks a lot! It did the trick... I am learning so much! Would you look at my other question from a couple of days ago, the one about plotting maps! – Jean-Paul Bardou Aug 09 '22 at 13:59

0 Answers0