Is there a way to use a column calculated with dplyr in scale_x_continuous() from ggplot2 in the same pipeline?
p2 <- chat %>%
count(author) %>%
ggplot(aes(x = reorder(author, n), y = n, fill = n)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_classic() +
scale_fill_viridis() +
scale_x_continuous(breaks = seq(0, **max(n)**, by = 250))
theme(
axis.title.x = element_blank(), axis.title.y = element_blank(),
legend.position = "none",
plot.title = element_text(size = 13, face = "bold", hjust = 0.5),
plot.subtitle = element_text(color = '#666664', size = 10, hjust = 0.5))
Basically, I'm counting the number of times a different author (factor column) appears in the dataframe. However, R is not letting me use n
(which is then name of the column that count() returns) in scale_x_continuous. But it does within the ggplot() function.
Is there a way to do so? Or am I forced to do something like:
data <- chat %>%
count(author)
p2 <- ggplot(data, aes(x = reorder(author, n), y = n, fill = n)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_classic() +
scale_fill_viridis() +
scale_x_continuous(breaks = seq(0, **max(data$n)**, by = 250))
theme(
axis.title.x = element_blank(), axis.title.y = element_blank(),
legend.position = "none",
plot.title = element_text(size = 13, face = "bold", hjust = 0.5),
plot.subtitle = element_text(color = '#666664', size = 10, hjust = 0.5))
Thanks in advance!