[英]Plotting a graph with multiple geom_lines with loop
我有一個 dataframe,在幾個月內,一堆產品的價格變化百分比不同。 dataframe 是這樣的:
DATA P10 P25 P50 P75 P90
1 2011-03-01 0.034638180 0.086482130 0.133986300 0.177072700 0.233044900
2 2011-04-01 -0.185378000 -0.112070500 -0.064632480 -0.027086950 0.036643230
3 2011-05-01 0.008258164 0.053702510 0.094340370 0.137678700 0.270847900
4 2011-06-01 -0.105608500 -0.072065040 -0.019818160 0.018149950 0.069389460
5 2011-07-01 -0.080303930 -0.040885830 -0.006315288 0.030778970 0.084747610
6 2011-08-01 0.001524279 0.052229100 0.075928880 0.126691500 0.167735600
7 2011-09-01 -0.097216090 -0.066777680 -0.040682890 -0.014226140 0.034411750
我為創建 plot 而編寫的代碼是:
ggplot()+
geom_line(data = dataPerc, aes(x = dataPerc$DATA, y =dataPerc$P10,color="P10"),size=1)+
geom_line(data = dataPerc, aes(x = dataPerc$DATA, y =dataPerc$P25,color='P25'),size=1)+
geom_line(data = dataPerc, aes(x = dataPerc$DATA, y =dataPerc$P50,color = "P50"),size = 1)+
geom_line(data = dataPerc, aes(x = dataPerc$DATA, y =dataPerc$P75,color= "P75"),size=1)+
geom_line(data = dataPerc, aes(x = dataPerc$DATA, y =dataPerc$P90,color="P90"),size=1)+
scale_x_date(date_labels="%b %y",date_breaks ="1 month")+
theme(axis.text.x = element_text(angle = 90))+
labs(color='Percentile')+
scale_y_continuous(labels = function(x) paste0(x*100, "%"))+
xlab("Moth/Year")+
ylab("% fat. ")
基本上,我想創建相同的 plot 並用一個循環替換上面的 geom_lines 序列。 謝謝。
不要使用循環 - 將您的數據從寬轉換為長。
long_data = tidyr::pivot_longer(your_data, -DATA, names_to = "Percentile")
ggplot(long_data, aes(x = DATA, y = value, color = name)) +
geom_line(size = 1) +
theme(axis.text.x = element_text(angle = 90)) +
labs(x = "Month/Year", y = "% fat. ") +
scale_y_continuous(labels = scales::label_percent(accuracy = 0.1))
另外,不要在aes()
中使用data$column
- 它需要不帶引號的列名。
這是一個沒有你說你想要的循環的答案。 ggplot2
不適用於循環。
# Read in your data - changed `DATA` to `date`
a <-
"date P10 P25 P50 P75 P90
1 2011-03-01 0.034638180 0.086482130 0.133986300 0.177072700 0.233044900
2 2011-04-01 -0.185378000 -0.112070500 -0.064632480 -0.027086950 0.036643230
3 2011-05-01 0.008258164 0.053702510 0.094340370 0.137678700 0.270847900
4 2011-06-01 -0.105608500 -0.072065040 -0.019818160 0.018149950 0.069389460
5 2011-07-01 -0.080303930 -0.040885830 -0.006315288 0.030778970 0.084747610
6 2011-08-01 0.001524279 0.052229100 0.075928880 0.126691500 0.167735600
7 2011-09-01 -0.097216090 -0.066777680 -0.040682890 -0.014226140 0.034411750
"
df <- read.table(text = a, header = TRUE)
library(tidyr)
library(dplyr)
library(ggplot2)
# make the data tidy. ggplot2 needs tidy data (one observation per row)
df <- df %>% pivot_longer(cols = -date, names_to = "pct")
# format date as date
df$date <- as.Date(df$date)
ggplot(df, aes(x = date, y = value, color = pct)) +
geom_line(size=1) +
scale_x_date(date_labels="%b %y",date_breaks ="1 month") +
theme(axis.text.x = element_text(angle = 90)) +
labs(color='Percentile') +
scale_y_continuous(labels = function(x) paste0(x * 100, "%")) +
xlab("Month/Year") +
ylab("% fat. ")
```[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/OCk8O.png
一旦您的數據采用寬格式,可能性就無窮無盡。 我稍微簡化了您的代碼,並通過您的原始列名(例如,p10、p25、p50 等)對數據進行了分面。 這允許您在每個類別中獨立地 plot 單獨的行。 現在您可以在每個方面觀察從 3 月到 9 月的趨勢。 我將這些方面組織成一列。 只有單個月份名稱跨越 x 軸,因為您只有一年的數據。 隨意調整col =...
參數以找到正確的表示。
如果 faceting 不是你的風格,那么完全放棄對facet_wrap()
的調用並嘗試在aes()
中插入col = factor(perc)
) 。 這會將線堆疊在一個 plot 上; 您還可以免費獲得一個不錯的傳奇。 我將在下面演示這兩種方法。
# Here is how to avoid looping and layering on multiple geoms
library(tidyverse)
library(lubridate)
df <- tribble(
~date, ~p10, ~p25, ~p50, ~p75, ~p90,
"2011-03-01", 0.034638180, 0.086482130, 0.133986300, 0.177072700, 0.233044900,
"2011-04-01", -0.185378000, -0.112070500, -0.064632480, -0.027086950, 0.036643230,
"2011-05-01", 0.008258164, 0.053702510, 0.094340370, 0.137678700, 0.270847900,
"2011-06-01", -0.105608500, -0.072065040, -0.019818160, 0.018149950, 0.069389460,
"2011-07-01", -0.080303930, -0.040885830, -0.006315288, 0.030778970, 0.084747610,
"2011-08-01", 0.001524279, 0.052229100, 0.075928880, 0.126691500, 0.167735600,
"2011-09-01", -0.097216090, -0.066777680, -0.040682890, -0.014226140, 0.034411750)
# Some quick data preparation
long_df <- df %>%
mutate(date = ymd(date)) %>%
pivot_longer(-date, names_to = "perc", values_to = "p_scores")
# Here is a subset of the data frame in long format
# A tibble: 35 x 3
date perc p_scores
<date> <chr> <dbl>
1 2011-03-01 p10 0.0346
2 2011-03-01 p25 0.0865
3 2011-03-01 p50 0.134
4 2011-03-01 p75 0.177
5 2011-03-01 p90 0.233
6 2011-04-01 p10 -0.185
7 2011-04-01 p25 -0.112
8 2011-04-01 p50 -0.0646
9 2011-04-01 p75 -0.0271
10 2011-04-01 p90 0.0366
# … with 25 more rows
# Simplified code
ggplot(long_df, aes(x = date, y = p_scores)) +
geom_line(size = 1) +
scale_x_date("Month",
date_breaks = "1 month",
date_labels = '%B') +
scale_y_continuous("% Fat.", labels = function(x) paste0(x*100, "%")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
facet_wrap(~ perc, ncol = 1)
如果您想將線堆疊到一個 plot 上,以下是我的另一個建議。 似乎每條線都會隨着時間的推移同步移動,波動性不大。 為了重現性,我也包含了代碼。
ggplot(long_df, aes(x = date, y = p_scores, col = factor(perc))) +
geom_line(size = 1) +
scale_x_date("Month",
date_breaks = "1 month",
date_labels = '%B') +
scale_y_continuous("% Fat.", labels = function(x) paste0(x*100, "%")) +
labs(color = "Score \nType:") + # This is a generic legend title
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
此外,您可以在不將perc
轉換為aes()
內部的因子變量的情況下獲得相同的結果。 但我離題了。
我希望這有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.