问题描述
我正在尝试创建一个堆叠条形图来表示这些数据。变量 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。 谢谢!
解决方法
试试这个。您必须将数据重塑为长形,然后绘制:
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,0L,0L),methods = c("AR","ARMA","ARIMA","SARIMA","Loess decomposition","Classical decomposition","Exponential smoothing"
)),row.names = c(NA,-7L),class = "data.frame")