我正在尝试创建一个堆叠条形图来表示此数据。变量n_paper是使用该方法的论文总数,变量paper_ui是其中有多少论文使用了上涌索引。
n_papers paper_ui methods
1 6 3 AR
2 4 2 ARMA
3 5 4 ARIMA
4 1 0 SARIMA
5 6 1 Loess decomposition
6 2 1 Classical decomposition
7 1 0 Exponential smoothing
ggplot(df,aes(x = methods, y = n_papers)) +
geom_bar(stat="identity", color="dodgerblue4", fill="dodgerblue4")+
scale_x_discrete() + xlab("Time series methods") +
ylab("Nº of papers") +
theme(
axis.title.x = element_text(size=15),
axis.title.y = element_text(size=15),
axis.text = element_text(size=12),
axis.text.x = element_text(angle=45, hjust=1)
)该图如下所示,但我希望在该图中使用变量paper_ui。谢谢!
发布于 2021-01-20 08:37:16
尝尝这个。您必须将您的数据调整为long,然后绘制:
library(ggplot2)
library(dplyr)
library(tidyr)
#Code
df %>% pivot_longer(-methods) %>%
ggplot(aes(x = methods, y = value,color=name,fill=name)) +
geom_bar(stat="identity")+
scale_x_discrete() + xlab("Time series methods") +
ylab("Nº of papers") +
theme(
axis.title.x = element_text(size=15),
axis.title.y = element_text(size=15),
axis.text = element_text(size=12),
axis.text.x = element_text(angle=45, hjust=1)
)输出:

使用的一些数据:
#Data
df <- structure(list(n_papers = c(6L, 4L, 5L, 1L, 6L, 2L, 1L), paper_ui = c(3L,
2L, 4L, 0L, 1L, 1L, 0L), methods = c("AR", "ARMA", "ARIMA", "SARIMA",
"Loess decomposition", "Classical decomposition", "Exponential smoothing"
)), row.names = c(NA, -7L), class = "data.frame")https://stackoverflow.com/questions/65801512
复制相似问题