【发布时间】:2019-03-16 02:47:35
【问题描述】:
我有一个由 4 个图表组成的循环,其中包含一个字符列表,例如“a、b、c、d”,所以在每个图表的标题中我想要“a”、“b”、“c”或“d”。但是,当我运行我的代码时,'a' 出现在所有标题中。
这是我正在使用的数据的 dput。
structure(list(Point = c(5, 6, 7, 8), La = c(535, 565, 532, 587
), Ce = c(45, 46, 58, 43), Pr = c(56, 54, 43, 50), Nd = c(23,
28, 18, 26)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -4L), spec = structure(list(cols = list(
Point = structure(list(), class = c("collector_double", "collector"
)), La = structure(list(), class = c("collector_double",
"collector")), Ce = structure(list(), class = c("collector_double",
"collector")), Pr = structure(list(), class = c("collector_double",
"collector")), Nd = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
以及到目前为止我提出的代码。只有 cols 不会在标题中循环。其余代码完美运行。我还是个初学者,所以感谢您的时间和耐心。
acq <- select(X1, La:Nd)
##loop##
gg <- for (ii in acq){
cols <- names(X1)[2:5]
m <-mean(ii)
sds <- sd(ii)
m1 <- mean(ii)+1
m2 <-mean(ii)-1
##plot##
g <- ggplot(X1,aes_string(x="Point",y="ii")) +
ggtitle(paste(cols,"\n",m,"\n",sds,"\n")) +
theme(plot.title = element_text(hjust = 0.5)) +
geom_line() + geom_hline(aes(yintercept=mean(ii))) + ylab('') + xlab('')+
geom_hline(aes(yintercept=m1),linetype=2) +
geom_text(x=8,y=m1,label="10%",vjust=-1) +
geom_hline(aes(yintercept=m2),linetype=2) +
geom_text(x=8,y=m2,label="10%",vjust=-1)
print(g)
}
我的数据:
~Point, ~La, ~Ce, ~Pr, ~Nd,
5, 535, 45, 56, 23,
6, 565, 46, 54, 28,
7, 532, 58, 43, 18,
8, 587, 43, 50, 26
【问题讨论】:
-
对于reproducibility,请使用
dput(df)的输出编辑您的问题。 -
@Tung 很抱歉,这段代码太复杂了,我无法理解。
-
@Yria:您能否通过分享您的数据样本来让您的问题可重现,以便其他人可以提供帮助(请不要使用
str()、head()或屏幕截图)?你可以使用reprex和datapasta包来帮助你。另见Help me Help you & How to make a great R reproducible example? -
@Tung 谢谢你的建议。我添加了 dput、实际代码和用于构建代码的实际数据。