使用R的ggplot库,将数据框的所有列绘制成多条线,并使用相同的颜色。

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

R ggplot - plot all columns of dataframe as multiple lines with the same color

问题

以下是您要翻译的部分:

"I want to plot all columns of a dataframe as separate lines in one plot. They all should have the same color (grey, dashed, in the Background). I then want to plot the mean of all values as solid fat line in the front.

My code so far works for all requirements except of the same color for all the separate lines:

  set.seed(200)                                            
  df <- data.frame(x = 1:10,
                   y1 = rnorm(5),
                   y2 = runif(5),
                   y3 = rpois(5, 1))
  
  df[,"mean"] = rowMeans(df[,c(2:4)])
  
  # Reshape data frame
  df_reshaped <- data.frame(x = df$x,                           
                            y = c(df$y1, df$y2, df$y3),
                            group = c(rep("y1", nrow(df)),
                                      rep("y2", nrow(df)),
                                      rep("y3", nrow(df))))
  
  ggplot(data = df_reshaped, aes(x=x)) +
    geom_line(
      data = df_reshaped,
      aes(y = y, col=group),
      linetype = "dashed",
      # color = "grey", # this part does not work properly
      linewidth = 0.25
    ) + 
    geom_line(
      data = df,
      aes(y = mean),
      linetype = "solid",
      color = "black",
      linewidth = 0.5
    )

My result is:
使用R的ggplot库,将数据框的所有列绘制成多条线,并使用相同的颜色。

When I uncomment the line # color = "grey" I get a weird result that looks like this:
使用R的ggplot库,将数据框的所有列绘制成多条线,并使用相同的颜色。

icing on the cake: Add a legend that says:

---- single values
____ mean value

Thanks, I appreciate your help!"

希望这有助于您的需求。

英文:

I want to plot all columns of a dataframe as separate lines in one plot. They all should have the same color (grey, dashed, in the Background). I then want to plot the mean of all values as solid fat line in the front.

My code so far works for all requirements except of the same color for all the separate lines:

  set.seed(200)                                            
  df <- data.frame(x = 1:10,
                   y1 = rnorm(5),
                   y2 = runif(5),
                   y3 = rpois(5, 1))
  
  df[,"mean"] = rowMeans(df[,c(2:4)])
  
  # Reshape data frame
  df_reshaped <- data.frame(x = df$x,                           
                            y = c(df$y1, df$y2, df$y3),
                            group = c(rep("y1", nrow(df)),
                                      rep("y2", nrow(df)),
                                      rep("y3", nrow(df))))
  
  ggplot(data = df_reshaped, aes(x=x)) +
    geom_line(
      data = df_reshaped,
      aes(y = y, col=group),
      linetype = "dashed",
      # color = "grey", # this part does not work properly
      linewidth = 0.25
    ) + 
    geom_line(
      data = df,
      aes(y = mean),
      linetype = "solid",
      color = "black",
      linewidth = 0.5
    )

My result is:
使用R的ggplot库,将数据框的所有列绘制成多条线,并使用相同的颜色。

When I uncomment the line # color = "grey" I get a weird result that looks like this:
使用R的ggplot库,将数据框的所有列绘制成多条线,并使用相同的颜色。

icing on the cake: Add a legend that says:

---- single values
____ mean value

Thanks, I appreciate your help!

答案1

得分: 2

你可以使用scale_color_manualvalues来添加颜色,因为你已经将它分配给了你的美学,就像这样:

library(ggplot2)

ggplot(data = df_reshaped, aes(x=x)) +
  geom_line(
    data = df_reshaped,
    aes(y = y, col=group),
    linetype = "dashed",
    # color = "grey", # 这部分不能正常工作
    linewidth = 0.25
  ) + scale_color_manual(values = rep("grey", 3)) + 
  geom_line(
    data = df,
    aes(y = mean),
    linetype = "solid",
    color = "black",
    linewidth = 0.5
  )

