英文:
20 panel graphs from ggplot have shifted values
问题
我正在尝试创建一个包含20个面板的图表,以便能够分别绘制20个y值和1个x值。x轴是7月到10月的日期,y轴值是1996年到2015年每年的温度。以下是您提供的R代码和相关的问题描述,我将只翻译代码部分:
rm(list = ls())
library(data.table)
library(ggplot2)
set.seed(20)
atlanta_temp_data = read.table("temps.txt", header = TRUE)
atlanta_temp_data$DAY = as.Date(atlanta_temp_data$DAY, format = "%d-%b")
class(atlanta_temp_data$DAY)
atlanta_temp_data$Year = rep(1996:2015, each = 12)[1:nrow(atlanta_temp_data)]
ggplot(atlanta_temp_data, aes(DAY, group = Year, color = Year)) +
geom_line(aes(y = X1996, color = "X1996")) +
geom_line(aes(y = X1997, color = "X1997")) +
geom_line(aes(y = X1998, color = "X1998")) +
geom_line(aes(y = X1999, color = "X1999")) +
geom_line(aes(y = X2000, color = "X2000")) +
geom_line(aes(y = X2001, color = "X2001")) +
geom_line(aes(y = X2002, color = "X2002")) +
geom_line(aes(y = X2003, color = "X2003")) +
geom_line(aes(y = X2004, color = "X2004")) +
geom_line(aes(y = X2005, color = "X2005")) +
geom_line(aes(y = X2006, color = "X2006")) +
geom_line(aes(y = X2007, color = "X2007")) +
geom_line(aes(y = X2008, color = "X2008")) +
geom_line(aes(y = X2009, color = "X2009")) +
geom_line(aes(y = X2010, color = "X2010")) +
geom_line(aes(y = X2011, color = "X2011")) +
geom_line(aes(y = X2012, color = "X2012")) +
geom_line(aes(y = X2013, color = "X2013")) +
geom_line(aes(y = X2014, color = "X2014")) +
geom_line(aes(y = X2015, color = "X2015")) +
geom_point(aes(y = X1996, color = "X1996")) +
geom_point(aes(y = X1997, color = "X1997")) +
geom_point(aes(y = X1998, color = "X1998")) +
geom_point(aes(y = X1999, color = "X1999")) +
geom_point(aes(y = X2000, color = "X2000")) +
geom_point(aes(y = X2001, color = "X2001")) +
geom_point(aes(y = X2002, color = "X2002")) +
geom_point(aes(y = X2003, color = "X2003")) +
geom_point(aes(y = X2004, color = "X2004")) +
geom_point(aes(y = X2005, color = "X2005")) +
geom_point(aes(y = X2006, color = "X2006")) +
geom_point(aes(y = X2007, color = "X2007")) +
geom_point(aes(y = X2008, color = "X2008")) +
geom_point(aes(y = X2009, color = "X2009")) +
geom_point(aes(y = X2010, color = "X2010")) +
geom_point(aes(y = X2011, color = "X2011")) +
geom_point(aes(y = X2012, color = "X2012")) +
geom_point(aes(y = X2013, color = "X2013")) +
geom_point(aes(y = X2014, color = "X2014")) +
geom_point(aes(y = X2015, color = "X2015")) +
scale_color_discrete(name = "Year") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
ggtitle("Atlanta temps for July to October (1996-2015)") +
labs(y = "Temperature (degF)") +
facet_wrap(~Year)
希望这有助于解决您的问题。如果还有其他问题,请随时提出。
英文:
I am trying to make a graph with 20 panels so as to be able to graph 20 y-values and 1 x-value individually. The x-axis are days of July to October and y-axis values are temperatures for each of the years 1996 to 2015.
The 20 panels that I am getting have shifted temp values for July-October months of each year (see attached and do not show all the 20 years but only for 10 years (1996-2006).
The output of dput(head(atlanta_temp_data))
is as following
structure(list(DAY = structure(c(19539, 19540, 19541, 19542, 19543, 19544), class = "Date"), X1996 = c(98L, 97L, 97L, 90L,
89L, 93L), X1997 = c(86L, 90L, 93L, 91L, 84L, 84L), X1998 = c(91L,
88L, 91L, 91L, 91L, 89L), X1999 = c(84L, 82L, 87L, 88L, 90L,
91L), X2000 = c(89L, 91L, 93L, 95L, 96L, 96L), X2001 = c(84L,
87L, 87L, 84L, 86L, 87L), X2002 = c(90L, 90L, 87L, 89L, 93L,
93L), X2003 = c(73L, 81L, 87L, 86L, 80L, 84L), X2004 = c(82L,
81L, 86L, 88L, 90L, 90L), X2005 = c(91L, 89L, 86L, 86L, 89L,
82L), X2006 = c(93L, 93L, 93L, 91L, 90L, 81L), X2007 = c(95L,
85L, 82L, 86L, 88L, 87L), X2008 = c(85L, 87L, 91L, 90L, 88L,
82L), X2009 = c(95L, 90L, 89L, 91L, 80L, 87L), X2010 = c(87L,
84L, 83L, 85L, 88L, 89L), X2011 = c(92L, 94L, 95L, 92L, 90L,
90L), X2012 = c(105L, 93L, 99L, 98L, 100L, 98L), X2013 = c(82L,
85L, 76L, 77L, 83L, 83L), X2014 = c(90L, 93L, 87L, 84L, 86L,
87L), X2015 = c(85L, 87L, 79L, 85L, 84L, 84L)), row.names = c(NA,
6L), class = "data.frame")
Please help to fix it.
Following is the R-code that I am using,
rm(list = ls())
library(data.table)
library(ggplot2)
set.seed(20)
atlanta_temp_data=read.table("temps.txt",header=TRUE)
atlanta_temp_data$DAY=as.Date(atlanta_temp_data$DAY,format="%d-%b")
class(atlanta_temp_data$DAY)
atlanta_temp_data$Year = rep(1996:2015, each=12)[1:nrow(atlanta_temp_data)]
ggplot(atlanta_temp_data, aes(DAY, group = Year, color = Year)) +
geom_line(aes(y = X1996, color = "X1996"))+
geom_line(aes(y = X1997, color = "X1997")) +
geom_line(aes(y = X1998, color = "X1998")) +
geom_line(aes(y = X1999, color = "X1999")) +
geom_line(aes(y = X2000, color = "X2000")) +
geom_line(aes(y = X2001, color = "X2001")) +
geom_line(aes(y = X2002, color = "X2002")) +
geom_line(aes(y = X2003, color = "X2003")) +
geom_line(aes(y = X2004, color = "X2004")) +
geom_line(aes(y = X2005, color = "X2005")) +
geom_line(aes(y = X2006, color = "X2006")) +
geom_line(aes(y = X2007, color = "X2007")) +
geom_line(aes(y = X2008, color = "X2008")) +
geom_line(aes(y = X2009, color = "X2009")) +
geom_line(aes(y = X2010, color = "X2010")) +
geom_line(aes(y = X2011, color = "X2011")) +
geom_line(aes(y = X2012, color = "X2012")) +
geom_line(aes(y = X2013, color = "X2013")) +
geom_line(aes(y = X2014, color = "X2014")) +
geom_line(aes(y = X2015, color = "X2015")) + geom_point(aes(y = X1996, color = "X1996"))+
geom_point(aes(y = X1997, color = "X1997")) +
geom_point(aes(y = X1998, color = "X1998")) +
geom_point(aes(y = X1999, color = "X1999")) +
geom_point(aes(y = X2000, color = "X2000")) +
geom_point(aes(y = X2001, color = "X2001")) +
geom_point(aes(y = X2002, color = "X2002")) +
geom_point(aes(y = X2003, color = "X2003")) +
geom_point(aes(y = X2004, color = "X2004")) +
geom_point(aes(y = X2005, color = "X2005")) +
geom_point(aes(y = X2006, color = "X2006")) +
geom_point(aes(y = X2007, color = "X2007")) +
geom_point(aes(y = X2008, color = "X2008")) +
geom_point(aes(y = X2009, color = "X2009")) +
geom_point(aes(y = X2010, color = "X2010")) +
geom_point(aes(y = X2011, color = "X2011")) +
geom_point(aes(y = X2012, color = "X2012")) +
geom_point(aes(y = X2013, color = "X2013")) +
geom_point(aes(y = X2014, color = "X2014")) +
geom_point(aes(y = X2015, color = "X2015")) +
scale_color_discrete(name = "Year") + theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))+
ggtitle("Atlanta temps for July to October (1996-2015)")+labs(y = "Temperature (degF)")+facet_wrap(~Year)
答案1
得分: 1
library(tidyverse)
[Your data] %>%
pivot_longer(-DAY) %>%
mutate(year = parse_number(name)) %>%
ggplot(aes(DAY, value, color = year)) +
geom_point() +
facet_wrap(~year)
<details>
<summary>英文:</summary>
library(tidyverse)
[Your data] %>%
pivot_longer(-DAY) %>%
mutate(year = parse_number(name)) %>%
ggplot(aes(DAY, value, color = year)) +
geom_point() +
facet_wrap(~year)
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/Q4HvT.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论