在 R ggplot 中绘制多列并使图例正确

发布于 2025-01-20 18:23:06 字数 1139 浏览 1 评论 0 原文

数据框架,有5列和51行,如下:

Var1 var2 我有一个
1990 2.2 4.1
1991 2.3 4.4

我想绘制绘制,因为多年的列是多年来每个VAR的趋势的列。

我尝试了:

lt <- c("VAR2" = "twodash", "VAR3" = "dotted", "VAR4" = "dashed", "VAR5" = "solid")

ggp1 <- ggplot(DATAFRAME, aes(VAR1)) +       # Create ggplot2 plot
  geom_line(aes(y = VAR2, linetype = "twodash", group=1, )) +
  geom_line(aes(y = VAR3, linetype = "dotted", group=1)) +
  geom_line(aes(y = VAR4, linetype = "dashed", group=1)) + 
  geom_line(aes(y = VAR5, linetype = "solid", group=1)) +
  labs(y = "number of parties in government", x= "year", lintype = "Legend")+
  scale_linetype_manual(values = lt) 
ggp1 

但是它不起作用。我该怎么办?

谢谢大家的帮助。

I have a data frame, with 5 columns and 51 rows like the one below:

year var1 Var2
1990 2.2 4.1
1991 2.3 4.4

I wanted to graph as many lines s are the columns to see the trend of each var over the years.

I tried with:

lt <- c("VAR2" = "twodash", "VAR3" = "dotted", "VAR4" = "dashed", "VAR5" = "solid")

ggp1 <- ggplot(DATAFRAME, aes(VAR1)) +       # Create ggplot2 plot
  geom_line(aes(y = VAR2, linetype = "twodash", group=1, )) +
  geom_line(aes(y = VAR3, linetype = "dotted", group=1)) +
  geom_line(aes(y = VAR4, linetype = "dashed", group=1)) + 
  geom_line(aes(y = VAR5, linetype = "solid", group=1)) +
  labs(y = "number of parties in government", x= "year", lintype = "Legend")+
  scale_linetype_manual(values = lt) 
ggp1 

But it doesn't work. How can I do?

thank you all for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴骨ら 2025-01-27 18:23:06

这应该做到:

library(ggplot2)
DATAFRAME <- data.frame(
  VAR1 = 1:20, 
  VAR2 = runif(20,0,1), 
  VAR3 = runif(20,1,2), 
  VAR4 = runif(20,2,3), 
  VAR5 = runif(20,3,4)
)


lt <- c("VAR2" = "twodash", "VAR3" = "dotted", "VAR4" = "dashed", "VAR5" = "solid")

ggplot(DATAFRAME, aes(VAR1)) +       # Create ggplot2 plot
  geom_line(aes(y = VAR2, linetype = "VAR2", group=1, )) +
  geom_line(aes(y = VAR3, linetype = "VAR3", group=1)) +
  geom_line(aes(y = VAR4, linetype = "VAR4", group=1)) + 
  geom_line(aes(y = VAR5, linetype = "VAR5", group=1)) +
  labs(y = "number of parties in government", x= "year", lintype = "Legend") +
  scale_linetype_manual(values = unname(lt) )

“”

