英文:
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
)
When I uncomment the line # color = "grey"
I get a weird result that looks like this:
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
)
When I uncomment the line # color = "grey"
I get a weird result that looks like this:
icing on the cake: Add a legend that says:
---- single values
____ mean value
Thanks, I appreciate your help!
答案1
得分: 2
你可以使用scale_color_manual
和values
来添加颜色,因为你已经将它分配给了你的美学,就像这样:
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
)
创建于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
)
<!-- -->
<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()
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 <-
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()
<!-- -->
<sup>Created on 2023-07-04 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论