我有以下資料:年份和 12 個月的值。如何繪制相對于時間(以年表示)的值,并且兩年之間將有 12 個值?謝謝
1847 0.031 0.099 -1.585 1.170 1.763 -0.260 0.746 1.129 -0.324 0.445 2.459 1.760
1848 -0.792 1.770 0.757 -1.023 0.691 -1.780 1.867 2.641 -2.546 -2.436 -0.842 2.548
1849 2.419 2.767 -0.562 -0.990 -0.517 -3.210 1.203 0.701 -2.234 -0.078 0.802 -1.238
1850 -0.163 4.134 -2.216 0.965 -1.157 0.403 0.305 0.148 -2.077 -2.701 2.390 2.358
1851 3.293 1.028 1.504 -1.658 -1.534 -1.621 -5.395 4.679 1.852 0.777 -1.769 1.742
1852 1.464 0.411 -2.502 -1.597 0.245 0.093 -1.134 2.943 -2.021 -1.646 -0.930 1.029
像這樣的東西

uj5u.com熱心網友回復:
或者,如果您想將所有值放在一行中:
dat <- import("https://quantoid.net/files/test.txt")
names(dat) <- c("year", paste0("month_", 1:12))
dat <- dat %>%
pivot_longer(-year, names_pattern="month_(\\d )", names_to="month", values_to="val") %>%
mutate(month = as.numeric(month),
yrmo = year (month - 1)/12)
ggplot(dat, aes(x=yrmo, y=val))
geom_line()
scale_x_continuous(breaks=1847:1852)

uj5u.com熱心網友回復:
我有一個解決方案,使用zoo用于整齊繪制日期的highcharter包和用于生成圖表的包,以及tidyverse對資料進行排序。
library(zoo)
library(highcharter)
library(tidyverse)
我模擬了你的資料集如下:
df <- data.frame(year = c(rep(1847,12),rep(1848,12),rep(1849,12),rep(1850,12),rep(1851,12),rep(1852,12)),
month = c(rep(seq(01,12, by = 01),6)),
values = c(runif(72, min = -2, max = 2)
)) %>%
unite("date",c(year,month),sep = "-") %>%
mutate(date = as.yearmon(date)
) %>%
mutate(color = ifelse(values >= 0, "above", "below"))
這產生了這樣的東西:
head(df)
date values color
1 Jan 1847 0.7233567 above
2 Feb 1847 0.4621962 above
3 Mar 1847 -0.2388412 below
4 Apr 1847 -0.3818243 below
5 May 1847 -1.5017872 below
6 Jun 1847 -1.8706521 below
我創建了一個變數來指示值是高于還是低于 0。然后我使用 highcharter 的列繪圖函式進行繪圖:
colors <- c("slateblue","firebrick")
df %>%
hchart("column", hcaes(x = date, y = values, group = color)) %>%
hc_title(text = "My Chart Title",
align = "center",
style = list(
fontSize = '2em',
color = "#000000"
)) %>%
hc_legend(enabled = FALSE) %>%
hc_colors(colors)
其中產生:

uj5u.com熱心網友回復:
一個ggplot()版本facet_wrap()
樣本資料:
df<-read.table(header=T,text=
"Year January February March April May June July August October September November December
1847 0.031 0.099 -1.585 1.170 1.763 -0.260 0.746 1.129 -0.324 0.445 2.459 1.760
1848 -0.792 1.770 0.757 -1.023 0.691 -1.780 1.867 2.641 -2.546 -2.436 -0.842 2.548
1849 2.419 2.767 -0.562 -0.990 -0.517 -3.210 1.203 0.701 -2.234 -0.078 0.802 -1.238
1850 -0.163 4.134 -2.216 0.965 -1.157 0.403 0.305 0.148 -2.077 -2.701 2.390 2.358
1851 3.293 1.028 1.504 -1.658 -1.534 -1.621 -5.395 4.679 1.852 0.777 -1.769 1.742
1852 1.464 0.411 -2.502 -1.597 0.245 0.093 -1.134 2.943 -2.021 -1.646 -0.930 1.029")
示例代碼:
library(lubridate)
library(ggplot)
df_melt<-melt(df, id.var="Year")
df_melt$Year<- as.factor(df_melt$Year)
ggplot(df_melt, aes(x=value, y=variable, group=Year))
geom_col(aes(fill=value)) theme_bw()
facet_wrap(~as.factor(Year))
scale_fill_gradient(low="lightyellow", high="red")
labs(x="", y="", title="", fill="r")
theme_bw()
theme(plot.title = element_text(hjust = 0.5, face="bold", size=20, color="black"))
theme(axis.title.x = element_text(family="Times", face="bold", size=16, color="black"))
theme(axis.title.y = element_text(family="Times", face="bold", size=16, color="black"))
theme(axis.text.x = element_text( hjust = 1, face="bold", size=14, color="black") )
theme(axis.text.y = element_text( hjust = 1, face="bold", size=14, color="black") )
theme(plot.title = element_text(hjust = 0.5))
theme(legend.title = element_text(family="Times", color = "black", size = 16,face="bold"),
legend.text = element_text(family="Times", color = "black", size = 14,face="bold"),
legend.position="right",
plot.title = element_text(hjust = 0.5))
theme(strip.text.x = element_text(size = 16, colour = "black",family="Times", face="bold"))

uj5u.com熱心網友回復:
您需要整理資料并為列命名,以便以后識別它們。我們可以使用reshape2:
names(df) <- c("Year", month.name)
df <- melt(df, id.var = "Year", variable.name = "Month")
使用這樣格式化的資料,我們只需要使用正確的 aes in ggplot:
ggplot(df, aes(x = Month, y = value, group = Year, color = factor(Year)))
geom_line() geom_point()
uj5u.com熱心網友回復:
df %>%
pivot_longer(
-year
) %>%
ggplot(aes(x = year, y=value, color=factor(name), group=name))
geom_point()
geom_line()

像這樣的東西?
library(tidyverse)
colnames(df) <- c("year", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
df %>%
pivot_longer(
-year
) %>%
ggplot(aes(x = name, y=value, color=factor(year), group=year))
geom_point()
geom_line()

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/376485.html
標籤:r
上一篇:從資料框R中提取某些列
