gpt4 book ai didi

r - 如何为次轴ggplot添加图例

转载 作者:行者123 更新时间:2023-12-04 13:21:36 27 4
gpt4 key购买 nike

我正在尝试在以下代码中为辅助轴添加图例

library(ggplot2)
library(dplyr)
library(reshape2)

df = data.frame(period = seq(as.POSIXct("2017-01-01"),as.POSIXct("2017-12-01"), by = "month"),
b = c(100, 110, 105, 200, 210, 190, 180, 170, 165, 175, 140, 145),
c = c(120, 130, 150, 170, 250, 160, 130, 120, 110, 130, 120, 170),
d = c(1060, 1180, 1050, 2070, 2150, 1900, 1850, 1070, 1605, 1750, 1460, 1250)) %>%
mutate(period = factor(period))

df_bar = melt(df, id.vars = "period", measure.vars = c("b", "c", "d")) %>% filter(variable != "d")

df_line = df %>% select(period, d)

ggplot(data = df_bar, aes(x = period, y = value, fill = variable)) +
geom_bar(stat = "identity", position = "dodge") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
theme(axis.text=element_text(size=9),
axis.title=element_text(size=14,face="bold")) +
ylab("primary axis") +
geom_line(data = df_line, aes(x = period, y = (d)/10, group = 1), inherit.aes = FALSE) +
geom_point(data = df_line, aes(x = period, y = (d)/10, group = 1), inherit.aes = FALSE) +
scale_y_continuous(sec.axis = sec_axis(~.*10, name = "secondary axis"))

plot

我也想要折线图的图例。

最佳答案

您可以在 geom_line 调用中的 aes 内添加 linetype 来为该线创建一个单独的图例,然后将其图例移近 填充图例

另见 answer

library(reshape2)
library(tidyverse)

ggplot(data = df_bar, aes(x = period, y = value, fill = variable)) +
geom_bar(stat = "identity", position = "dodge") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
theme(
axis.text = element_text(size = 9),
axis.title = element_text(size = 14, face = "bold")
) +
ylab("primary axis") +
geom_line(data = df_line, aes(x = period, y = (d) / 10, group = 1, linetype = "My line"), inherit.aes = FALSE) +
scale_linetype_manual(NULL, values = 1) +
geom_point(data = df_line, aes(x = period, y = (d) / 10, group = 1), inherit.aes = FALSE) +
scale_y_continuous(sec.axis = sec_axis(~. * 10, name = "secondary axis")) +
theme(legend.background = element_rect(fill = "transparent"),
legend.box.background = element_rect(fill = "transparent", colour = NA),
legend.key = element_rect(fill = "transparent"),
legend.spacing = unit(-1, "lines"))

要获得同一图例中的点和线,我们可以将 color 映射到 aes 并使用 scale_color_manual

ggplot(data = df_bar, aes(x = period, y = value, fill = variable)) +
geom_bar(stat = "identity", position = "dodge") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
theme(
axis.text = element_text(size = 9),
axis.title = element_text(size = 14, face = "bold")
) +
ylab("primary axis") +
geom_line(data = df_line, aes(x = period, y = (d) / 10, group = 1, color = "My line"), inherit.aes = FALSE) +
scale_color_manual(NULL, values = "black") +
geom_point(data = df_line, aes(x = period, y = (d) / 10, group = 1, color = "My line"), inherit.aes = FALSE) +
scale_y_continuous(sec.axis = sec_axis(~. * 10, name = "secondary axis")) +
theme(legend.background = element_rect(fill = "transparent"),
legend.box.background = element_rect(fill = "transparent", colour = NA),
legend.key = element_rect(fill = "transparent"),
legend.spacing = unit(-1, "lines"))

reprex package 创建于 2018-07-21 (v0.2.0.9000).

关于r - 如何为次轴ggplot添加图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51456307/

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