如何创建一个多行图表

huangapple go评论65阅读模式
英文:

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(&quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, 
       &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, 
       &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, 
       &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;,
       &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;,
       &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;)
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 &lt;- 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(&quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, 
        &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, 
        &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, &quot;Pattern 1&quot;, 
        &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;,
        &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;,
        &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;, &quot;Pattern 2&quot;)
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 &lt;- 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 = &quot;nr&quot;, linetype = &quot;pattern&quot;)

如何创建一个多行图表

huangapple
  • 本文由 发表于 2023年6月22日 16:35:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定