问题:ggplot2线条图存在问题:出现倾斜的直线,而不是预期的线条图。

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

Issue with ggplot2 line plot: Tilted straight lines instead of expected line plot

问题

I'm trying to create a line plot using ggplot2 in R, but I'm facing an issue where the plot shows tilted straight lines instead of the expected line plot as shown in the figure below.

这里我尝试在R中使用ggplot2创建一条线图,但我遇到了一个问题,即图表显示的是倾斜的直线,而不是下面图中所示的期望线图。

Here's the code I'm using:

这是我正在使用的代码:

aws_plot <- df_aws_atm %>% ggplot(aes(x=Month, y=values, color=variables, 
                                      group=1)) +
  geom_line(linewidth=0.5) +
  scale_color_manual(values = c("P"="black","E"="blue","C"="green",
                                "WVSC"="red")) +
  # scale_y_continuous(limits = c(-50, 1500)) +
  scale_x_discrete(expand = c(0.02, 0)) +
  ggtitle("Atmospheric Water Balance") +
  xlab("Month") +
  ylab("mm") +
  theme_bw()

And here's the data I'm using:

这是我正在使用的数据:

> dput(df_aws_atm)
structure(list(Month = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 
10L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L), levels = c("Jan", 
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
"Nov", "Dec"), class = "factor"), variables = structure(c(1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), levels = c("P", 
"E", "C", "WVSC"), class = "factor"), values = c(61.6012042725641, 
19.4143472506923, 41.107596204, -1.07926081812821, 61.0211111110256, 
-5.24147855594872, 66.7437418039231, 0.48115213725641, 80.6383290582051, 
7.16041454266667, 76.3176395729744, 2.83972504705128, 100.314478631795, 
69.3612280245385, 34.7814502373077, 3.82819962705128, 112.722914529231, 
92.58926418, 27.2810184358718, 7.14736808612821, 105.177636751538, 
110.168340093846, 2.30605669376923, 7.2967600354359, 99.9483119687179, 
126.626434667949, -22.3589573936923, 4.31916530276923, 87.8823653871795, 
112.96183799, -26.497900053, -1.41842745128205, 80.9593547017949, 
79.0862466384615, -5.19606537007692, -7.06917343351282, 84.362542735641, 
48.421970115641, 28.3443924478205, -7.5961801698718, 82.4711452974359, 
13.4414487457436, 63.4596880341795, -5.57000852123077, 70.4012179492308, 
8.49156181276923, 58.8562424521282, -3.05341368164103)), row.names = c(NA, 
-48L), class = c("tbl_df", "tbl", "data.frame"))
> 
英文:

I'm trying to create a line plot using ggplot2 in R, but I'm facing an issue where the plot shows tilted straight lines instead of the expected line plot as shown in the figure below.

问题:ggplot2线条图存在问题:出现倾斜的直线,而不是预期的线条图。

Here's the code I'm using:

aws_plot &lt;- df_aws_atm %&gt;% ggplot(aes(x=Month, y=values, color=variables, 
group=1)) +
geom_line(linewidth=0.5) +
scale_color_manual(values = c(&quot;P&quot;=&quot;black&quot;,&quot;E&quot;=&quot;blue&quot;,&quot;C&quot;=&quot;green&quot;,
&quot;WVSC&quot;=&quot;red&quot;)) +
# scale_y_continuous(limits = c(-50, 1500)) +
scale_x_discrete(expand = c(0.02, 0)) +
ggtitle(&quot;Atmospheric Water Balance&quot;) +
xlab(&quot;Month&quot;) +
ylab(&quot;mm&quot;) +
theme_bw()

And here's the data I'm using:

&gt; dput(df_aws_atm)
structure(list(Month = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 
10L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L), levels = c(&quot;Jan&quot;, 
&quot;Feb&quot;, &quot;Mar&quot;, &quot;Apr&quot;, &quot;May&quot;, &quot;Jun&quot;, &quot;Jul&quot;, &quot;Aug&quot;, &quot;Sep&quot;, &quot;Oct&quot;, 
&quot;Nov&quot;, &quot;Dec&quot;), class = &quot;factor&quot;), variables = structure(c(1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), levels = c(&quot;P&quot;, 
&quot;E&quot;, &quot;C&quot;, &quot;WVSC&quot;), class = &quot;factor&quot;), values = c(61.6012042725641, 
19.4143472506923, 41.107596204, -1.07926081812821, 61.0211111110256, 
-5.24147855594872, 66.7437418039231, 0.48115213725641, 80.6383290582051, 
7.16041454266667, 76.3176395729744, 2.83972504705128, 100.314478631795, 
69.3612280245385, 34.7814502373077, 3.82819962705128, 112.722914529231, 
92.58926418, 27.2810184358718, 7.14736808612821, 105.177636751538, 
110.168340093846, 2.30605669376923, 7.2967600354359, 99.9483119687179, 
126.626434667949, -22.3589573936923, 4.31916530276923, 87.8823653871795, 
112.96183799, -26.497900053, -1.41842745128205, 80.9593547017949, 
79.0862466384615, -5.19606537007692, -7.06917343351282, 84.362542735641, 
48.421970115641, 28.3443924478205, -7.5961801698718, 82.4711452974359, 
13.4414487457436, 63.4596880341795, -5.57000852123077, 70.4012179492308, 
8.49156181276923, 58.8562424521282, -3.05341368164103)), row.names = c(NA, 
-48L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
&gt; 

答案1

得分: 0

library(ggplot2)

df_aws_atm  |&gt; 
  ggplot(aes(x=Month, y=values, colour=variables, group = variables)) +
  geom_line(linewidth=0.5) +
  scale_color_manual(values = c(&quot;P&quot;=&quot;黑色&quot;,&quot;E&quot;=&quot;蓝色&quot;,&quot;C&quot;=&quot;绿色&quot;,
                                &quot;WVSC&quot;=&quot;红色&quot;)) +
  # scale_y_continuous(limits = c(-50, 1500)) +
  scale_x_discrete(expand = c(0.02, 0)) +
  ggtitle(&quot;大气水平衡&quot;) +
  xlab(&quot;月份&quot;) +
  ylab(&quot;毫米&quot;) +
  theme_bw()

问题:ggplot2线条图存在问题:出现倾斜的直线,而不是预期的线条图。<!-- -->

<sup>2023-07-03 创建,使用 reprex v2.0.2</sup>

英文:
library(ggplot2)

df_aws_atm  |&gt; 
  ggplot(aes(x=Month, y=values, colour=variables, group = variables)) +
  geom_line(linewidth=0.5) +
  scale_color_manual(values = c(&quot;P&quot;=&quot;black&quot;,&quot;E&quot;=&quot;blue&quot;,&quot;C&quot;=&quot;green&quot;,
                                &quot;WVSC&quot;=&quot;red&quot;)) +
  # scale_y_continuous(limits = c(-50, 1500)) +
  scale_x_discrete(expand = c(0.02, 0)) +
  ggtitle(&quot;Atmospheric Water Balance&quot;) +
  xlab(&quot;Month&quot;) +
  ylab(&quot;mm&quot;) +
  theme_bw()

问题:ggplot2线条图存在问题:出现倾斜的直线,而不是预期的线条图。<!-- -->

<sup>Created on 2023-07-03 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年7月3日 15:56:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602826.html
匿名

发表评论

匿名网友

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

确定