まず、ほぼ同じタイトルの別の質問がすでに存在します。Rラティスパッケージを使用して線のリストをプロットします。ただし、その意図は異なります。つまり、列ごとに個別のプロットが必要です。私の必要性は、各線を重ね合わせた一連のプロットを含む単一のプロットです。そのために機能するハードコードされた列名のバージョンは次のとおりです。
library(lattice)
library(tibble)
cols = c('confirmed','recovered','exposed')
df = tibble( exposed= c(50,80,90), confirmed= c(10,20,30), recovered= c(3,5,7))
City1=df
Day = c(1:length(df))
Exposed=df$exposed
Confirmed=df$confirmed
Recovered=df$recovered
xyplot(Exposed + Confirmed + Recovered~ Day, main='City1 Stats',xlab='Day',ylab='Cases',
cex.lab=0.6, xaxt="n", type = "l", auto.key = list(points = FALSE,lines = TRUE,
par.settings = list(superpose.line = list(col = c("green","red","orange")))))
ハードコードされているのではなく、列名のベクトルを送信したいのですが、どうすればよいですか?その形式は次のようになります。
plotVars = c(Exposed, Confirmed, Recovered)
xyplot( plotVars ~ Day, main='City1 Stats',xlab='Day',ylab='Cases',
cex.lab=0.6, xaxt="n", type = "l",
auto.key = list(points = FALSE,lines = TRUE,
par.settings = list(superpose.line = list(col = c("green","red","orange")))))
のplotVars
わかりやすいリストを作成するにはどうすればよいxyplot
ですか?
更新以下の回答から、区切り文字としてpaste
列名を設定するために使用することをお勧めします+
。そのアプローチを使用して更新されたコードは次のとおりです。
library(lattice)
cols = c('confirmed','recovered','exposed')
df = tibble( exposed= c(50,80,90), confirmed= c(10,20,30), recovered= c(3,5,7))
City1=df
Day = c(1:length(df))
exposed=df$exposed
confirmed=df$confirmed
recovered=df$recovered
fml = formula(paste(paste0(cols, collapse = " + "), "Day", sep = " ~ "))
xyplot(fml, main='City1 Stats',xlab='Day',ylab='Cases', cex.lab=0.6,
xaxt="n", type = "l", auto.key = list(points = FALSE,lines = TRUE,
par.settings = list(superpose.line = list(col = c("green","red","orange")))))
これが前のコメントの意味です。プロットの式を指定するためのより良い方法があるかもしれませんが、現時点ではこれ以上良い方法は考えられません。
# packages
library(lattice)
library(tibble)
# data
df = tibble(
exposed = c(50, 80, 90),
confirmed = c(10, 20, 30),
recovered= c(3, 5, 7)
)
Day = seq_len(nrow(df))
# plot
plotVars = c("exposed", "confirmed", "recovered")
xyplot(
formula(paste(paste0(plotVars, collapse = " + "), "Day", sep = " ~ ")),
data = df,
main = 'City1 Stats',
xlab = 'Day',
ylab = 'Cases',
cex.lab = 0.6,
xaxt = "n",
type = "l",
auto.key = list(
points = FALSE,
lines = TRUE,
par.settings = list(
superpose.line = list(col = c("green","red","orange"))
)
)
)
reprexパッケージ(v0.3.0)によって2020-03-05に作成されました
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加