英文:
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.
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"))
>
答案1
得分: 0
library(ggplot2)
df_aws_atm |>
ggplot(aes(x=Month, y=values, colour=variables, group = variables)) +
geom_line(linewidth=0.5) +
scale_color_manual(values = c("P"="黑色","E"="蓝色","C"="绿色",
"WVSC"="红色")) +
# scale_y_continuous(limits = c(-50, 1500)) +
scale_x_discrete(expand = c(0.02, 0)) +
ggtitle("大气水平衡") +
xlab("月份") +
ylab("毫米") +
theme_bw()
<!-- -->
<sup>2023-07-03 创建,使用 reprex v2.0.2</sup>
英文:
library(ggplot2)
df_aws_atm |>
ggplot(aes(x=Month, y=values, colour=variables, group = variables)) +
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()
<!-- -->
<sup>Created on 2023-07-03 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论