I’m trying to access the data passed to a ggplot
command in a scale_y_continuous
‘layer’
diamonds %>%
filter(cut %in% c("Very Good"), carat > 2.8) %>%
ggplot(aes(x=table, y=price, fill=color)) +
geom_bar(stat="identity", position="dodge") +
geom_text(aes(label=table, group=color),
vjust=-0.5,
position=position_dodge2(width = 1, preserve="single")) +
scale_y_continuous(expand = c(0, 0), limits = c(0, max(.$table) * 1.1))
However, it’s saying:
Error: object '.' not found
3
This doesn’t directly answer your question, but to access the data, I’d make a plotdata dataframe then call that dataframe for setting limits:
plotdata <- diamonds %>%
filter(cut %in% c("Very Good"), carat > 2.8)
ggplot(plotdata, aes(...
...
scale_y_continuous(expand = c(0, 0), limits = c(0, max(plotdata$price) * 1.1))
Or we can use expansion:
diamonds %>%
filter(cut %in% c("Very Good"), carat > 2.8) %>%
ggplot(aes(x=table, y=price, fill=color)) +
geom_bar(stat="identity", position="dodge") +
geom_text(aes(label=table, group=color),
vjust=-0.5,
position=position_dodge2(width = 1, preserve="single")) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1)))
Recognized by R Language Collective