dplyr + ggplot2。在同一管道内使用 scale_x_continuous() 内用 dplyr 计算的列
dplyr + ggplot2. Use column calculated with dplyr inside scale_x_continuous() within the same pipeline
有没有办法在同一管道中使用来自 ggplot2 的 scale_x_continuous() 中的 dplyr 计算的列?
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))
基本上,我计算的是不同作者(因子列)出现在数据框中的次数。但是,R 不允许我在 scale_x_continuous 中使用 n
(然后是 count() returns 的列的名称)。但它在 ggplot() 函数内。
有办法吗?还是我被迫做类似的事情:
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))
提前致谢!
可以使用大括号和点符号(相关信息在, and here中接受的答案的最后部分):
library(tidyverse)
library(viridis)
#> Loading required package: viridisLite
p2 <- iris %>%
sample_n(100) %>%
count(Species) %>%
{
ggplot(., aes(x = reorder(Species, n), y = n, fill = n)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_classic() +
scale_fill_viridis() +
scale_y_continuous(breaks = seq(0, max(.$n), by = 20)) +
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)
)
}
p2
由 reprex package (v0.3.0)
于 2019-11-24 创建
请注意,您没有提供任何可重现的示例,因此我以 iris
为起点,进行了一些行采样以获得物种计数的不同频率。如果您用 reproducible example 更新您的问题,我会更新我的答案。
有没有办法在同一管道中使用来自 ggplot2 的 scale_x_continuous() 中的 dplyr 计算的列?
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))
基本上,我计算的是不同作者(因子列)出现在数据框中的次数。但是,R 不允许我在 scale_x_continuous 中使用 n
(然后是 count() returns 的列的名称)。但它在 ggplot() 函数内。
有办法吗?还是我被迫做类似的事情:
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))
提前致谢!
可以使用大括号和点符号(相关信息在
library(tidyverse)
library(viridis)
#> Loading required package: viridisLite
p2 <- iris %>%
sample_n(100) %>%
count(Species) %>%
{
ggplot(., aes(x = reorder(Species, n), y = n, fill = n)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_classic() +
scale_fill_viridis() +
scale_y_continuous(breaks = seq(0, max(.$n), by = 20)) +
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)
)
}
p2
由 reprex package (v0.3.0)
于 2019-11-24 创建请注意,您没有提供任何可重现的示例,因此我以 iris
为起点,进行了一些行采样以获得物种计数的不同频率。如果您用 reproducible example 更新您的问题,我会更新我的答案。