我创建了自己的数据框,如何在 ggplot 中创建图例?

发布于 2025-01-17 21:58:00 字数 539 浏览 3 评论 0原文

我已经创建了自己的data.frame并尝试绘制它。 但是,我只是无法让我的传奇弹出。

我认为在数据的情况下出现了问题。框架部分..但不清楚

有人可以帮我吗?

data <- data.frame(x=c(1, 2, 3, 4, 5),
                y1=c(1, 3, 5, 6, 3),
                y2=c(2, 4, 1, 3, 4))
data

data_plot <- ggplot(data, aes(x), col=group) + geom_smooth(aes(y=y1), color='black') + geom_smooth(aes(y=y2), color='blue') + labs(x="xxx", y="yyy", title="I need help")

data_plot

单击以检查图像

I have created my own data.frame and trying to plot it.
However, I just cant get my legend to pop up.

I think something is wrong on the case of the data.frame part.. but not clear

Could anyone please help me?

data <- data.frame(x=c(1, 2, 3, 4, 5),
                y1=c(1, 3, 5, 6, 3),
                y2=c(2, 4, 1, 3, 4))
data

data_plot <- ggplot(data, aes(x), col=group) + geom_smooth(aes(y=y1), color='black') + geom_smooth(aes(y=y2), color='blue') + labs(x="xxx", y="yyy", title="I need help")

data_plot

click to check image

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

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

发布评论

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

评论(2

來不及說愛妳 2025-01-24 21:58:00

您必须在 aes(...) 中指定颜色,否则它们不会在自动生成的图例中弹出:

library(tidyverse)

data <- data.frame(
  x = c(1, 2, 3, 4, 5),
  y1 = c(1, 3, 5, 6, 3),
  y2 = c(2, 4, 1, 3, 4)
)
data
#>   x y1 y2
#> 1 1  1  2
#> 2 2  3  4
#> 3 3  5  1
#> 4 4  6  3
#> 5 5  3  4

data_plot <- ggplot(data, aes(x), col = group) +
  geom_smooth(aes(y = y1, color = "group 1")) +
  geom_smooth(aes(y = y2, color = "group2")) +
  labs(x = "xxx", y = "yyy", title = "I need help")

data_plot

# long format data frames are better to map group column to color
data %>%
  pivot_longer(matches("y"), values_to = "y") %>%
  ggplot(aes(x, y, color = name)) +
    geom_smooth()

reprex 包 (v2.0.0)

You must specify colors inside aes(...), otherwise they would not pop up in the automatically generated legend:

library(tidyverse)

data <- data.frame(
  x = c(1, 2, 3, 4, 5),
  y1 = c(1, 3, 5, 6, 3),
  y2 = c(2, 4, 1, 3, 4)
)
data
#>   x y1 y2
#> 1 1  1  2
#> 2 2  3  4
#> 3 3  5  1
#> 4 4  6  3
#> 5 5  3  4

data_plot <- ggplot(data, aes(x), col = group) +
  geom_smooth(aes(y = y1, color = "group 1")) +
  geom_smooth(aes(y = y2, color = "group2")) +
  labs(x = "xxx", y = "yyy", title = "I need help")

data_plot

# long format data frames are better to map group column to color
data %>%
  pivot_longer(matches("y"), values_to = "y") %>%
  ggplot(aes(x, y, color = name)) +
    geom_smooth()

Created on 2022-03-30 by the reprex package (v2.0.0)

过期以后 2025-01-24 21:58:00

您需要在aes()中定义color,例如geom_smooth(aes(y=y1, color='black'))并使用scale_color_identity () 分配颜色。身份尺度的默认值为 guide = "none"

library(ggplot2)
library(ggthemes)

    ggplot(data, aes(x), col=group) + 
  geom_smooth(aes(y=y1, color='black')) + 
  geom_smooth(aes(y=y2, color='blue')) + 
  scale_color_identity(guide = "legend")+
  labs(x="X-axis", y="Y-axis", title="How to make my legend visible", color="Legend")+
  theme_base()+
  theme(axis.text.x = element_text(hjust = 1, face="bold", size=12, color="black"), 
        axis.title.x = element_text(face="bold", size=16, color="black"),
        axis.text.y = element_text(face="bold", size=12, color="black"),
        axis.title.y = element_text(face="bold", size=16, color="black"),
        legend.text = element_text(color = "black", size = 16,face="bold"),
        legend.title = element_text(face="bold", size=16, color="black"),
        legend.position="top",
        legend.box = "horizontal",
        plot.title = element_text(hjust = 0.5, size=20, face="bold"))

情节:

在此处输入图像描述

示例数据:

data <- data.frame(x=c(1, 2, 3, 4, 5),
                   y1=c(1, 3, 5, 6, 3),
                   y2=c(2, 4, 1, 3, 4))
data

You need to define color in aes(), such as geom_smooth(aes(y=y1, color='black')) and use scale_color_identity () to assign colours. The default is guide = "none" for identity scales.

library(ggplot2)
library(ggthemes)

    ggplot(data, aes(x), col=group) + 
  geom_smooth(aes(y=y1, color='black')) + 
  geom_smooth(aes(y=y2, color='blue')) + 
  scale_color_identity(guide = "legend")+
  labs(x="X-axis", y="Y-axis", title="How to make my legend visible", color="Legend")+
  theme_base()+
  theme(axis.text.x = element_text(hjust = 1, face="bold", size=12, color="black"), 
        axis.title.x = element_text(face="bold", size=16, color="black"),
        axis.text.y = element_text(face="bold", size=12, color="black"),
        axis.title.y = element_text(face="bold", size=16, color="black"),
        legend.text = element_text(color = "black", size = 16,face="bold"),
        legend.title = element_text(face="bold", size=16, color="black"),
        legend.position="top",
        legend.box = "horizontal",
        plot.title = element_text(hjust = 0.5, size=20, face="bold"))

Plot:

enter image description here

Sample data:

data <- data.frame(x=c(1, 2, 3, 4, 5),
                   y1=c(1, 3, 5, 6, 3),
                   y2=c(2, 4, 1, 3, 4))
data
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文