R-ggplot2图例未出现在折线图中 [英] R - ggplot2 Legend not appearing for line graph

查看:146
本文介绍了R-ggplot2图例未出现在折线图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前曾有人问过这个问题,我已经看过许多链接,但似乎没有一个对我的情况有所帮助.我正在为4条不同颜色的线绘制线图.但我无法显示图例.

I know this question has been asked before, and I've looked at many of the links, but none of them seem to be helping my case. I'm plotting a line graph for 4 lines of different colors. But I can't get the legend to appear.

我已经读到我需要将color属性放在图形的aes部分中.那也没有成功.

I've read that I need to put the color attribute in the aes part of the graph. That hasn't been successful either.

我有一个四列和1000行的数据框.这是一个可重现的小示例,它显示了我的数据以及如何绘制数据.

I have a data frame of four column, and 1000 rows. Here's a small reproducible example of what my data looks like, and how I'd like to plot it.

library(ggplot2)

vec1 <- c(0.1, 0.2, 0.25, 0.12, 0.3, 0.7, 0.41)
vec2 <- c(0.5, 0.4, 0.3, 0.55, 0.12, 0.12, 0.6)
vec3 <- c(0.01, 0.02, 0.1, 0.5, 0.14, 0.2, 0.5)
vec4 <- c(0.08, 0.1, 0.54, 0.5, 0.1, 0.12, 0.3)

df <- data.frame(vec1, vec2, vec3, vec4)

df_plot <- ggplot() +
  geom_line(data = df, color = "black", aes(x = c(1:7), y = df[,1], color = 
"black")) +
  geom_line(data = df, color = "blue", aes(x = c(1:7), y = df[,2], color = 
"blue")) +
  geom_line(data = df, color = "green", aes(x = c(1:7), y = df[,3], color = 
"green")) +
  geom_line(data = df, color = "yellow", aes(x = c(1:7), y = df[,4], color 
= "yellow")) +
  xlab("x axis") +
  ylab("y axis") +
  ggtitle("A random plot") +
  theme(legend.title = element_text("Four lines"), legend.position = 
"right")

(此外,SO是否更改了缩进代码的过程?在此之前,我可以按Ctrl + K来缩进整个代码块.但是我不能再这样做了.Ctrl + K将光标放在我的URL中框出于某种原因)

(Also, did SO change the process of indenting code? Before, I could just press Ctrl + K to indent the entire block of code. But I can't do that anymore. Ctrl+K puts the cursor in my URL box for some reason)

我希望它在图的右侧打印图例.

I'd like ti it print the legend to the right of the graph.

推荐答案

首先:我看到很多人通过首先创建单个矢量来创建数据帧.我不知道这种做法的起源,但这不是必须的:

First: I see a lot of people here creating data frames by first creating individual vectors. I don't know where this practice originated but it isn't necessary:

df1 <- data.frame(vec1 = c(0.1, 0.2, 0.25, 0.12, 0.3, 0.7, 0.41),
                  vec2 = c(0.5, 0.4, 0.3, 0.55, 0.12, 0.12, 0.6),
                  vec3 = c(0.01, 0.02, 0.1, 0.5, 0.14, 0.2, 0.5),
                  vec4 = c(0.08, 0.1, 0.54, 0.5, 0.1, 0.12, 0.3))

下一步:您的数据为宽"格式. ggplot2 在长"格式下效果更好:一列用于变量,另一列用于变量.您可以使用 tidyr :: gather 达到目的.在此过程中,我们可以使用 dplyr :: mutate 添加x变量:

Next: your data is in "wide" form. ggplot2 works better with "long" form: one column for variables, another for their values. You can get to that using tidyr::gather. While we're at it, we can use dplyr::mutate to add the x variable:

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

df1 %>% 
  gather(Var, Val) %>% 
  mutate(x = rep(1:7, 4))

现在我们可以绘图.使用这种形式的数据,无需为每个变量使用单独的geom,并且 aes()会自动处理颜色和图例.您可以使用 scale_color_manual 指定自定义颜色.我不知道黄色或绿色是不错的选择,但是在这里:

Now we can plot. With the data in this form, there is no need to use a separate geom for each variable and aes() automatically takes care of colors and legends. You can specify custom colors using scale_color_manual. I don't know that yellow or green are great choices, but here it is:

df1 %>% 
  gather(Var, Val) %>% 
  mutate(x = rep(1:7, 4)) %>% 
  ggplot(aes(x, Val)) + 
    geom_line(aes(color = Var)) + 
    scale_color_manual(values = c("black", "blue", "green", "yellow"))

关键是要以正确的格式存储数据,并了解如何使 aes 将变量映射到统计图属性.

The key is having your data in the correct format, and understanding how that allows aes to map variables to chart properties.

这篇关于R-ggplot2图例未出现在折线图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