尝试制作带有多条线的折线图,但得到了一个空的图表。

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

Trying to make a line plot with multiple lines, but get an empty plot

问题

这是您的翻译内容:

所以,我想在一个图表上绘制这位棒球球员从2017年到2022年的打击率、出垒率、长打率和出垒加长打率。数据框包括年份,然后是相同顺序的上述统计数据。我用于折线图的代码如下:

    library(ggplot2)
    
    ggplot(slashline, aes(x = Year)) +
      geom_line(aes(y = BA), color = "BA") +
      geom_line(aes(y = OBP), color = "OBP") +
      geom_line(aes(y = SLG), color = "SLG") +
      geom_line(aes(y = OPS), color = "OPS")

从我看到的情况来看,这应该可以工作,但实际上只在Y轴上绘制BA、OBP、SLG和OPS,没有绘制任何数字或实际数据,因此线条显然也不会显示出来。如果有任何帮助将不胜感激。

编辑:因此,感谢Moliz的答案,我能够解决大部分问题。到目前为止,这是我的代码:

    happData <- read_csv("我的路径文件")

    slashline <- happData %>%
    select(Year, "BA", "OBP", "SLG", "OPS")

    slashline <- slashline[-c(7, 8),]

    slashline2 <- slashline %>%
    gather(key="type",value="value",BA,OBP,SLG,OPS)
    slashline2

    # 这是图形的代码
    ggplot(slashline2,aes(x=Year,y= value,color=type))+
    geom_point()+
    geom_line(alpha = .5)+
    labs(y = "",
       title = "Ian Happ的统计数据,2017-2022",
       key = "")+
    theme(plot.title = element_text(hjust = 0.5))+
    guides(color = guide_legend(title = ""))+
    scale_color_discrete(labels = c("打击率", "出垒率", 
    "长打率", "出垒加长打率"))

这是这个图形的输出:

[Ian Happ Statistics][1]

  [1]: https://i.stack.imgur.com/XVHZb.png

显然,问题在于没有线条连接这些点,尽管理论上应该有。我不太确定为什么会出现这种情况。当我插入group = 1时,图形会变得混乱,线条到处都是。如果有任何帮助将不胜感激。
英文:

So, I'm wanting to graph this baseball player's batting average, on base percentage, slugging percentage, and on base plus slugging on one graph across the years 2017-2022. The dataframe has the years, then the aforementioned statistics in that same order. My code for the line plot is this:

library(ggplot2)

ggplot(slashline, aes(x = Year)) +
  geom_line(aes(y = BA), color = &quot;BA&quot;) +
  geom_line(aes(y = OBP), color = &quot;OBP&quot;) +
  geom_line(aes(y = SLG), color = &quot;SLG&quot;) +
  geom_line(aes(y = OPS), color = &quot;OPS&quot;)

From what I've seen, this should work, but instead it just plots BA, OBP, SLG, and OPS on the Y axis and doesn't plot any numbers or actual data, and so the lines obviously don't show up either. Any help would be appreciated.

Edit: So thanks to the answer from Moliz, I was able to get most of the problem solved. Here is my code thus far:

happData &lt;- read_csv(&quot;my pathfile&quot;)

slashline &lt;- happData %&gt;% 
select(Year, &quot;BA&quot;, &quot;OBP&quot;, &quot;SLG&quot;, &quot;OPS&quot;)

slashline &lt;- slashline[-c(7, 8),]

slashline2 &lt;- slashline %&gt;% 
gather(key=&quot;type&quot;,value=&quot;value&quot;,BA,OBP,SLG,OPS)
slashline2

# Here&#39;s the code for the graph
ggplot(slashline2,aes(x=Year,y= value,color=type))+
geom_point()+
geom_line(alpha = .5)+
labs(y = &quot;&quot;,
   title = &quot;Ian Happ&#39;s Statistics, 2017-2022&quot;,
   key = &quot;&quot;)+
theme(plot.title = element_text(hjust = 0.5))+
guides(color = guide_legend(title = &quot;&quot;))+
scale_color_discrete(labels = c(&quot;Batting Average&quot;, &quot;On-Base Percentage&quot;, 
&quot;Slugging Percentage&quot;, &quot;On-Base Plus Slugging&quot;))

Here's the output for this graph:

Ian Happ Statistics

Obviously, the problem is that there's no lines to connect the dots even though there should be. I'm not really sure why this is. When I plug in group = 1, it makes the graph messy with lines going all over the place. Any help would be appreciated.

答案1

得分: 2

根据您的描述,我创建了一个随机数据集。

`runif(6,0,1)` 从均匀分布中生成了6个介于0到1之间的随机数。

我认为您的数据集看起来是这样的:

| 年份 |    BA     |    OBP     |    SLG     |    OPS     |
|-----|-----------|------------|------------|------------|
| 2017 | 0.1915609 | 0.03188816 | 0.57335645 | 0.64135658 |
| 2018 | 0.5832220 | 0.11446759 | 0.44750805 | 0.52573932 |
| 2019 | 0.4594732 | 0.46893548 | 0.08380201 | 0.03928139 |
| 2020 | 0.4674340 | 0.39698674 | 0.21913855 | 0.54585984 |
| 2021 | 0.3998326 | 0.83361919 | 0.07557029 | 0.37276310 |
| 2022 | 0.5052856 | 0.76112174 | 0.53442678 | 0.96130241 |

不过现在我们需要做一点修改。在这里,您需要使用 `tidyr` 库并使用 `gather()` 函数:

```R
df2 <- df %>% gather(key="type", value="value", BA, OBP, SLG, OPS)
df2

最后使用 ggplot2

ggplot(df2, aes(x=year, y=value, color=type)) + geom_point() + geom_line(alpha=0.5)

**注意:** 由于我没有设置随机数种子并重新运行代码,所以此处的 `df2` 与上述描述的不同。您可以在您的计算机上运行上面的代码,验证输出的图像是否符合您的要求。 ❤️

<details>
<summary>英文:</summary>

Following your description I created a random dataset.

year <- 2017:2022
BA <- runif(6,0,1)
OBP <- runif(6,0,1)
SLG <- runif(6,0,1)
OPS <- runif(6,0,1)
df <- data.frame(year,BA,OBP,SLG,OPS)
df

`runif(6,0,1)` draws 6 random numbers from 0 to 1 according to a uniform distribution.

I think this is what your dataset looks like.

year BA OBP SLG OPS
1 2017 0.1915609 0.03188816 0.57335645 0.64135658
2 2018 0.5832220 0.11446759 0.44750805 0.52573932
3 2019 0.4594732 0.46893548 0.08380201 0.03928139
4 2020 0.4674340 0.39698674 0.21913855 0.54585984
5 2021 0.3998326 0.83361919 0.07557029 0.37276310
6 2022 0.5052856 0.76112174 0.53442678 0.96130241

But now we need to make a little change. Here you need to library `tidyr` package and use `gather()` function:
[How to use gather function][1]

df2 <- df %>% gather(key="type",value="value",BA,OBP,SLG,OPS)
df2

year type value
1 2017 BA 0.19156087
2 2018 BA 0.58322197
3 2019 BA 0.45947319
4 2020 BA 0.46743405
5 2021 BA 0.39983256
6 2022 BA 0.50528560
7 2017 OBP 0.03188816
8 2018 OBP 0.11446759
9 2019 OBP 0.46893548
10 2020 OBP 0.39698674
11 2021 OBP 0.83361919
12 2022 OBP 0.76112174
13 2017 SLG 0.57335645
14 2018 SLG 0.44750805
15 2019 SLG 0.08380201
16 2020 SLG 0.21913855
17 2021 SLG 0.07557029
18 2022 SLG 0.53442678
19 2017 OPS 0.64135658
20 2018 OPS 0.52573932
21 2019 OPS 0.03928139
22 2020 OPS 0.54585984
23 2021 OPS 0.37276310
24 2022 OPS 0.96130241


Finally use `ggplot2`:

ggplot(df2,aes(x=year,y=value,color=type))+geom_point()+geom_line(alpha=0.5)

[![enter image description here][2]][2]

(Because I did not set the random number seed and re-run the code, so here `df2` is not the same as described above)

You can run the above code on your computer and verify that the output image meets your requirements. &lt;3


  [1]: https://tidyr.tidyverse.org/reference/gather.html
  [2]: https://i.stack.imgur.com/BIlyG.png

</details>



# 答案2
**得分**: 1

"BA", "OBP" ...不是颜色代码,应该放在`aes()`内

```R
library(ggplot2)
ggplot(slashline, aes(x = Year)) +
  geom_line(aes(y = BA, color = "BA")) +
  geom_line(aes(y = OBP, color = "OBP")) +
  geom_line(aes(y = SLG, color = "SLG")) +
  geom_line(aes(y = OPS, color = "OPS"))

没有您的示例数据,我无法在我的端上测试它,但我相当确定这应该可以正常工作,除非数据存在问题。

英文:

"BA", "OBP"... are not color code, should be put inside aes()

library(ggplot2)
ggplot(slashline, aes(x = Year)) +
  geom_line(aes(y = BA, color = &quot;BA&quot;)) +
  geom_line(aes(y = OBP, color = &quot;OBP&quot;)) +
  geom_line(aes(y = SLG, color = &quot;SLG&quot;)) +
  geom_line(aes(y = OPS, color = &quot;OPS&quot;))

Without your example data, I cannot test it on my side but I am pretty sure this should work unless there are issues with data.

huangapple
  • 本文由 发表于 2023年2月18日 09:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490649.html
匿名

发表评论

匿名网友

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

确定