使用R的ggplot库,将数据框的所有列绘制成多条线,并使用相同的颜色。

创建于2023年07月04日,使用reprex v2.0.2

英文:

You could add the colors using scale_color_manual with values since you assigned it to your aesthetics like this:

library(ggplot2)

ggplot(data = df_reshaped, aes(x=x)) +
  geom_line(
    data = df_reshaped,
    aes(y = y, col=group),
    linetype = "dashed",
    # color = "grey", # this part does not work properly
    linewidth = 0.25
  ) + scale_color_manual(values = rep("grey", 3)) + 
  geom_line(
    data = df,
    aes(y = mean),
    linetype = "solid",
    color = "black",
    linewidth = 0.5
  )

使用R的ggplot库,将数据框的所有列绘制成多条线,并使用相同的颜色。<!-- -->

<sup>Created on 2023-07-04 with reprex v2.0.2</sup>

答案2

得分: 1

另一种方法是将数据编译成一个长数据框,这样你就可以得到y值的图例以及均值。

library(ggplot2)
library(dplyr)
library(tidyr)

set.seed(200)                                            
df1 <- 
  data.frame(x = 1:10,
             y1 = rnorm(5),
             y2 = runif(5),
             y3 = rpois(5, 1)) %>%
  rowwise() %>%
  mutate(mean = mean(c(y1, y2, y3))) %>%
  pivot_longer(-x)



ggplot(df1, ) +
  geom_line(aes(x = x, y = value, linetype = name, linewidth = name, colour = name))+
  scale_linetype_manual(breaks = c("y1", "y2", "y3", "mean"),
                        values = c(rep("dashed", 3), "solid")) +
  scale_linewidth_manual(breaks = c("y1", "y2", "y3", "mean"),
                         values = c(rep(0.25, 3), 0.5)) +
  scale_colour_manual(breaks = c("y1", "y2", "y3", "mean"),
               values = c(rep("grey50", 3), "black"))+
  labs(y = "y",
       linetype = NULL,
       linewidth = NULL,
       colour = NULL)+
  theme_bw()

使用R的ggplot库,将数据框的所有列绘制成多条线,并使用相同的颜色。

Created on 2023-07-04 with reprex v2.0.2

英文:

Alternative approach compiling the data into one long dataframe gives you the legend for the y values as well as the mean.

library(ggplot2)
library(dplyr)
library(tidyr)

set.seed(200)                                            
df1 &lt;- 
  data.frame(x = 1:10,
             y1 = rnorm(5),
             y2 = runif(5),
             y3 = rpois(5, 1)) |&gt; 
  rowwise()|&gt;
  mutate(mean = mean(c(y1, y2, y3))) |&gt; 
  pivot_longer(-x)



ggplot(df1, ) +
  geom_line(aes(x = x, y = value, linetype = name, linewidth = name, colour = name))+
  scale_linetype_manual(breaks = c(&quot;y1&quot;, &quot;y2&quot;, &quot;y3&quot;, &quot;mean&quot;),
                        values = c(rep(&quot;dashed&quot;, 3), &quot;solid&quot;)) +
  scale_linewidth_manual(breaks = c(&quot;y1&quot;, &quot;y2&quot;, &quot;y3&quot;, &quot;mean&quot;),
                         values = c(rep(0.25, 3), 0.5)) +
  scale_colour_manual(breaks = c(&quot;y1&quot;, &quot;y2&quot;, &quot;y3&quot;, &quot;mean&quot;),
               values = c(rep(&quot;grey50&quot;, 3), &quot;black&quot;))+
  labs(y = &quot;y&quot;,
       linetype = NULL,
       linewidth = NULL,
       colour = NULL)+
  theme_bw()

使用R的ggplot库,将数据框的所有列绘制成多条线,并使用相同的颜色。<!-- -->

<sup>Created on 2023-07-04 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年7月4日 21:40:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613246.html
匿名

发表评论

匿名网友

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

确定