绘制带有循环的多个geom_lines的图形

问题描述 投票:0回答:3

我有一个数据框,在几个月内,一堆产品的价格变化的百分比值不同。数据框是这样的:

 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

我编写的用于创建情节的代码是:

 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. ")

[基本上,我想用一个循环创建相同的图,该循环替代上面的geom_lines序列。谢谢。

r loops ggplot2
3个回答
1
投票

不要使用循环-将数据从宽转换为长。

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))      

此外,请勿在data$column中使用aes()-它需要未引用的列名。


0
投票

这里是您没有想要的循环的答案。 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

0
投票

一旦您的数据采用了广泛的格式,可能性将无穷无尽。我对您的代码做了一些简化,并用原始列名(例如p10,p25,p50等)对其进行了说明。这使您可以独立观察每条线。现在,您可以在每个方面(得分类型)中查看从3月到9月的趋势。由于您的所有月份都在同一年内,因此我仅在X轴上包含一列,并指定了各个月份的名称。随时调整col = ...参数以找到正确的演示文稿。 如果刻面不是您的风格,则将呼叫完全放到facet_wrap(),然后尝试将col = factor(perc))插入aes()的内部。这将在一条图上将线堆叠在一起。您还可以免费获得一个漂亮的传说。

# 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)

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS90NHBhNy5qcGdzIn0=” width =“ 400” height =“ 999”>

以下是我的另一项建议,如果要将线堆叠到一个图上。看起来每条线都随着时间而串联移动,而没有太大的波动。我还提供了代码以提高可重复性。

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:") + theme_bw() + theme(axis.text.x = element_text(angle = 45, hjust = 1))

我希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.