这里的技巧是,当您以这种方式创建美学时(例如, lineType =“ var2” var2 “ lineType =“ twodash” ,ggplot2将它们变成一个因素,并按字母顺序排序级别。在您的原始代码中,级别将是按顺序,虚线,点缀,固体,固体的, 然后将线型值的向量放在 ggplot

我上面做的是将级别放在预期的顺序中, 设置背景中创建的因子的级别。


ggplot(DATAFRAME, aes(VAR1)) +       # Create ggplot2 plot
  geom_line(aes(y = VAR2, linetype = "twodash", group=1, )) +
  geom_line(aes(y = VAR3, linetype = "dotted", group=1)) +
  geom_line(aes(y = VAR4, linetype = "dashed", group=1)) + 
  geom_line(aes(y = VAR5, linetype = "solid", group=1)) +
  labs(y = "number of parties in government", x= "year", lintype = "Legend") +
  scale_linetype_manual(values = unname(lt), breaks=c("twodash", "dotted", "dashed", "solid"))

还指定 在 scale_linetype_manual /I.SSTATIC.NET/BQOHO.PNG“ alt =”“>

在2022-04-12上由(v2.0.1)

This should do it:

library(ggplot2)
DATAFRAME <- data.frame(
  VAR1 = 1:20, 
  VAR2 = runif(20,0,1), 
  VAR3 = runif(20,1,2), 
  VAR4 = runif(20,2,3), 
  VAR5 = runif(20,3,4)
)


lt <- c("VAR2" = "twodash", "VAR3" = "dotted", "VAR4" = "dashed", "VAR5" = "solid")

ggplot(DATAFRAME, aes(VAR1)) +       # Create ggplot2 plot
  geom_line(aes(y = VAR2, linetype = "VAR2", group=1, )) +
  geom_line(aes(y = VAR3, linetype = "VAR3", group=1)) +
  geom_line(aes(y = VAR4, linetype = "VAR4", group=1)) + 
  geom_line(aes(y = VAR5, linetype = "VAR5", group=1)) +
  labs(y = "number of parties in government", x= "year", lintype = "Legend") +
  scale_linetype_manual(values = unname(lt) )

The trick here is that when you create aesthetics this way (e.g., linetype = "VAR2" or linetype = "twodash", ggplot2 will turn them into a factor and order the levels alphabetically. In your original code, the levels would be, in order, dashed, dotted, solid and twodash. What I did above was to put the levels in the intended order and then gave the vector of line-type values. Another catch is that ggplot wants that vector to be un-named.

You could also specify breaks in scale_linetype_manual to set the levels of the factor that is created in the background. Then, your code would work as well.


ggplot(DATAFRAME, aes(VAR1)) +       # Create ggplot2 plot
  geom_line(aes(y = VAR2, linetype = "twodash", group=1, )) +
  geom_line(aes(y = VAR3, linetype = "dotted", group=1)) +
  geom_line(aes(y = VAR4, linetype = "dashed", group=1)) + 
  geom_line(aes(y = VAR5, linetype = "solid", group=1)) +
  labs(y = "number of parties in government", x= "year", lintype = "Legend") +
  scale_linetype_manual(values = unname(lt), breaks=c("twodash", "dotted", "dashed", "solid"))

Created on 2022-04-12 by the reprex package (v2.0.1)

笑叹一世浮沉 2025-01-27 18:23:06

要将名称添加到图例中,您可以遵循u/DaveArmstrong的答案,但可以说更好的方法是将数据集转换为“长格式”或 整洁的数据框

总体思路是每一列代表一个变量,每一行代表一个观察值。列布局应为 年 |变量|值 而不是年份| VAR1 | VAR2 | VAR3...。有很多方法可以转换数据框。一种非常简单的方法是使用 pivot_longer() ,如下所示:

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

set.seed(8675309)  # so you get the same randomness I do

df <- data.frame(
  year = 2000:2020,
  VAR1 = rnorm(21, 10, 1),
  VAR2 = rnorm(21, 15, 2),
  VAR3 = rbinom(21, size=20, prob = 0.9),
  VAR4 = rcauchy(21, location=12, scale=2)
)

df %>% pivot_longer(cols = -year, names_to = "variable", values_to = "value") %>%
  ggplot(aes(x=year, y=value)) +
  geom_line(aes(linetype=variable)) +
  theme_bw()

在此处输入图像描述

To get the names into the legend, you can follow the answer from u/DaveArmstrong, but the arguably much better way would be to transform your dataset into a "long form" or Tidy Dataframe.

The general idea is that each column represents one variable and each row represents an observation. The column layout should be year | variable | value instead of year | VAR1 | VAR2 | VAR3.... There are lots of ways to convert your dataframe. A pretty straightforward way is to use pivot_longer() as you'll see below:

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

set.seed(8675309)  # so you get the same randomness I do

df <- data.frame(
  year = 2000:2020,
  VAR1 = rnorm(21, 10, 1),
  VAR2 = rnorm(21, 15, 2),
  VAR3 = rbinom(21, size=20, prob = 0.9),
  VAR4 = rcauchy(21, location=12, scale=2)
)

df %>% pivot_longer(cols = -year, names_to = "variable", values_to = "value") %>%
  ggplot(aes(x=year, y=value)) +
  geom_line(aes(linetype=variable)) +
  theme_bw()

enter image description here

初心未许 2025-01-27 18:23:06

您还可以使用 pivot_wider()将数据转换,因此您无需多次调用 Geom_line 。然后,您也可以将所有变量都馈入新变量。

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

DATAFRAME <- data.frame(
  VAR1 = 1:20, 
  VAR2 = runif(20,0,1), 
  VAR3 = runif(20,1,2), 
  VAR4 = runif(20,2,3), 
  VAR5 = runif(20,3,4)
)
#transpose to long format except your year variable
df2 <- pivot_longer(DATAFRAME, cols =  paste0("VAR",2:5))

ggplot(df2) +
  geom_line(aes(x=VAR1 , y = value,  group =name, linetype = name)) +
  labs(y = "number of parties in government", x= "year", lintype = "Legend")
 

you can also transpose the data with pivot_wider() so you don't have to call geom_line multiple times. You then can feed in the new variable with all of your variables to linetype as well.

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

DATAFRAME <- data.frame(
  VAR1 = 1:20, 
  VAR2 = runif(20,0,1), 
  VAR3 = runif(20,1,2), 
  VAR4 = runif(20,2,3), 
  VAR5 = runif(20,3,4)
)
#transpose to long format except your year variable
df2 <- pivot_longer(DATAFRAME, cols =  paste0("VAR",2:5))

ggplot(df2) +
  geom_line(aes(x=VAR1 , y = value,  group =name, linetype = name)) +
  labs(y = "number of parties in government", x= "year", lintype = "Legend")
 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文