20 panel graphs from ggplot have shifted values.

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

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.20 panel graphs from ggplot have shifted values.

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).20 panel graphs from ggplot have shifted values.

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)

20 panel graphs from ggplot have shifted values.


<details>
<summary>英文:</summary>
library(tidyverse)
[Your data] %&gt;%
pivot_longer(-DAY) %&gt;%
mutate(year = parse_number(name)) %&gt;% 
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>

huangapple
  • 本文由 发表于 2023年2月6日 13:12:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357535.html
匿名

发表评论

匿名网友

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

确定