gpt4 book ai didi

R - (ggplot2 library) - 图例不显示在图表上

转载 作者:行者123 更新时间:2023-12-01 07:57:37 24 4
gpt4 key购买 nike

我在做什么

我正在使用一个名为 ggplot2 的 R 库,它允许许多不同的选项来创建图形和其他内容。我正在使用它在一张图表上显示两个不同的数据集,并为我要显示的每组数据使用不同的颜色。

问题

我还试图在我的图表中显示一个图例,告诉用户哪组数据对应于哪种颜色。到目前为止,我无法让它显示出来。

我尝试过的

我已将它设置为在顶部/底部/左侧/右侧有一个位置,以确保没有任何东西使它的位置变为 默认情况下,它会隐藏它。

代码

# PDF/Plot generation
pdf("activity-plot.pdf")
ggplot(data.frame("Time"=times), aes(x=Time)) +

#Data Set 1
geom_density(fill = "#1A3552", colour = "#4271AE", alpha = 0.8) +
geom_text(x=mean(times)-1, y=max(density(times)$y/2), label="Mean {1} Activity", angle=90, size = 4) +
geom_vline(aes(xintercept=mean(times)), color="cyan", linetype="dashed", size=1, alpha = 0.5) +

# Data Set 2
geom_density(data=data.frame("Time"=timesSec), fill = "gray", colour = "orange", alpha = 0.8) +
geom_text(x=mean(timesSec)-1, y=max(density(timesSec)$y/2), label="Mean {2} Activity", angle=90, size = 4) +
geom_vline(aes(xintercept=mean(timesSec)), color="orange", linetype="dashed", size=1, alpha = 0.5) +

# Main Graph Info
labs(title="Activity in the past 48 hours", subtitle="From {DATE 1} to {DATE 2}", caption="{LOCATION}") +
scale_x_continuous(name = "Time of Day", breaks=seq(c(0:23))) +
scale_y_continuous(name = "Activity") +
theme(legend.position="top")

dev.off()

结果

image

最佳答案

正如@Ben 所指出的,您应该将颜色传递给 aes 以便显示图例。

但是,获得 ggplot 的更好方法是将您的两个值“Time”和“Timesec”合并到一个数据帧中,并将您的数据帧 reshape 为更长的格式。在这里,为了说明这一点,我创建了这个虚拟数据框:

Time = sample(1:24, 200, replace = TRUE)
Timesec = sample(1:24, 200, replace = TRUE)
df <- data.frame(Time, Timesec)

Time Timesec
1 22 23
2 21 9
3 19 9
4 10 6
5 7 24
6 15 9
... ... ...

因此,第一步是将数据框 reshape 为更长的格式。在这里,我使用 tidyr 包中的 pivot_longer 函数:

library(tidyr)
library(dplyr)
df %>% pivot_longer(everything(), names_to = "var",values_to = "val")

# A tibble: 400 x 2
var val
<chr> <int>
1 Time 22
2 Timesec 23
3 Time 21
4 Timesec 9
5 Time 19
6 Timesec 9
7 Time 10
8 Timesec 6
9 Time 7
10 Timesec 24
# … with 390 more rows

要根据您的值的平均值添加 geom_vlinegeom_text,一种轻松完成此操作的好方法是创建第二个数据框,收集平均值和最大密度需要绘制的值:

library(tidyr)
library(dplyr)
df_lab <- df %>% pivot_longer(everything(), names_to = "var",values_to = "val") %>%
group_by(var) %>%
summarise(Mean = mean(val),
Density = max(density(val)$y))

# A tibble: 2 x 3
var Mean Density
<chr> <dbl> <dbl>
1 Time 11.6 0.0555
2 Timesec 12.1 0.0517

因此,使用 dfdf_lab,您可以生成整个图。在这里,我们将 colorfill 参数传递给 aes 并使用 scale_color_manualscale_fill_manual 设置适当的颜色:

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

df %>% pivot_longer(everything(), names_to = "var",values_to = "val") %>%
ggplot(aes(x = val, fill = var, colour = var))+
geom_density(alpha = 0.8)+
scale_color_manual(values = c("#4271AE", "orange"))+
scale_fill_manual(values = c("#1A3552", "gray"))+
geom_vline(inherit.aes = FALSE, data = df_lab,
aes(xintercept = Mean, color = var), linetype = "dashed", size = 1,
show.legend = FALSE)+
geom_text(inherit.aes = FALSE, data = df_lab,
aes(x = Mean-0.5, y = Density/2, label = var, color = var), angle = 90,
show.legend = FALSE)+
labs(title="Activity in the past 48 hours", subtitle="From {DATE 1} to {DATE 2}", caption="{LOCATION}") +
scale_x_continuous(name = "Time of Day", breaks=seq(c(0:23))) +
scale_y_continuous(name = "Activity") +
theme(legend.position="top")

enter image description here

它能回答您的问题吗?

关于R - (ggplot2 library) - 图例不显示在图表上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60479670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com