英文:
How to create a multiline plot
问题
我有以下问题。我想在R中创建一幅线图,该图根据变量'Nr'进行区分,同时也根据每个单独的课程区分模式1(实线)和模式2(虚线)。图应如下所示:
我的代码是:
sc = c(0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4)
pat = c("Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1",
"Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1",
"Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1",
"Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2",
"Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2",
"Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2")
nr= c(3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5)
changes = c(10,8,7,5,4,7,3,1,0,0,4,0,0,0,0,11,8,4,2,1,6,2,0,0,0,2,0,0,0,0)
df <- data.frame(sc = sc, pat = pat, nr = nr, changes = changes)
如何让它工作?我猜想可以使用ggplot,但我不知道该如何操作。
英文:
I have the following question. I would like to create a line plot in R that differentiates by the variable 'Nr' but also differentiates between pattern 1 (solid line) and pattern 2 (dashed line) for each of the individual courses. The plot should look like this:
Picture
My code is:
sc = c(0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4)
pat = c("Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1",
"Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1",
"Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1",
"Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2",
"Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2",
"Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2")
nr= c(3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5)
changes = c(10,8,7,5,4,7,3,1,0,0,4,0,0,0,0,11,8,4,2,1,6,2,0,0,0,2,0,0,0,0)
df <- data.frame(sc = sc, pat = pat, nr = nr, changes = changes)
How do I get this to work? My guess would be ggplot but i dont know how.
答案1
得分: 2
你可以使用ggplot和geom_line,同时使用条件来设置颜色和线型。以下是答案:
sc = c(0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4)
pat = c("Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1",
"Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1",
"Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1",
"Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2",
"Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2",
"Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2")
nr= c(3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5)
changes = c(10,8,7,5,4,7,3,1,0,0,4,0,0,0,0,11,8,4,2,1,6,2,0,0,0,2,0,0,0,0)
df <- data.frame(sc = sc, pat = pat, nr = nr, changes = changes)
ggplot(data = df, aes(x = sc, y = changes, color = as.factor(nr))) +
geom_line(mapping = aes(linetype = pat)) +
labs(color = "nr", linetype = "pattern")
英文:
You can use ggplot with geom_line using the conditional for both the colour and linetype. Here is the answer
sc = c(0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4)
pat = c("Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1",
"Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1",
"Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1", "Pattern 1",
"Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2",
"Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2",
"Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2", "Pattern 2")
nr= c(3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5)
changes = c(10,8,7,5,4,7,3,1,0,0,4,0,0,0,0,11,8,4,2,1,6,2,0,0,0,2,0,0,0,0)
df <- data.frame(sc = sc, pat = pat, nr = nr, changes = changes)
ggplot(data = df, aes(x = sc, y = changes, color = as.factor(nr))) +
geom_line(mapping = aes(linetype = pat)) +
labs(color = "nr", linetype = "pattern")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论