0

I get this error after I filter a simple datafarme, from 12 obs to 9, both with 2 variable...

tmp_Type <- c("A", "B", "C","D", "E", "F", "G", "H", "I", "J", "K", "L")
tmp_Sum <- c(380000.2,0,1500,4532,2,34567,29344,545,838.5,1000,0,0)
tmp_Sum <- round(tmp_Sum)

sum(tmp_Sum, na.rm=T)

tmp_Summary <- data.frame(tmp_Type, tmp_Sum) # create df

summary(tmp_Summary)

ggplot(data=tmp_Summary, aes(x=tmp_Type, y=tmp_Sum)) +
  geom_histogram (stat = "identity", aes(fill= tmp_Type)) +
  geom_text (label = (tmp_Sum), vjust=-1, hjust=0.5)

tmp_Summary <- tmp_Summary %>% filter(tmp_Sum > 0)

summary(tmp_Summary)

ggplot(data=tmp_Summary, aes(x=tmp_Type, y=tmp_Sum)) +
  geom_histogram (stat = "identity", aes(fill= tmp_Type)) +
  geom_text (label = (tmp_Sum), vjust=-1, hjust=0.5)
  • 1
    Put `label` in `aes` i.e `geom_text (aes(label = tmp_Sum), vjust=-1, hjust=0.5)` – Ronak Shah Feb 10 '21 at 04:58
  • Thanks Ronak. That worked. But why does the second plot call throw the error and the first Not? Any ideas?. –  Feb 10 '21 at 05:09

1 Answers1

0

The reason why the first plot worked is because you have tmp_Sum vector in the global environment. If you remove them after creating the dataframe the first plot would give you error as well.

tmp_Type <- c("A", "B", "C","D", "E", "F", "G", "H", "I", "J", "K", "L")
tmp_Sum <- c(380000.2,0,1500,4532,2,34567,29344,545,838.5,1000,0,0)
tmp_Sum <- round(tmp_Sum)
tmp_Summary <- data.frame(tmp_Type, tmp_Sum) 
rm(tmp_Type, tmp_Sum) #removing variables

Now plot.

library(ggplot2)
ggplot(data=tmp_Summary, aes(x=tmp_Type, y=tmp_Sum)) +
  geom_histogram (stat = "identity", aes(fill= tmp_Type)) +
  geom_text (label = (tmp_Sum), vjust=-1, hjust=0.5)

Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomText, : object 'tmp_Sum' not found

Always include label inside aes.

library(ggplot2)
ggplot(data=tmp_Summary, aes(x=tmp_Type, y=tmp_Sum)) +
  geom_histogram (stat = "identity", aes(fill= tmp_Type)) +
  geom_text (aes(label = tmp_Sum), vjust=-1, hjust=0.5)

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213